From b20971a72cd08cf3c8ba06091f5aa2a8d14ac49e Mon Sep 17 00:00:00 2001 From: "open-session-submodule[bot]" <341020755+open-session-submodule[bot]@users.noreply.github.com> Date: Wed, 11 Nov 2026 14:47:39 +0100 Subject: [PATCH 1/2] Allocate an environment table layer's map on its first binding 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 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> --- compiler-rust/src/types/env.rs | 134 ++++++++++++++++++++++++++++++--- 1 file changed, 125 insertions(+), 9 deletions(-) diff --git a/compiler-rust/src/types/env.rs b/compiler-rust/src/types/env.rs index d94474494b..ce9c05147f 100644 --- a/compiler-rust/src/types/env.rs +++ b/compiler-rust/src/types/env.rs @@ -490,6 +490,83 @@ fn ident_tbl_fold_name_order<'a>(names: impl Iterator) -> order } +/// The persistent map of one layer of an Env table. It allocates nothing +/// until its first insertion: every `open` starts a new layer in each of the +/// environment's tables, most layers never get a binding of their own, and +/// an empty persistent map still owns a root node and a hasher. +#[derive(Clone)] +struct LayerMap(Option>); + +impl std::fmt::Debug for LayerMap +where + K: std::fmt::Debug + std::hash::Hash + Eq, + V: std::fmt::Debug, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match &self.0 { + Some(map) => map.fmt(f), + None => f.write_str("{}"), + } + } +} + +impl Default for LayerMap { + fn default() -> Self { + LayerMap(None) + } +} + +impl LayerMap { + #[inline] + fn is_empty(&self) -> bool { + self.0.as_ref().is_none_or(|map| map.is_empty()) + } + + fn len(&self) -> usize { + self.0.as_ref().map_or(0, |map| map.len()) + } + + #[inline] + fn get(&self, key: &K) -> Option<&V> { + self.0.as_ref()?.get(key) + } + + #[inline] + fn contains_key(&self, key: &K) -> bool { + self.0.as_ref().is_some_and(|map| map.contains_key(key)) + } + + fn get_mut(&mut self, key: &K) -> Option<&mut V> { + self.0.as_mut()?.get_mut(key) + } + + fn insert(&mut self, key: K, value: V) { + self.0.get_or_insert_with(Default::default).insert(key, value); + } + + fn iter(&self) -> impl Iterator { + self.0.iter().flat_map(|map| map.iter()) + } + + fn keys(&self) -> impl Iterator { + self.iter().map(|(key, _)| key) + } + + fn values(&self) -> impl Iterator { + self.iter().map(|(_, value)| value) + } + + /// Whether both are the same map, sharing every node. + #[cfg(test)] + fn ptr_eq(&self, other: &Self) -> bool { + match (&self.0, &other.0) { + (Some(left), Some(right)) => left.ptr_eq(right), + (None, None) => true, + _ => false, + } + } +} + /// Same-name bindings in an Env table, oldest first. Most names are bound /// once, so a single binding is stored inline; a name bound again moves its /// bindings to a shared vector, so copying a persistent-map node on insert @@ -571,7 +648,7 @@ fn recent_name_bit(name: Name) -> u64 { /// map is copied once per batch. Every reader looks in the list first. #[derive(Debug, Clone)] pub struct IdTbl { - current: PersistentHashMap>, + current: LayerMap>, recent: Option>>, opened: Option>>, } @@ -579,7 +656,7 @@ pub struct IdTbl { impl IdTbl { pub fn new() -> Self { Self { - current: PersistentHashMap::default(), + current: LayerMap::default(), recent: None, opened: None, } @@ -676,7 +753,7 @@ impl IdTbl { } let next = std::mem::take(self); *self = Self { - current: PersistentHashMap::default(), + current: LayerMap::default(), recent: None, opened: Some(Arc::new(IdOpened { root: Some(root), @@ -998,18 +1075,18 @@ struct TycompOpened { /// as lists. #[derive(Debug, Clone)] pub struct TycompTbl { - current: PersistentHashMap>, + current: LayerMap>, /// Order in which each name of `current` was first added: the `Ident.add` /// sequence that shapes OCaml's AVL tree (compiler/ext/ident.ml:144-150). - current_first_add: PersistentHashMap, + current_first_add: LayerMap, opened: Option>>, } impl TycompTbl { pub fn new() -> Self { Self { - current: PersistentHashMap::default(), - current_first_add: PersistentHashMap::default(), + current: LayerMap::default(), + current_first_add: LayerMap::default(), opened: None, } } @@ -1042,8 +1119,8 @@ impl TycompTbl { } let next = std::mem::take(self); *self = Self { - current: PersistentHashMap::default(), - current_first_add: PersistentHashMap::default(), + current: LayerMap::default(), + current_first_add: LayerMap::default(), opened: Some(Arc::new(TycompOpened { components, next: Arc::new(next), @@ -3216,6 +3293,45 @@ mod tests { assert_eq!(snapshot.find_by_name("z"), None); } + #[test] + fn a_table_layer_has_no_map_until_a_binding_is_moved_into_it() { + let mut values: IdTbl = IdTbl::new(); + assert!(values.current.0.is_none()); + let first = Ident::create_local("x"); + values.add(first, 1); + assert!( + values.current.0.is_none(), + "a recent binding needs no map" + ); + for n in 1..RECENT_LIMIT { + values.add(Ident::create_local("y"), n as i32); + } + assert!(values.current.0.is_some()); + assert_eq!(values.current.len(), 2); + + // An open starts a new layer, which again has no map; the layer + // below keeps its own. + let root = Path::pident(Ident::create_persistent("Opened")); + values.add_open_layer(root, vec![(Ident::create_persistent("z"), 7, 0)], None); + assert!(values.current.0.is_none() && values.current.is_empty()); + assert_eq!(values.find_by_name("x"), Some(&1)); + assert_eq!(values.find_by_ident(&first), Some(&1)); + assert_eq!(values.find_by_name("z"), Some(&7)); + // Every binding of both layers: `x`, each `y` and the opened `z`. + assert_eq!(values.iter().count(), RECENT_LIMIT as usize + 1); + + let mut labels: TycompTbl = TycompTbl::new(); + assert!(labels.current.0.is_none() && labels.current_first_add.0.is_none()); + assert_eq!(labels.find_by_name("l"), None); + assert!(labels.fold_name_order().is_empty()); + labels.add(Ident::create_local("l"), 1); + assert!(labels.current.0.is_some() && labels.current_first_add.0.is_some()); + labels.add_open_layer(vec![(Ident::create_persistent("m"), 2)], None); + assert!(labels.current.0.is_none() && labels.current_first_add.0.is_none()); + assert_eq!(labels.find_by_name("l"), Some(&1)); + assert_eq!(labels.find_all("m"), vec![&2]); + } + #[test] fn updating_a_recent_binding_leaves_a_snapshot_alone() { let mut tbl = IdTbl::new(); From 6c2d57f0a03403edba223ed8ee2eae91d53afa1c Mon Sep 17 00:00:00 2001 From: "open-session-submodule[bot]" <341020755+open-session-submodule[bot]@users.noreply.github.com> Date: Wed, 11 Nov 2026 14:48:03 +0100 Subject: [PATCH 2/2] Document the lazy environment layer maps Co-authored-by: Jaap Frolich 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> --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4950221d69..59471d0ffe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -95,6 +95,7 @@ - Keep the bindings most recently added to a type-checking environment in a short list in front of its map, so a `let` no longer copies map nodes that every recorded environment then holds until the module is done. https://github.com/jfrolich/rescript-rust/pull/91 - Type-check faster: lower argument levels directly, settle the checks before binding a type variable in one walk, and skip the inclusion check of a unit against its own inferred signature. https://github.com/jfrolich/rescript-rust/pull/84 +- Allocate the map of an environment table layer on its first binding, so an `open` no longer creates and drops empty maps in every table. https://github.com/jfrolich/rescript-rust/pull/93 - Size the compile worker pool to one worker per core on CPUs with efficiency cores, instead of oversubscribing it. https://github.com/jfrolich/rescript-rust/pull/83 - Reduce backend identifier-allocation setup work by scanning arena identifier tables instead of recursively walking Lambda and JavaScript trees. https://github.com/jfrolich/rescript-rust/pull/82 - Remove OCaml leftovers: unused warning numbers, the `Record_float_unused` representation, the `ns.braces` attribute name and unused `Ast_helper` functions. https://github.com/rescript-lang/rescript/pull/8805