Repository navigation
c_import is linear in a header's declarations: emitted names are a hash set, decl locations an index - #1386
Merged
Conversation
This was referenced Sep 23, 2026
…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)
ehartford
force-pushed
the
cimport-emitted-name-set
branch
from
September 23, 2026 02:35
816b15f to
fdb7c49
Compare
Contributor
Author
ehartford
marked this pull request as ready for review
September 23, 2026 02:35
ehartford
pushed a commit
that referenced
this pull request
Sep 23, 2026
…he first declaration that uses it; one nothing uses is not emitted (#1396) Root cause: ci_translate_struct took with_cimport_decl_name, which for a file-scope record with no tag and no typedef name is libclang's cursor spelling `struct (unnamed at file:line:col)`, and emitted it as a type name; the bindings failed to parse. Every use of such a record translated as `c_void` (ClangBridge translate_type_recursive_mode's Record branch), so `struct { int b; } g;` also imported as `extern var g: c_void`, an array of it collapsed to `c_void`, and a function returning one was omitted. ClangBridge now names each file-scope record with no name after the first declaration, in translation-unit order, whose type reaches it: `g_anon` (variable, also through arrays/pointers), `f_return_anon` / `f_p_anon` (`f_argN_anon` unnamed), `T_anon` (typedef through a pointer/array); a name another declaration already spells takes the first free `_2` suffix. The cursor spelling cache and type translation both answer with it, so the record, `extern var g: g_anon`, `[4]g3_anon` and `fn mk() -> mk_return_anon` agree. Names depend only on declaration order and spellings: deterministic. A record nothing uses — winnt.h's C_ASSERT(TYPE_ALIGNMENT(T)) builds one inside an expression — has no spelling in C or With and is not emitted (with_cimport_decl_is_unused_anon_record), as the issue prescribes. Two neighbours winnt.h/wtypes.h reach next, same root (records libclang lists only under another record): - A member record with no name held through an array or pointer (`struct { ... } ScopeRecord[1];`, SCOPE_TABLE_AMD64) translated as `c_void`, i.e. an opaque by-value field. The bridge names it by CImport's member rule, `Parent_field`, CImport emits it, and an array of a demoted one demotes its holder. The flexible-array accessor rename replaced every occurrence of the field's spelling in the last field, including the new type name; it now renames the field only. - A tag declared inside a record (`union __MIDL_IWinTypes_0009 {...} u;`, and `enum { A, B } k;` constants) is file scope in C but was never collected: the field named an unknown type. hoist_nested_tags lists each just before the top-level record holding it. sqlite3.h's sqlite3_index_constraint/_orderby/_constraint_usage import with fields now instead of opaque. Lookups stay linear in the header (#1386): a record finds its name through a hash on its Decl, and a candidate name is checked against a hash set of the declarations' spellings built once (emitted_name_slot), never a scan. Shapes (darwin, stage1): `struct {..} g;`, `static struct {..} g;`, `extern struct {..} g[4];`, `struct {..} a, b;` (one type), union, anonymous enum (constants), anonymous-in-anonymous members, function result and prototype parameter, pointer typedef, pointer var, name collision, const initializer, and the C_ASSERT expression record (not emitted). Tests: behav_c_import_anon_file_scope_record.w (layouts, literals, const initializer) and _uses.w (check-only: every declaration's type identity).
ehartford
pushed a commit
that referenced
this pull request
Sep 23, 2026
…he first declaration that uses it; one nothing uses is not emitted (#1396) Root cause: ci_translate_struct took with_cimport_decl_name, which for a file-scope record with no tag and no typedef name is libclang's cursor spelling `struct (unnamed at file:line:col)`, and emitted it as a type name; the bindings failed to parse. Every use of such a record translated as `c_void` (ClangBridge translate_type_recursive_mode's Record branch), so `struct { int b; } g;` also imported as `extern var g: c_void`, an array of it collapsed to `c_void`, and a function returning one was omitted. ClangBridge now names each file-scope record with no name after the first declaration, in translation-unit order, whose type reaches it: `g_anon` (variable, also through arrays/pointers), `f_return_anon` / `f_p_anon` (`f_argN_anon` unnamed), `T_anon` (typedef through a pointer/array); a name another declaration already spells takes the first free `_2` suffix. The cursor spelling cache and type translation both answer with it, so the record, `extern var g: g_anon`, `[4]g3_anon` and `fn mk() -> mk_return_anon` agree. Names depend only on declaration order and spellings: deterministic. A record nothing uses — winnt.h's C_ASSERT(TYPE_ALIGNMENT(T)) builds one inside an expression — has no spelling in C or With and is not emitted (with_cimport_decl_is_unused_anon_record), as the issue prescribes. Two neighbours winnt.h/wtypes.h reach next, same root (records libclang lists only under another record): - A member record with no name held through an array or pointer (`struct { ... } ScopeRecord[1];`, SCOPE_TABLE_AMD64) translated as `c_void`, i.e. an opaque by-value field. The bridge names it by CImport's member rule, `Parent_field`, CImport emits it, and an array of a demoted one demotes its holder. The flexible-array accessor rename replaced every occurrence of the field's spelling in the last field, including the new type name; it now renames the field only. - A tag declared inside a record (`union __MIDL_IWinTypes_0009 {...} u;`, and `enum { A, B } k;` constants) is file scope in C but was never collected: the field named an unknown type. hoist_nested_tags lists each just before the top-level record holding it. sqlite3.h's sqlite3_index_constraint/_orderby/_constraint_usage import with fields now instead of opaque. Lookups stay linear in the header (#1386): a record finds its name through a hash on its Decl, and a candidate name is checked against a hash set of the declarations' spellings built once (emitted_name_slot), never a scan. Shapes (darwin, stage1): `struct {..} g;`, `static struct {..} g;`, `extern struct {..} g[4];`, `struct {..} a, b;` (one type), union, anonymous enum (constants), anonymous-in-anonymous members, function result and prototype parameter, pointer typedef, pointer var, name collision, const initializer, and the C_ASSERT expression record (not emitted). Tests: behav_c_import_anon_file_scope_record.w (layouts, literals, const initializer) and _uses.w (check-only: every declaration's type identity).
ehartford
pushed a commit
that referenced
this pull request
Sep 23, 2026
…he first declaration that uses it; one nothing uses is not emitted (#1396) Root cause: ci_translate_struct took with_cimport_decl_name, which for a file-scope record with no tag and no typedef name is libclang's cursor spelling `struct (unnamed at file:line:col)`, and emitted it as a type name; the bindings failed to parse. Every use of such a record translated as `c_void` (ClangBridge translate_type_recursive_mode's Record branch), so `struct { int b; } g;` also imported as `extern var g: c_void`, an array of it collapsed to `c_void`, and a function returning one was omitted. ClangBridge now names each file-scope record with no name after the first declaration, in translation-unit order, whose type reaches it: `g_anon` (variable, also through arrays/pointers), `f_return_anon` / `f_p_anon` (`f_argN_anon` unnamed), `T_anon` (typedef through a pointer/array); a name another declaration already spells takes the first free `_2` suffix. The cursor spelling cache and type translation both answer with it, so the record, `extern var g: g_anon`, `[4]g3_anon` and `fn mk() -> mk_return_anon` agree. Names depend only on declaration order and spellings: deterministic. A record nothing uses — winnt.h's C_ASSERT(TYPE_ALIGNMENT(T)) builds one inside an expression — has no spelling in C or With and is not emitted (with_cimport_decl_is_unused_anon_record), as the issue prescribes. Two neighbours winnt.h/wtypes.h reach next, same root (records libclang lists only under another record): - A member record with no name held through an array or pointer (`struct { ... } ScopeRecord[1];`, SCOPE_TABLE_AMD64) translated as `c_void`, i.e. an opaque by-value field. The bridge names it by CImport's member rule, `Parent_field`, CImport emits it, and an array of a demoted one demotes its holder. The flexible-array accessor rename replaced every occurrence of the field's spelling in the last field, including the new type name; it now renames the field only. - A tag declared inside a record (`union __MIDL_IWinTypes_0009 {...} u;`, and `enum { A, B } k;` constants) is file scope in C but was never collected: the field named an unknown type. hoist_nested_tags lists each just before the top-level record holding it. sqlite3.h's sqlite3_index_constraint/_orderby/_constraint_usage import with fields now instead of opaque. Lookups stay linear in the header (#1386): a record finds its name through a hash on its Decl, and a candidate name is checked against a hash set of the declarations' spellings built once (emitted_name_slot), never a scan. Shapes (darwin, stage1): `struct {..} g;`, `static struct {..} g;`, `extern struct {..} g[4];`, `struct {..} a, b;` (one type), union, anonymous enum (constants), anonymous-in-anonymous members, function result and prototype parameter, pointer typedef, pointer var, name collision, const initializer, and the C_ASSERT expression record (not emitted). Tests: behav_c_import_anon_file_scope_record.w (layouts, literals, const initializer) and _uses.w (check-only: every declaration's type identity).
ehartford
pushed a commit
that referenced
this pull request
Sep 23, 2026
…he first declaration that uses it; one nothing uses is not emitted (#1396) Root cause: ci_translate_struct took with_cimport_decl_name, which for a file-scope record with no tag and no typedef name is libclang's cursor spelling `struct (unnamed at file:line:col)`, and emitted it as a type name; the bindings failed to parse. Every use of such a record translated as `c_void` (ClangBridge translate_type_recursive_mode's Record branch), so `struct { int b; } g;` also imported as `extern var g: c_void`, an array of it collapsed to `c_void`, and a function returning one was omitted. ClangBridge now names each file-scope record with no name after the first declaration, in translation-unit order, whose type reaches it: `g_anon` (variable, also through arrays/pointers), `f_return_anon` / `f_p_anon` (`f_argN_anon` unnamed), `T_anon` (typedef through a pointer/array); a name another declaration already spells takes the first free `_2` suffix. The cursor spelling cache and type translation both answer with it, so the record, `extern var g: g_anon`, `[4]g3_anon` and `fn mk() -> mk_return_anon` agree. Names depend only on declaration order and spellings: deterministic. A record nothing uses — winnt.h's C_ASSERT(TYPE_ALIGNMENT(T)) builds one inside an expression — has no spelling in C or With and is not emitted (with_cimport_decl_is_unused_anon_record), as the issue prescribes. Two neighbours winnt.h/wtypes.h reach next, same root (records libclang lists only under another record): - A member record with no name held through an array or pointer (`struct { ... } ScopeRecord[1];`, SCOPE_TABLE_AMD64) translated as `c_void`, i.e. an opaque by-value field. The bridge names it by CImport's member rule, `Parent_field`, CImport emits it, and an array of a demoted one demotes its holder. The flexible-array accessor rename replaced every occurrence of the field's spelling in the last field, including the new type name; it now renames the field only. - A tag declared inside a record (`union __MIDL_IWinTypes_0009 {...} u;`, and `enum { A, B } k;` constants) is file scope in C but was never collected: the field named an unknown type. hoist_nested_tags lists each just before the top-level record holding it. sqlite3.h's sqlite3_index_constraint/_orderby/_constraint_usage import with fields now instead of opaque. Lookups stay linear in the header (#1386): a record finds its name through a hash on its Decl, and a candidate name is checked against a hash set of the declarations' spellings built once (emitted_name_slot), never a scan. Shapes (darwin, stage1): `struct {..} g;`, `static struct {..} g;`, `extern struct {..} g[4];`, `struct {..} a, b;` (one type), union, anonymous enum (constants), anonymous-in-anonymous members, function result and prototype parameter, pointer typedef, pointer var, name collision, const initializer, and the C_ASSERT expression record (not emitted). Tests: behav_c_import_anon_file_scope_record.w (layouts, literals, const initializer) and _uses.w (check-only: every declaration's type identity).
ehartford
pushed a commit
that referenced
this pull request
Sep 23, 2026
…he first declaration that uses it; one nothing uses is not emitted (#1396) Root cause: ci_translate_struct took with_cimport_decl_name, which for a file-scope record with no tag and no typedef name is libclang's cursor spelling `struct (unnamed at file:line:col)`, and emitted it as a type name; the bindings failed to parse. Every use of such a record translated as `c_void` (ClangBridge translate_type_recursive_mode's Record branch), so `struct { int b; } g;` also imported as `extern var g: c_void`, an array of it collapsed to `c_void`, and a function returning one was omitted. ClangBridge now names each file-scope record with no name after the first declaration, in translation-unit order, whose type reaches it: `g_anon` (variable, also through arrays/pointers), `f_return_anon` / `f_p_anon` (`f_argN_anon` unnamed), `T_anon` (typedef through a pointer/array); a name another declaration already spells takes the first free `_2` suffix. The cursor spelling cache and type translation both answer with it, so the record, `extern var g: g_anon`, `[4]g3_anon` and `fn mk() -> mk_return_anon` agree. Names depend only on declaration order and spellings: deterministic. A record nothing uses — winnt.h's C_ASSERT(TYPE_ALIGNMENT(T)) builds one inside an expression — has no spelling in C or With and is not emitted (with_cimport_decl_is_unused_anon_record), as the issue prescribes. Two neighbours winnt.h/wtypes.h reach next, same root (records libclang lists only under another record): - A member record with no name held through an array or pointer (`struct { ... } ScopeRecord[1];`, SCOPE_TABLE_AMD64) translated as `c_void`, i.e. an opaque by-value field. The bridge names it by CImport's member rule, `Parent_field`, CImport emits it, and an array of a demoted one demotes its holder. The flexible-array accessor rename replaced every occurrence of the field's spelling in the last field, including the new type name; it now renames the field only. - A tag declared inside a record (`union __MIDL_IWinTypes_0009 {...} u;`, and `enum { A, B } k;` constants) is file scope in C but was never collected: the field named an unknown type. hoist_nested_tags lists each just before the top-level record holding it. sqlite3.h's sqlite3_index_constraint/_orderby/_constraint_usage import with fields now instead of opaque. Lookups stay linear in the header (#1386): a record finds its name through a hash on its Decl, and a candidate name is checked against a hash set of the declarations' spellings built once (emitted_name_slot), never a scan. Shapes (darwin, stage1): `struct {..} g;`, `static struct {..} g;`, `extern struct {..} g[4];`, `struct {..} a, b;` (one type), union, anonymous enum (constants), anonymous-in-anonymous members, function result and prototype parameter, pointer typedef, pointer var, name collision, const initializer, and the C_ASSERT expression record (not emitted). Tests: behav_c_import_anon_file_scope_record.w (layouts, literals, const initializer) and _uses.w (check-only: every declaration's type identity).
ehartford
added a commit
that referenced
this pull request
Sep 23, 2026
…first user (#1396); pointer type args in sizeof; nested records; unspellable layouts opaque (#1419) * Parser: sizeof[*mut T]() / transmute[*const T](x) — a raw pointer type argument in expression position parses as a type (§16.12) `*` followed by `mut` or `const` never dereferences anything, so inside `name[...]` it begins a type, like the `fn`/`extern` function-pointer case already beside it. c_import emits `sizeof[*mut c_void]()` for C's `sizeof(PVOID)`; winnt.h's TOKEN_INTEGRITY_LEVEL_MAX_SIZE failed with "expected expression" at `mut` once #1396's unnamed records parsed. Test: behav_sizeof_pointer_type_arg.w. * c_import: a record with no tag and no typedef name gets the name of the first declaration that uses it; one nothing uses is not emitted (#1396) Root cause: ci_translate_struct took with_cimport_decl_name, which for a file-scope record with no tag and no typedef name is libclang's cursor spelling `struct (unnamed at file:line:col)`, and emitted it as a type name; the bindings failed to parse. Every use of such a record translated as `c_void` (ClangBridge translate_type_recursive_mode's Record branch), so `struct { int b; } g;` also imported as `extern var g: c_void`, an array of it collapsed to `c_void`, and a function returning one was omitted. ClangBridge now names each file-scope record with no name after the first declaration, in translation-unit order, whose type reaches it: `g_anon` (variable, also through arrays/pointers), `f_return_anon` / `f_p_anon` (`f_argN_anon` unnamed), `T_anon` (typedef through a pointer/array); a name another declaration already spells takes the first free `_2` suffix. The cursor spelling cache and type translation both answer with it, so the record, `extern var g: g_anon`, `[4]g3_anon` and `fn mk() -> mk_return_anon` agree. Names depend only on declaration order and spellings: deterministic. A record nothing uses — winnt.h's C_ASSERT(TYPE_ALIGNMENT(T)) builds one inside an expression — has no spelling in C or With and is not emitted (with_cimport_decl_is_unused_anon_record), as the issue prescribes. Two neighbours winnt.h/wtypes.h reach next, same root (records libclang lists only under another record): - A member record with no name held through an array or pointer (`struct { ... } ScopeRecord[1];`, SCOPE_TABLE_AMD64) translated as `c_void`, i.e. an opaque by-value field. The bridge names it by CImport's member rule, `Parent_field`, CImport emits it, and an array of a demoted one demotes its holder. The flexible-array accessor rename replaced every occurrence of the field's spelling in the last field, including the new type name; it now renames the field only. - A tag declared inside a record (`union __MIDL_IWinTypes_0009 {...} u;`, and `enum { A, B } k;` constants) is file scope in C but was never collected: the field named an unknown type. hoist_nested_tags lists each just before the top-level record holding it. sqlite3.h's sqlite3_index_constraint/_orderby/_constraint_usage import with fields now instead of opaque. Lookups stay linear in the header (#1386): a record finds its name through a hash on its Decl, and a candidate name is checked against a hash set of the declarations' spellings built once (emitted_name_slot), never a scan. Shapes (darwin, stage1): `struct {..} g;`, `static struct {..} g;`, `extern struct {..} g[4];`, `struct {..} a, b;` (one type), union, anonymous enum (constants), anonymous-in-anonymous members, function result and prototype parameter, pointer typedef, pointer var, name collision, const initializer, and the C_ASSERT expression record (not emitted). Tests: behav_c_import_anon_file_scope_record.w (layouts, literals, const initializer) and _uses.w (check-only: every declaration's type identity). * c_import: layouts With cannot spell import opaque — `_`-tagged records with bitfields, bitfields in member records, fields under their natural alignment (#1396) Three holes in the demotion pre-scan, each an emitted record that either failed to compile or compiled with the wrong layout: - ci_collect_demoted_types skipped every tag with a leading underscore, though ci_translate_struct emits them: winbase.h's `_DCB` bitfields came out as whole DWORD fields with `@[align(1)]` ("alignment is less than natural"), and darwin's `union wait` as four 4-byte fields. - ci_record_decl_directly_demoted_cursor (member records) never looked for bitfields (§16.9: a record with bitfields is opaque). - A field below its natural alignment (`#pragma pack(2)`, wingdi.h's BITMAPFILEHEADER) was emitted `@[align(2)]`; §16.4 rule 2 forbids an alignment under the natural one, and `@[packed]` would drop the record's own alignment to 1. It imports opaque, usable by pointer, like a bitfield layout. An exact spelling needs a packed(N) layout the spec does not have. Windows 10: the 146 "alignment is less than natural" errors in the windows.h closure are gone. Darwin headers change only where they were wrong: `union wait` is opaque; forward-declared `_` records gain the "demoted to opaque" location comment. Test: behav_c_import_nested_records.w (also covers the array/pointer member records and nested tags of the previous commit). --------- Co-authored-by: Eric Hartford <[email protected]>
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.
Two lookups scanned every entry on every query, so c_import cost grew with
the square of a header's declarations:
is_name_emittedwalked the list of emitted names withc_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.
ci_get_decl_locationwalked every top-level cursor, making aspelling 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 c-migrate: compiler-C migration exceeds the 64 GiB memory cap #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
sampleprofile at 6000 declarations had c_strcmp insideis_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.
Draft until the seed-driven battery is green (D56). This is a performance change, so
:drop-auditis not needed. Iterate tier so far:build :devrc=0, and the 40 c_import behavior fixtures give the same results on stage1 with and without the change (3 fail on both, the stage1-only class).