From fe7939cddef833b04452ae639783debf91393388 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 07:21:10 +0000 Subject: [PATCH] Map.done: its slots given back, leaving it empty Met in rwalk, whose table of plain-name rules lives for one directory of a walk: Buf and Vec had done, Map had none, so its memory could only be dropped. done frees the slots, if any, and leaves the map as init does, to be used again. Library growth that breaks no program; test/run/MapDone counts the blocks through an allocator of its own. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 9b44f96..cb6c1da 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -491,29 +491,6 @@ breaks a `match` on `Status` that names all four, so it is left out: the shell knows when it continues a job itself. -## `Map.done` - -`Map` takes memory from its allocator as it grows and frees the old -slots, but never gives the last ones back: a map made over `Heap` for -a while, as a table kept for one directory of a walk, can't be -emptied of its memory, only dropped, and the memory is lost. `Buf` -and `Vec` each have a `done`; `Map` has none, and is the odd one out. - -``` -proc done*(m: ^Map); (* its memory given back, leaving it empty *) -``` - -As `Vec.done`: free the slots if there are any, then `cap` and `len` -0, so the map can be used again and grows anew. `init` already leaves -it so, with no memory taken until the first `put`. - -Met in rwalk's `Rwignore`, which keeps a table of the rules that are a -plain name (`node_modules`, `target`) for each ignore file, made as the -walk enters a directory and dropped as it leaves. Without `done` it -used a `Vec` of rule numbers instead, searched in turn. Library growth -that breaks no program; about ten lines, with a test in `Map`'s -`main`. - ## Enumerations' names, each its own An enumeration's names are declared in the scope around its type diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 6c2bdcd..a675a23 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -520,6 +520,7 @@ proc remove*(m: ^Map; k: K): BOOL; (* false if k wasn't there *) proc get*(m: ^Map; k: K): (V, BOOL); (* what k maps to, and whether it maps to anything *) proc next*(m: ^Map; i: INT): (INT, K, V); (* the entry after i *) +proc done*(m: ^Map); (* its memory given back, leaving it empty *) module Vec(T); type Vec* = record len*: INT; ... end; diff --git a/lib/Map.raz b/lib/Map.raz index 127b99e..1779381 100644 --- a/lib/Map.raz +++ b/lib/Map.raz @@ -115,4 +115,14 @@ return -1, k, v; end; +(* Gives the map's memory back to its allocator, leaving it empty, as + init does: it may be used again, and grows anew. *) +proc done*(m: ^Map); +begin + if m^.slots # nil then Mem.free(m^.a, ^U8(m^.slots)); end; + m^.slots := nil; + m^.cap := 0; + m^.len := 0; +end; + end Map. diff --git a/test/run/MapDone.expect b/test/run/MapDone.expect new file mode 100644 index 0000000..deb6b84 --- /dev/null +++ b/test/run/MapDone.expect @@ -0,0 +1,4 @@ +3 entries, 1 block +0 entries, 0 blocks, found false +1 entry, 7 +0 blocks diff --git a/test/run/MapDone.raz b/test/run/MapDone.raz new file mode 100644 index 0000000..d5c0fdd --- /dev/null +++ b/test/run/MapDone.raz @@ -0,0 +1,39 @@ +(* Map.done: the slots given back to the allocator, through one that + counts its blocks; the map empty after, and used again. *) +module MapDone; +import Fmt, Heap, Mem, Str, Names := Map([]U8, INT, Str.hash, Str.eq); + +var counting: Mem.Allocator; live: INT; + +proc alloc(_a: ^Mem.Allocator; size, align: INT): ^U8; +begin + INC(live); + return Mem.alloc(Heap.heap, size, align); +end; + +proc free(_a: ^Mem.Allocator; p: ^U8); +begin + DEC(live); + Mem.free(Heap.heap, p); +end; + +proc main*; +var m: Names.Map; i, v: INT; found, _ok: BOOL; keys: [3][]U8; +begin + counting := {alloc: alloc, free: free}; + keys := ["node_modules", "target", "build"]; + Names.init(ADR(m), ADR(counting)); + Names.done(ADR(m)); (* nothing taken yet *) + for i := 0 to 99 do _ok := Names.put(ADR(m), keys[i mod 3], i); end; + Fmt.print("{} entries, {} block\n", [Fmt.int(m.len), Fmt.int(live)]); + Names.done(ADR(m)); + v, found := Names.get(ADR(m), "target"); + Fmt.print("{} entries, {} blocks, found {}\n", [Fmt.int(m.len), Fmt.int(live), Fmt.bool(found)]); + _ok := Names.put(ADR(m), "target", 7); + v, found := Names.get(ADR(m), "target"); + Fmt.print("{} entry, {}\n", [Fmt.int(m.len), Fmt.int(v)]); + Names.done(ADR(m)); + Fmt.print("{} blocks\n", [Fmt.int(live)]); +end; + +end MapDone.