Skip to content

perf(stdlib): batch write-back in table.sort plain fast path #307

Closed

Description

@davydog187

Context

table.sort on plain (metatable-less) tables is 1.44× slower than luerl
at n=100 (46.1µs vs 31.9µs in benchmarks/table_ops.exs).

The plain-table sort fast path (PR #299) reads + sorts efficiently, but the
write-back loop still calls Table.put/3 per index, which rebuilds the
seven-field %Table{} struct on every iteration and updates state.tables.
Luerl's array:from_list writes the sorted slice back in a single step
(deps/luerl/src/luerl_lib_table.erl:363-373).

Change

lib/lua/vm/stdlib/table.ex:312-326 — replace:

```elixir
updated =
sorted
|> Enum.with_index(1)
|> Enum.reduce(table, fn {val, idx}, tbl -> Table.put(tbl, idx, val) end)
```

with a single map build and one Table.replace_data/2 call. replace_data
already exists. Sort doesn't touch order/dead-key state, so the simpler
replacement is safe.

Verification

  • mix test (full suite stays green)
  • mix test test/lua/vm/stdlib/table_test.exs
  • mix run benchmarks/table_ops.exs — record new ratio in the PR body

Expected

Closes ~30 % of the 1.44× sort gap. Target: ≤ 1.10× luerl.

Activity

  1. added this to the 1.0.0 milestone on Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind:perfPerformance change

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions