3 ms·
In the main scanner function, a few minor performance squeezing notes I'd like to test: 1. Moving "length := 0" above the for loop, since it's reassigned in al
by sbr464 6y ago
In the main scanner function, a few minor performance squeezing notes I'd like to test:
1. Moving "length := 0" above the for loop, since it's reassigned in all the needed cases.
2. To avoid having an extra "if whitespace[c]", Including the whitespace cases in the main switch statement, even if it means duplicating or moving "s.br.release"?
Or, using a switch statement vs a lookup ("whitespace[c]"), if it must be done.
3. In the switch statement, using multiple assignment (in most cases):
length = validateToken(&s.br, "false")
s.pos = length
//
s.pos = length = validateToken(&s.br, "false")
4. In the String and default cases, inlining the length assignment within the if statement.
5. Returning "s.br.window()[:length]" in each case vs breaking out of the switch statement to return. Even though it's ugly, to avoid one step.
6. I'm curious if any performance could be gained by including more cases for common characters (A-Z,a-z, 0-9), to avoid using the default case. Testing if there is a penalty for using a default case vs more cases, even if it's ugly.
7. Including additional cases for exact values to avoid extra function calls to "parseString(&s.br)" or "s.parseNumber()".
8. I'm curious in some cases, if peeking at the next character with a nested switch statement, could avoid additional iterations or function calls to validate/release.
9. In the whitespace check, peeking for common JSON formatting patterns to avoid iterations. Such as 2 or 4 spaced json, a new line, followed by tabs or spaces etc. Or possibly establishing that the JSON is "probably2Spaced/probably4Spaced" and then peeking more efficiently?
- deleted 6y ago[deleted]
- e12e 6y ago> 7. Including additional cases for exact values to avoid extra function calls to "parseString(&s.br)" or "s.parseNumber()". I can see how you might choose some numbers to optimize for (1..10 for example) - but strings? You could of course do a frequency analysis of the test data - but would that help in general, beyond just cheating on the benchmark? I guess you could try for "key" and "value", and maybe "id"? Possibly adding "email" and "name"? Also from tfa regarding numbers: > Scanner.parseNumber is slow because it visits its input twice; once at the scanner and a second time when it is converted to a float. I did an experiment and the first parse can be faster if we just look to find the termination of the number without validation, canada.json went from 650mb/s to 820mb/sec.