Skip to content

Add environment bindings to a short list in front of the map - #91

Merged
jfrolich merged 2 commits into
ocaml-to-rust-rewritefrom
env-recent-bindings
Nov 11, 2026
Merged

jfrolich merged 2 commits into
ocaml-to-rust-rewritefrom
env-recent-bindings

Conversation

@jfrolich

@jfrolich jfrolich commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

What

A table of environment bindings (IdTbl: values, types, modules, module types) now keeps the bindings added since its map was last extended in a short linked list, newest first, and moves the list into the persistent map once it holds 16 of them.

Why

Every typed-tree node records the environment it was checked in, so each version of an environment's tables stays alive until the module is torn down. The local bindings were held only in a persistent hash map, and adding one binding to a map that a snapshot shares copies the map nodes on the path to it. Each node is up to 32 entries wide, so one let allocated and kept 1 KB or more, took a reference count for every entry copied, and paid the same again to release it at teardown. In a profile of a 3,735-module application, adding values was 1.06% of CPU and dropping the recorded environments 1.14%.

With the list, adding a binding allocates one small node and the map is copied once per batch. Readers look in the list first. Each node carries a bit set of the names in the list below it, so a lookup of a name that is not listed does not walk it.

Behaviour

No change. Every reader of the table was updated to read the list before the map:

  • lookups by name, by identifier and find_all see list bindings as the newest ones;
  • updating a binding in place moves the list into the map first;
  • the name order used for spelling suggestions is computed from a merged copy, so it is the same as before.

Measurements

Clean build of a 3,735-module application, release builds with one codegen unit, alternating pairs, retired instructions from /usr/bin/time -l. Re-measured after rebasing onto 4c8358d:

Workers Trunk This branch Paired difference Peak RSS
1 (n=6) 83.33 G 82.94 G −0.39 ± 0.09 G (−0.5%) 964 → 956 MB
10 (n=10) 85.98 G 85.48 G −0.50 ± 0.22 G (−0.6%) 1315 → 1283 MB

Before the rebase (base c7fabe6) the same comparison gave −0.59 ± 0.22 G at 1 worker and, with the list switched on and off inside one binary, −0.58 ± 0.16 G at 10 workers. Batch sizes 8, 12, 16, 24, 32 and 64 were compared in one binary; 16 was best single-threaded and within noise of 12 at 10 workers.

The 3,980 generated files are byte-identical to trunk's, before and after the rebase.

Tests

Three unit tests in types::env:

  • recent_bindings_read_like_map_bindings compares every reader of a table that uses the list against one that adds straight to the map, after each of 53 additions with rebinding and an open in the middle of a batch;
  • adding_to_a_table_leaves_a_snapshot_and_its_map_alone checks that an addition after a snapshot does not copy the map and that the snapshot keeps its view;
  • updating_a_recent_binding_leaves_a_snapshot_alone.

Started by Jaap Frolich in this Assistant session

open-session-submodule Bot and others added 2 commits November 11, 2026 18:22
Every typed-tree node records the environment it was checked in, so each
version of an environment's binding tables stays alive until the module is
torn down. The local bindings of a table are a persistent hash map, and
adding one binding to a map a snapshot shares copies the map nodes on the
path to it, each up to 32 entries wide: about 1 KB or more allocated and
kept per binding, a reference count taken for every entry copied, and the
same again to release them at teardown.

A table now keeps the bindings added since its map was last extended in a
linked list, newest first, and moves the list into the map once it holds
16 of them. Adding a binding allocates one small node, and the map is
copied once per batch. Readers look in the list first; each node carries a
bit set of the names in the list below it, so a lookup of a name that is
not listed does not walk it.

Clean build of a 3,735-module application, one-codegen-unit release
builds, alternating pairs:
- 1 worker: 79.99 -> 79.40 G instructions (paired -0.59 +- 0.22, n=6),
  peak RSS 954 -> 942 MB.
- 10 workers, same binary with the list on and off: 82.33 -> 81.75 G
  (paired -0.58 +- 0.16, n=10), peak RSS 1267 -> 1239 MB.
Generated files (3,980) and the incremental build record are identical.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1c9fa-0c5d-7912-a93b-3e7f38e53547
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1c9fa-0c5d-7912-a93b-3e7f38e53547
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
@jfrolich
jfrolich force-pushed the env-recent-bindings branch from 7b952b2 to 93be21e Compare November 11, 2026 17:23
@jfrolich
jfrolich merged commit beedd82 into ocaml-to-rust-rewrite Nov 11, 2026
1 check passed
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