Repository navigation
c_import: a DEFINE_GUID var no longer feeds winnt.h to the C expression parser; parser bounded; OOM is loud (#1387) - #1399
Merged
Conversation
added 5 commits
September 22, 2026 21:54
…sh set, decl locations an index Two lookups scanned every entry on every query, so c_import cost grew with the square of a header's declarations: - ClangBridge `is_name_emitted` walked the list of emitted names with c_strcmp on every lookup and every mark. It is now an open-addressing hash set. ClangBridge is compiled without the prelude, so it cannot use std HashMap. - CImport `ci_get_decl_location` walked every top-level cursor, making a spelling string for each, once per translated struct. It is now a name -> first cursor index, rebuilt when the session or parse generation changes and cleared at migration teardown, the same pattern as the #744 indexes. This is why behav_c_import_scales_linearly.w failed on the darwin, linux-aarch64 and windows-x86_64 nightly legs (runs 35512407331 and 35616828206), and it is a cause of the bzip2/libcurl UAT timeouts over windows.h on Windows. The installed compiler measured 0.72s / 5.88s / 23.3s at 750 / 3000 / 6000 declarations (8.2x for 4x the input); the test allows 8x+1s, so a fast Mac passes and a CI runner does not. Evidence: a `sample` profile at 6000 declarations had c_strcmp inside is_name_emitted on top. Once that was fixed, lldb stack samples put 10 of 14 samples in ci_translate_struct -> ci_get_decl_location. A/B of seed-built stage1 against the base commit, user CPU with fresh headers and about 3.25s of fixed startup: 3000 declarations went from 15.0-17.3s to 8.4-11.0s, 750 declarations from 5.1-7.1s to 5.0-6.5s. The 40 c_import behavior fixtures run the same on both. (cherry picked from commit 816b15f)
…g parsed one for; mixed macro spelling ranges and `>=` are not initializers (#1387) A DEFINE_GUID var (`EXTERN_C const GUID name`) has no initializer, yet ci_var_init_expr_for_type fell through to text mining whenever libclang returned no initializer cursor (CImport.w:14709-14710). The text came from cursor_spelling_text_from_cursor, whose spelling range starts at `extern` in the macro body and ends at `name` in the invocation: both in winnt.h, so it returned the 15.6k lines between them (ClangBridge.w:3883, the shape source_range_preferred_text_offsets already refused). ci_extract_var_initializer_text then took the `=` of `#if _MSC_VER >= 1200` as the declarator's (CImport.w:13988), and 373 KB of header went to the C expression parser: 29 GB and exit 99 on Windows. - ci_var_init_expr_from_decl_source_for_type reads the declaration's text only when clang_Cursor_getVarDeclInitializer has an initializer; otherwise only migrate's raw-source lookup by name remains. - source_range_spelling_is_mixed is the one rule for a body+argument range; the preferred-text and spelling-text readers both refuse it. - `==`, `!=`, `<=`, `>=` and compound assignments are not the declarator `=`. Observed in lldb on darwin (installed v0.15.2.1): ci_translate_c_initializer_for_cursor_type called from ci_var_init_expr_from_decl_source_for_type <- ci_var_init_expr_for_type+1396 with a 7863-byte init_src starting "0\n#endif\n#define M0(a) ..." for the 150-line repro. Issue repro, frontend.c_import: 800 filler lines 119.6 s -> 0.31 s; 1600 lines 0.31 s. (cherry picked from commit 36c79a8)
…ression; past it the text is reported untranslated (#1387) Each ci_parse_* level splits at its operator and, when the split fails, hands the whole text to the next level (`(T)-1` parses because add_expr's failed split falls through to cast_expr). On text that is not an expression the work multiplies across the split points: a 4.6 KB macro body of `M{i}(a) ((a) * {i} + 1) - ((a) << 2) * 3 / 4 >= ` fragments took 3.9 s at 100 fragments, 104 s at 200 and did not finish in 300 s at 400 (installed v0.15.2.1, frontend.c_import). With the budget: 0.47 s, 0.47 s, 0.59 s, and 1.7 s at 3000 fragments (148 KB), each with a warning naming the expression's size. Well-formed expressions scan a few times their length and never approach 64 MiB. (cherry picked from commit 4e0cc66)
…fore exit 99 (#1387) The allocator's four refusal paths (system malloc, large mmap, slab mmap, range-table growth) called rt_exit(99) with nothing on stderr, so the Windows bzip2/libcurl UATs that grew to 29 GB left empty logs. They now write `with: out of memory: requested=N committed=M bytes` to fd 2 with the allocation-free dbg_puts/dbg_put_i64 path the memory-limit report already uses, then exit 99 as before. (cherry picked from commit 023cf0c)
…ot a scan of the pipe-joined struct list (#1387) With the var-extent fix, c_import of bzlib.h on Windows finishes, but still takes 19.6 s (curl.h 22.1 s). Five of six cdb stack samples of the uncached check sit in ci_str_matches_at <- ci_str_contains <- ci_detect_member_functions: for every function, whose first parameter or return type names a struct, `ci_str_contains(known_structs, "|S|")` scans the pipe-joined list of every translated struct (thousands under windows.h), then a linear walk finds the struct's snake prefix, and emitted_methods grows by `++` and is scanned the same way. The struct list is parsed once into a name -> snake-prefix map (first entry wins, as the linear walk did), emitted methods are a set, and the output is a StringBuilder. Generated bindings are byte-identical to origin/main's stage1 for stdio.h, stdlib.h, string.h, time.h, pthread.h, sys/socket.h, zlib.h, sqlite3.h and curses.h (sqlite3.h: 162 method lines). (cherry picked from commit 041b325)
ehartford
force-pushed
the
fix-1387-cimport-var-extent
branch
from
September 23, 2026 02:35
041b325 to
540b26e
Compare
Contributor
Author
ehartford
marked this pull request as ready for review
September 23, 2026 02:35
This was referenced Sep 23, 2026
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.
Fixes #1387.
Root cause (exact lines, origin/main)
src/CImport.w:14709-14710—ci_var_init_expr_for_typefell through to text mining (ci_var_init_expr_from_decl_source_for_type) whenclang_Cursor_getVarDeclInitializerreturned null, i.e. for a var clang says has no initializer (everyDEFINE_GUID).src/compiler/ClangBridge.w:3883—cursor_spelling_text_from_cursoraccepted a spelling range that starts in the macro body (externon the#defineline) and ends in the invocation argument (name): both in winnt.h, so it returned the ~15.6k lines between them.source_range_preferred_text_offsetsalready refused this shape; the spelling reader did not.src/CImport.w:13988—ci_extract_var_initializer_texttook the=of>=as the declarator's.ci_parse_*_exprhands the whole text to the next level when a split fails, so non-expression text costs grow with the product of split points.Observed in lldb on darwin with the installed v0.15.2.1:
ci_translate_c_initializer_for_cursor_type<-ci_var_init_expr_from_decl_source_for_type<-ci_var_init_expr_for_type+1396with a 7,863-byte init_src starting0\n#endif\n#define M0(a) ...(150-line repro).Commits
source_range_spelling_is_mixedis the single body+argument rule used by both text readers;==/!=/<=/>=/compound ops are not the declarator=. Test:behav_c_import_macro_var_extent.w(ratio 200 vs 800 filler lines; macro-built and comparison-holding initializers keep their values).behav_c_import_expr_scan_budget.w.with: out of memory: requested=N committed=M bytesto fd 2 (allocation-free) before exit 99. Verified by forcingrt_mmapto return 0 in lldb:with: out of memory: requested=65536 committed=0 bytes, exit 99. No compile phase in the message: the runtime doesn't know phases, and adding a hook from the compiler would need a new runtime export plus a seed bump.ci_detect_member_functionsuses hash maps for the struct list and for emitted methods (5 of 6 cdb samples of the fixed Windows c_import were in itsci_str_containsscan). Thetranslated_structsstring itself still gets passed asknown_structs: &strto about 15 functions. Changing it to a set would touch all of them, so I left it. With the member-function scan gone,is_name_emittedis now the top hot spot, and c_import is linear in a header's declarations: emitted names are a hash set, decl locations an index #1386 fixes that.Measurements
Darwin (
frontend.c_import), issue repro with one GUID var:Darwin, bogus 4.6–148 KB macro body (budget commit): 100 fragments 3.9 s → 0.47 s; 200 fragments 104 s → 0.47 s; 400 fragments >300 s → 0.59 s; 3000 fragments → 1.7 s, 700 MB, warning.
Windows 10 x64 (win10, 32 GB),
with check src/main.win the bzip2 UAT project, uncached:with v0.15.2.1-g7d6082598)No budget warnings fire on the real windows.h closure.
The bzip2/libcurl UATs still fail on Windows. c_import now completes, but the bindings it generates don't parse. winnt.h's
C_ASSERT(TYPE_ALIGNMENT(LARGE_INTEGER) == 8)declares an untagged struct inside an expression at file scope, and c_import emits it astype struct (unnamed at ...winnt.h:2602:1) {...}. This has a different root cause and reproduces on darwin withstruct { int b; } g;. Filed as #1396.Evidence it changes nothing else
Iterate tier only (
:dev,:stage2, targeted tests). The seed-driven battery has not run, so this stays a draft.