Skip to content

Keep array parts that hold only numbers and booleans out of the GC's sight - #58

Draft
folbricht wants to merge 1 commit into
mainfrom
scalar-arrays
Draft

folbricht wants to merge 1 commit into
mainfrom
scalar-arrays

Conversation

@folbricht

@folbricht folbricht commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

Option A from the Value redesign. It is now based on main: it does not need #57.

What it does

While a table's array part holds only nil, booleans and numbers, it is allocated as scalarSlots: a Value's layout with a uintptr where the pointer was. The GC doesn't scan it, and the code reads it as []Value as before.

  • Table.ptrArr records that an array may hold pointers. Before the first one is stored, arrToPtrs moves the array to memory the GC scans.
  • A resize picks the kind from the new array's contents, so an array that no longer holds pointers stops being scanned.
  • In the loops, SETTABLE needs the check only when the slot's pointer word changes, or for an append. A value with the slot's own pointer word can't be the array's first pointer.
  • NEWTABLE's unused C operand carries a compiler hint, set when every positional item is a number, boolean or nil literal, or an arithmetic, comparison or unary expression. Without it, {left, right} allocates its array twice.
  • The public NewTable keeps allocating arrays that can hold pointers.

Safety

A pointer in an unscanned array would let the GC free an object that the table still refers to. The tests store fresh tables through the paths that write array parts, collect, and check through weak pointers that the GC sees them. A randomized run checks the same rules, and the contents against a model.

I broke 18 guards and allocations one at a time. The tests catch 17. The other, the checked store in table.pack, is redundant, because the array's kind is chosen from the same values.

An earlier version of this description said the test covered every write path. It did not: of the 14 mutants I first tried, 8 went unnoticed, one of them the redundant store in table.pack. A later version gave that count as 7.

Also passing: the full suite, -race, lua_stackstress, arm64 vet, asmgate, and the corpus, suite and table tests under GOGC=1.

Results against main

sieve, time in interleaved runs, fastest and median. The fast loop's range is over two sets of runs; the other rows are one set of 9:

fastest median
Fast loop -8% to -10% -22% to -26%
Checked loop -12% -19%
Fast loop, GC marking -22% -20%

Costs:

  • Fast loop: instruction counts are within 1% for all 21 scripts, except parse at -1.9%. sieve and fannkuch are at 0.0% and -0.7%. The first version of this PR cost them 3.1% and 1.6%.
  • Checked loop: instruction counts rise by up to 1.9% (sieve +1.9%, format +1.7%, policy +1.2%, fannkuch +0.8%). fannkuch takes 3% to 9% longer, which repeats across runs; main against itself varies by 0.5% to 1.5%.
  • Other scripts' times: instructions were counted for all 21 scripts, but only 8 were timed: sieve, fannkuch, spectralnorm, nbody, sort, binarytrees, json and oop. Leaving out sieve, and fannkuch on the checked loop, which are above:
    • json's first set showed +3% fastest and +4% median on the fast loop, and +3% and +11% on the checked loop. A second set gave -1.5% and +9% on the fast loop, where main against itself gave +2% and +4%, so a slowdown there is not ruled out. On the checked loop it was +0.6% and +0.4%.
    • nbody with the GC marking showed +12% and +4% in the first set, and +0.8% and +2.6% in the second.
    • The rest are between 8% faster and 5% slower, and except for fannkuch on the fast loop, each comes from a single set of 9 runs.
    • An earlier version said that other scripts' times were within the variation between runs. That was more than these runs show.

The gain is limited to scripts with large arrays of numbers or booleans. Most of the GC-marking gap comes from write barriers on the register stack, which this doesn't touch.

…sight

A table's array part was a []Value, which the GC scans whole on every cycle.
sieve's 50000-slot arrays of booleans cost it a scan about once per table.

A slot that holds nil, a boolean or a number has a pointer only to a tag, a
global. While a table's array part holds nothing else it is now allocated as
scalarSlots, a Value's layout with a uintptr where the pointer was, which the
GC does not scan, and read as []Value as before. Table.ptrArr records that the
array may hold pointers; arrToPtrs moves the array to memory the GC scans
before the first one is stored. Stores go through arrSet, or check the flag.
A resize picks the kind from what goes into the new array.

In the loops, SETTABLE needs the check only when the slot's p changes, or for
an append: a value with the slot's own p cannot be the array's first pointer.

NEWTABLE's C operand, unused until now, is set when every positional item of
the constructor is a number, boolean or nil literal, or an arithmetic,
comparison or unary expression. It is a hint: without it, {left, right} would
allocate its array twice.

The tests store fresh tables through each path that writes an array part,
collect, and check by weak pointers that the GC sees them; a randomized run
checks the same rules and the contents against a model.

sieve takes 8 to 26 percent less time on the fast loop. The fast loop's
instruction counts are within 1 percent of before, except parse at 1.9 percent
fewer; the checked loop's rise by up to 1.9 percent, and fannkuch is 3 to 9
percent slower there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant