Skip to content

Allocate an environment table layer's map on its first binding - #93

Merged
jfrolich merged 2 commits into
ocaml-to-rust-rewritefrom
env-lazy-layer-maps
Nov 11, 2026
Merged

jfrolich merged 2 commits into
ocaml-to-rust-rewritefrom
env-lazy-layer-maps

Conversation

@jfrolich

Copy link
Copy Markdown
Owner

Stacked on #91 (the recent-binding list), which this change relies on.

What

The persistent map of an environment table layer is allocated by its first insertion instead of when the layer is created.

Why

Every open starts a new layer in each of the environment's seven binding tables (values, types, constructors, labels, modules, module components, module types). Each layer held one or two persistent hash maps, and an empty persistent map still owns a root node of about 1.5 KB and a reference-counted hasher. Opening a layer also built the maps twice, once for the value mem::take leaves behind. That is about 18 root nodes allocated, zeroed and later dropped per open, and every compilation opens the standard library before anything else. Most layers never get a binding of their own, and with #91 a layer needs its map only once sixteen bindings have been added to it.

LayerMap wraps the map in an Option; readers see an empty map until the first insertion fills it.

Behaviour

No change: the 3,980 generated files of a 3,735-module application are byte-identical.

Measurements

Clean build of that application, release build with one codegen unit, the same binary with lazy and eager maps, alternating pairs, retired instructions from /usr/bin/time -l:

Workers Eager Lazy Paired difference
1 (n=12) 82.67 G 82.45 G −0.21 ± 0.16 G (−0.3%)

Peak RSS is unchanged.

Tests

a_table_layer_has_no_map_until_a_binding_is_moved_into_it in types::env, for both table kinds, including lookups through a layer without a map. The 228 tests under types:: pass.


Started by Jaap Frolich in this Assistant session

@jfrolich
jfrolich force-pushed the env-lazy-layer-maps branch from 1bbe060 to b2af266 Compare November 11, 2026 17:23
@jfrolich
jfrolich force-pushed the env-recent-bindings branch from 7b952b2 to 93be21e Compare November 11, 2026 17:23
@jfrolich
jfrolich changed the base branch from env-recent-bindings to ocaml-to-rust-rewrite November 11, 2026 17:53
open-session-submodule Bot and others added 2 commits November 11, 2026 18:53
Every `open` starts a new layer in each of the environment's seven
binding tables, and each layer held one or two persistent hash maps. An
empty persistent map still owns a root node of about 1.5 KB and a
reference-counted hasher, and opening a layer built the maps twice (once
for the value left behind by `mem::take`). Most layers never get a
binding of their own, and with the recent-binding list a layer needs its
map only once sixteen bindings have been added to it.

`LayerMap` wraps the map in an `Option` that is filled by the first
insertion; readers see an empty map until then.

Clean build of a 3,735-module application, one-codegen-unit release
build, the same binary with lazy and eager maps, alternating pairs,
1 worker: 82.67 -> 82.45 G instructions (paired -0.21 +- 0.16, n=12).
Peak RSS unchanged; the 3,980 generated files 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-lazy-layer-maps branch from b2af266 to 6c2d57f Compare November 11, 2026 17:54
@jfrolich
jfrolich merged commit 60a9d19 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