Skip to content

Share a loaded unit's alias rewrites between compilations - #92

Merged
jfrolich merged 2 commits into
ocaml-to-rust-rewritefrom
prelude-setup-once
Nov 11, 2026
Merged

jfrolich merged 2 commits into
ocaml-to-rust-rewritefrom
prelude-setup-once

Conversation

@jfrolich

@jfrolich jfrolich commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

What

A loaded unit's alias rewrites are computed once and shared by reference with every compilation that loads the unit, instead of being collected again per compilation.

Why

A compilation records, for every unit it loads, which flattened module names the unit's alias submodules stand for: module JSON = Stdlib_JSON makes Stdlib_JSON mean Stdlib.JSON. It did that by walking the unit's signature and inserting each rewrite into its own hash table, probing that table and the shared registry twice per alias. Every compilation opens the standard library, whose signature is mostly such aliases, so every module compiled repeated the same walk and the same insertions. In a profile of a 3,735-module application this was 0.45% of CPU.

How

  • PersStruct caches the table of its own rewrites (own_alias_rewrites), built on first use. The structure is shared between compilations, so the table is too.
  • AliasPrefixRewrites keeps a list of the tables of loaded units, in load order. A lookup consults the compilation's own entries, the registry and then the unit tables in order, which is the order the entries would have been recorded in, so the first rewrite of a name still wins. New entries are refused when a unit table already holds the name.
  • A unit reached through an alias (its rewrites carry that prefix) is recorded entry by entry, as before.

Behaviour

No change: the 3,980 generated files of the application are byte-identical to trunk's.

Measurements

Clean build of a 3,735-module application, release build with one codegen unit, the same binary with the table on and off, alternating pairs, retired instructions from /usr/bin/time -l:

Base Workers Off On Paired difference
4c8358d (current) 1 (n=14) 83.04 G 82.77 G −0.27 ± 0.10 G (−0.3%)
c7fabe6 1 (n=8) 79.83 G 79.57 G −0.27 ± 0.16 G (−0.3%)
c7fabe6 10 (n=12) 82.21 G 81.99 G −0.22 ± 0.15 G (−0.3%)

Peak RSS is unchanged.

Tests

Two unit tests in cmi::persistent_env:

  • a_units_alias_table_reads_like_recording_each_of_its_rewrites compares every reader of the rewrites between walking two units' signatures and adding their tables, with an earlier entry that must win and later entries that must lose;
  • a_loaded_unit_shares_its_alias_table_unless_it_is_reached_through_an_alias.

Started by Jaap Frolich in this Assistant session

open-session-submodule Bot and others added 2 commits November 11, 2026 18:23
A compilation records, for every unit it loads, which flattened module
names the unit's alias submodules stand for (`module JSON = Stdlib_JSON`
makes `Stdlib_JSON` mean `Stdlib.JSON`). It did so by walking the unit's
signature and inserting each rewrite into its own hash table, after
probing that table and the shared registry twice per alias. Every
compilation opens the standard library, whose signature is mostly such
aliases, so every module compiled paid for the same walk and the same
insertions.

A loaded unit now computes its rewrites once, into a table kept on the
shared structure, and a compilation that reaches the unit under its own
name adds a reference to that table. Lookups consult the compilation's
own entries, the registry and then the unit tables in load order, which
is the order the entries would have been recorded in, so the first
rewrite of a name still wins. A unit reached through an alias, whose
rewrites carry that prefix, is recorded entry by entry as before.

Clean build of a 3,735-module application, one-codegen-unit release
build, the same binary with the table on and off, alternating pairs:
- 1 worker: 79.83 -> 79.57 G instructions (paired -0.27 +- 0.16, n=8).
- 10 workers: 82.21 -> 81.99 G (paired -0.22 +- 0.15, n=12).
Peak RSS unchanged. 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 merged commit 8944104 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