-
-
Notifications
You must be signed in to change notification settings - Fork 670
String Hash Switch Argument #1518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@jtenner I don't know if your code sample is equivalent. In my current understanding, your code would result in run-time comparison of if (str == "constant") {
} The goal is to avoid byte for byte comparison of a long string, and instead be able to compare two switch (hash(str)) {
// 20+ cases
} *please correct if I'm incorrect |
I made a mistake with the comment and deleted it. It wasn't relevant. One of the things you can do is apply an AST transform for switch(hash(str)) {
case hash`constant string`:
} I don't know if @dcodeIO has implemented the parser for TaggedTemplateLiterals, but that sort of thing could be incredibly helpful for devs. |
Note that For strings specifically, some sort of multi-level table (switch on length and single chars before doing exact compare) might yield better results. |
Yes, I remember we talked about this a while ago. The best approach is static trie (multi-level table) which also scales well from one char to many while hash apprach has a lot of disadvantages and also required special handling for one-char cases and collision resolving. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
In order to enable
string
as a validswitch
argument type, create a compile-time & run-time string hashing function. For example:would be interpreted by the compiler as:
where
__hash
is:The text was updated successfully, but these errors were encountered: