Refactor std.* call signature table in checker#133
Open
kylejryan wants to merge 1 commit into
Open
Conversation
|
@kylejryan is attempting to deploy a commit to the Vercel Labs Team on Vercel. A member of the Team first needs to authorize it. |
Collapse the three parallel strcmp(...) chains in checker.c (std_call_return_type, std_call_arg_count, std_call_arg_type) plus std_http_error_code into a single sorted table keyed by name, with a bsearch lookup. The signature for each std.* helper now lives in one place (native/zero-c/src/std_calls.inc) and the arity is fixed structurally by the STD_CALL_<N> macro it is declared with, so arg_count cannot drift from the populated argument list. A debug-only constructor (check_std_sigs_sorted) asserts the table remains sorted at startup; release builds pay nothing for it. Behavior is preserved: all 182 example check outputs match byte-for-byte against pre-refactor; pnpm native:smoke, reliability:smoke, native:sanitize (ASan+UBSan), and test:zero (10 CLI tests) all pass. Net diff: -437 / +228 lines. Per-typecheck lookup drops from ~130 strcmps to ~7 (log2 of the table).
70b08c3 to
ea29413
Compare
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.
Summary
Collapses the three parallel
strcmp(...)chains innative/zero-c/src/checker.c—std_call_return_type,std_call_arg_count,std_call_arg_type— plus the smallstd_http_error_codehelper into a single sorted lookup table over a newStdSigrecord, looked up viabsearch. The per-helper signature now lives in exactly one place (native/zero-c/src/std_calls.inc).Why
The old shape stated each
std.*helper's signature three times across three functions, in three (manually maintained) orderings. Updating one and forgetting another was a silent typechecker bug. Each typecheck of astd.*call also paid for up to three full linearstrcmpchains (~130 entries each) plus two heap-allocatedZBufs for the dotted name.What changes
native/zero-c/src/std_calls.inc— new file. One row per helper, declared withSTD_CALL_<N>(name, return_type, args...). The arity is structurally fixed by the macro name, soarg_countcannot drift from the populated argument list.NULLis used for argument slots whose type is not enforced at the table level (matching prior behavior forstd.fs.readAll,std.json.parse{,Bytes},std.mem.{allocBytes,byteBuf,get,eqlBytes}).native/zero-c/src/checker.c:StdSigtable, astd_call_lookupbinary search, and three thin accessor functions preserving the existing call-site signatures (modulostd_call_return_typeswitching fromconst Expr *toconst char *for consistency with the other two accessors — the two call sites are updated).__attribute__((constructor))(check_std_sigs_sorted) that asserts the table is sorted at startup. Release builds (NDEBUG) skip it entirely.std.http.error*constants (errorNone, errorConnect, …) explicitly in the table; they were previously implicit viastd_http_error_codefall-throughs in two of the functions.ir.ckeeps its own independent copy ofir_std_http_error_codeand is untouched.Net diff: +228 / -437 lines.
Performance / space
ZBufallocations at the hot call site.textfor the lookup tables.rodataThe Makefile already picks up
src/*.incvia itsINCLUDESglob; no build changes required.Test plan
make -C native/zero-c— release build clean with-Wall -Wextra -Wpedantic -Os -std=c11zero checkagainst all 182.0files underexamples/— zero diff vs. pre-refactorpnpm run native:smoke— passpnpm run reliability:smoke— passbash scripts/sanitizer-smoke.sh— ASan + UBSan cleanpnpm run test:zero— 10/10 CLI tests passNotes / follow-ups (not in this PR)
checker.cto do a singlestd_call_lookupper typecheck instead of up to three, but that touches the largeEXPR_CALLblock and was kept out of scope here.ir.c'sir_std_http_error_codecould be deduplicated against the same table, but it's a separate module and out of scope.