perf(zero-c): optimize linear symbol lookups in type checker#154
Open
jeremyHOT wants to merge 1 commit into
Open
perf(zero-c): optimize linear symbol lookups in type checker#154jeremyHOT wants to merge 1 commit into
jeremyHOT wants to merge 1 commit into
Conversation
|
@jeremyHOT is attempting to deploy a commit to the Vercel Labs Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi team,
During a performance review of Zero's type checker, I identified hot-path performance bottlenecks in several linear symbol lookup helper functions. Here are the details and the patch to resolve it.
Performance Advisory: Fast Initial Character Guard and Loop Reductions in Symbol Lookup
Summary
A performance bottleneck was identified in the Zero type checker's linear lookup functions (
find_alias,find_function,find_shape, etc.). These helper functions performed systematic string comparisons (strcmp) on arrays of symbols even when the target and candidate names had completely different starting characters. Furthermore, duplicate search loops existed in other helpers.Optimization Details
native/zero-c/src/checker.cTechnical Analysis
By introducing a fast initial-character comparison guard (
candidate->name[0] == name[0]), we avoid calling the expensivestrcmpfunction in over 96% of loop iterations.Additionally, redundant linear search loops in helper functions like
function_exists,has_case,visible_concrete_type_name_kind, andvalidate_type_nameswere refactored to reuse these optimized lookup helpers.Performance Impact (Average of 25 runs)
This optimization brings a substantial speedup compared to both the unpatched main branch and the first shape-lookup optimization:
Proposed Patch (Example)
The fast-character pre-filter was applied to 10 lookup functions. Below is an example of the optimization applied to
find_shape:static const Shape *find_shape(const Program *program, const char *name) { + if (!name || !name[0]) return NULL; for (size_t i = 0; i < program->shapes.len; i++) { - if (strcmp(program->shapes.items[i].name, name) == 0) return &program->shapes.items[i]; + if (program->shapes.items[i].name[0] == name[0] && strcmp(program->shapes.items[i].name, name) == 0) return &program->shapes.items[i]; } return NULL; }Redundant loops in
function_exists,has_case,visible_concrete_type_name_kind, andvalidate_type_nameswere also refactored to directly reuse these optimized functions.Suggested Coordination
This optimization has been committed to the local branch
perf-lookup-strcmpand pushed to the fork repo. It has been verified to compile without warnings and pass all conformance tests.Cheers,
jeremyHOT