perf(zero-c): optimize generic shape lookup in type checker#152
Open
jeremyHOT wants to merge 1 commit into
Open
perf(zero-c): optimize generic shape lookup in type checker#152jeremyHOT 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 a hot-path performance bottleneck in the generic shape lookup function. Here are the details and the patch to resolve it.
Performance Advisory: Redundant Heap Allocations and Double Traversals in Generic Shape Lookup
Summary
A performance bottleneck was identified in the Zero type checker's generic shape lookup function (
find_shape_for_type). The function repeatedly invoked dynamic string parser calls (type_generic_arg_list) containing heap allocations for candidate shapes that did not match the requested type, resulting in significant heap allocation overhead and redundant string traversals during compilation.Optimization Details
native/zero-c/src/checker.cTechnical Analysis
The original
find_shape_for_typefunction iterates over all candidate shapes in the program and callstype_generic_arg_listto test for a match:For non-matching candidate shapes,
type_generic_arg_listproceeds to dynamically allocate memory (z_checked_callocandz_strndup) to parse argument lists before immediately freeing them viafree_type_arg_list. Furthermore, the matching checked the string lengths and prefixes multiple times.Performance Impact (Average of 25 runs)
By adding a zero-allocation pre-filter, the compiler avoids hundreds of heap allocations and double traversals per compilation:
Proposed Patch
We replace the naive loop checks with a zero-allocation prefix match and length verification:
Suggested Coordination
This optimization has been fully committed to the branch
perf-shape-lookupand verified to compile cleanly with strict pedantic flags and pass all CLI diagnostics tests. Let me know if you need any clarification or further performance runs.Cheers,
jeremyHOT