Background
PR #272 (rescribet, 2018) implemented term memoization across the term classes and measured real wins — ~40% memory reduction and ~8% parse-time improvement in the in-thread benchmark. It was closed unmerged today because every file it touched was moved or rewritten in the TypeScript / data-factory migration, with a commitment to re-file the idea at its modern seam. This is that re-file.
Proposal
Implement term interning/memoization in the pluggable data factory rather than in the term classes:
- The natural seam is
src/factories/canonical-data-factory.ts, where terms are created and already id-keyed (id() serializes terms to N-Quads strings) — an intern cache keyed by that id can return the existing term instance instead of allocating a new one.
- Because factories are pluggable, interning can ship as an opt-in factory (or factory option) first, leaving allocation behavior unchanged by default until measured across real workloads.
- Candidate scope:
namedNode/literal/blankNode creation paths, which dominate allocation during parsing.
Expected benefit
Per #272's benchmark: substantial memory reduction (duplicate terms collapse to one instance) and faster equality paths (interned terms can compare by identity before value comparison). Numbers need re-measuring on the current TS codebase.
References
Background
PR #272 (rescribet, 2018) implemented term memoization across the term classes and measured real wins — ~40% memory reduction and ~8% parse-time improvement in the in-thread benchmark. It was closed unmerged today because every file it touched was moved or rewritten in the TypeScript / data-factory migration, with a commitment to re-file the idea at its modern seam. This is that re-file.
Proposal
Implement term interning/memoization in the pluggable data factory rather than in the term classes:
src/factories/canonical-data-factory.ts, where terms are created and already id-keyed (id()serializes terms to N-Quads strings) — an intern cache keyed by that id can return the existing term instance instead of allocating a new one.namedNode/literal/blankNodecreation paths, which dominate allocation during parsing.Expected benefit
Per #272's benchmark: substantial memory reduction (duplicate terms collapse to one instance) and faster equality paths (interned terms can compare by identity before value comparison). Numbers need re-measuring on the current TS codebase.
References