Skip to content

Optimize boolean fields in JSFunctionDef, JSParseState and JSToken#1232

Merged
bnoordhuis merged 5 commits intoquickjs-ng:masterfrom
speedskater1610:patch-1
Nov 9, 2025
Merged

Optimize boolean fields in JSFunctionDef, JSParseState and JSToken#1232
bnoordhuis merged 5 commits intoquickjs-ng:masterfrom
speedskater1610:patch-1

Conversation

@speedskater1610
Copy link
Copy Markdown
Contributor

Changed multiple boolean fields in JSFunctionDef and JSParseState structures from 'bool' to '_Bool flag : 1' to optimize memory usage while maintaining readability. This is realted to issue #1125.

Changed multiple boolean fields in JSFunctionDef and JSParseState structures from 'bool' to '_Bool flag : 1' to optimize memory usage while maintaining readability.
…l flag` to fit with my prior PR:

JSRuntime, JSMapRecord, JSMapState, JSForInIterator, JSTypedArray, JSAsyncFunctionState, JSAsyncFunctionData, JSModuleDef, JSCallSiteData, JSParsePos, ClassFieldsDef, BCWriterState, ClassFieldsDef,  BCReaderState, JSIteratorConcatData, JSRegExpStringIteratorData, JSPromiseData, JSPromiseFunctionDataResolved, JSAtomicsWaiter,
@akrieger
Copy link
Copy Markdown
Contributor

akrieger commented Nov 7, 2025

bool is just a macro for _Bool until C23 after which _Bool is deprecated and bool is the only correct option, so feels like just sticking with bool is appropriate.

@speedskater1610
Copy link
Copy Markdown
Contributor Author

Sorry, I didn't realize; I’ve updated the fields to use bool flag : 1 instead. The intent was mainly to reduce the memory footprint of these structs, since they contain many flags, and bit-field width still provides a measurable space optimization even when keeping bool.

@bnoordhuis
Copy link
Copy Markdown
Contributor

Changing a field to a bool : 1 is only useful if there's an adjacent field to merge with.

Phrased another way, something like struct S { int x; bool b:1; }; is pointless and potentially a deoptimization:

  • pointless because the struct size remains the same (even on ppc, I suspect, because of alignment requirements), and
  • a deoptimization if the compiler still emits extra instructions when accessing the field.

That's why I mentioned reordering fields in the linked issue. There are probably also plenty of opportunities to reduce the bit size of adjacent fields; in lots of places we store things like lengths that fit in 31 bits as uint32s. Such changes need to be carefully considered and reviewed though.

I suggest updating this PR to only cover the lowest-hanging fruit like JSFunctionDef, and then follow up with a series of pull requests where you pick off the more complicated ones one by one.

…s. Reverted previous `bool : 1` changes in other structs; only JSFunctionDef is optimized in this PR. This reduces the memory footprint for JSFunctionDef.
@speedskater1610
Copy link
Copy Markdown
Contributor Author

In this commit, all previous bool : 1 changes in other structs were reverted. Only JSFunctionDef was optimized: all boolean flags are now grouped as 1-bit bitfields to reduce struct size and avoid padding.

@bnoordhuis bnoordhuis merged commit ca0d50d into quickjs-ng:master Nov 9, 2025
127 checks passed
@speedskater1610 speedskater1610 deleted the patch-1 branch November 9, 2025 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants