-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
MBE (macro_rules!) pattern-matching is unnecessarily O(n) even in simple cases. #68836
Copy link
Copy link
Open
Labels
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.I-compiletimeIssue: Problems and improvements with respect to compile times.Issue: Problems and improvements with respect to compile times.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.I-compiletimeIssue: Problems and improvements with respect to compile times.Issue: Problems and improvements with respect to compile times.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
There are some crates which do something like this (e.g.
string_cache_codegen):(@nnethercote came across this when profiling
html5ever's compilation)Also, prefixing patterns with
@footo create "internal rules" is common.But the current implementation (AFAICT) has to check each of the patterns in turn.
Instead, we could build something like a decision tree ahead of time, from the constant (i.e.
$-less) prefix of all arms, using maps for operators/idents/literals, to make the pattern-matching of the prefix amortize to being linear in the number of input tokens.We could keep it simple by limiting the constant prefix to leaf tokens (no
(...),[...]or{...}).For the example above, we'd pre-compute something like this:
Whereas for something using
@foorules it could be:(where
DecisionTree::Doneindicates the arms to continue with)cc @rust-lang/compiler (this might need a design meeting?)