@fraczak/k 8.4.0 → 8.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -0
- package/DOCS/ARCHITECTURE.md +83 -0
- package/DOCS/CODECS.md +93 -204
- package/DOCS/DICTIONARY.md +42 -1241
- package/DOCS/FILE_FORMATS.md +170 -625
- package/DOCS/GETTING_STARTED.md +164 -0
- package/DOCS/KIR_V1.md +22 -33
- package/DOCS/KVM_EXECUTION_MODEL.md +88 -481
- package/DOCS/PATTERNS.md +156 -123
- package/DOCS/POLYMORPHIC_IR.md +83 -101
- package/DOCS/PROFILING_AND_TRACING.md +52 -166
- package/DOCS/README.md +35 -0
- package/DOCS/REPL.md +100 -371
- package/DOCS/TEXTUAL_VALUES.md +53 -81
- package/DOCS/TYPE_DERIVATION.md +95 -234
- package/DOCS/book-combined.md +3786 -0
- package/Examples/arithmetics.k +45 -59
- package/Examples/djson.k +102 -112
- package/Examples/karatsuba-mult.k +2 -3
- package/Examples/list.k +6 -7
- package/Examples/nat.k +6 -6
- package/Examples/poly.k +45 -45
- package/LICENSE +15 -0
- package/README.md +72 -410
- package/backends/llvm/README.md +75 -97
- package/backends/llvm/src/cli.mjs +0 -31
- package/backends/wasm/README.md +88 -66
- package/backends/wasm/src/cli.mjs +0 -31
- package/canonical-pattern.mjs +69 -35
- package/codecs/README.md +23 -261
- package/codecs/WIRE_FORMAT.md +132 -0
- package/core.k +5 -15
- package/index.mjs +5 -1
- package/named-patterns.mjs +55 -7
- package/object.mjs +28 -6
- package/objects/README.md +24 -109
- package/package.json +8 -3
- package/paper/Makefile +17 -0
- package/paper/k-paper.pdf +0 -0
- package/paper/k-paper.tex +435 -0
- package/parser.jison +0 -10
- package/parser.mjs +32 -43
- package/repl.mjs +79 -101
- package/scripts/benchmark-idempotency.mjs +3 -3
- package/scripts/run-tests.mjs +1 -1
- package/scripts/test-package.mjs +64 -0
- package/tests/README.md +66 -46
- package/tests/code-derivation/core.mjs +61 -0
- package/tests/helpers/cli.mjs +45 -0
- package/tests/integration.sh +2 -3
- package/tests/test-arithmetics.mjs +47 -237
- package/tests/test-djson-methods.mjs +71 -275
- package/tests/test-hash-normalization.mjs +22 -1
- package/tests/test-karatsuba.mjs +18 -126
- package/tests/test-named-patterns.mjs +174 -606
- package/tests/test-paper-examples.mjs +90 -0
- package/tests/test-poly.mjs +2 -2
- package/tests/test-repl-html.mjs +2 -2
- package/tests/test-repl.mjs +89 -38
- package/tests/test.mjs +27 -68
- package/AGENTS.md +0 -35
- package/CLAUDE.md +0 -64
- package/DOCS/BIG_PICTURE.md +0 -439
- package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +0 -74
- package/DOCS/CANONICAL_PATTERNS.md +0 -349
- package/DOCS/CODEC_ARCHITECTURE.md +0 -257
- package/DOCS/CONVERGENCE.md +0 -123
- package/DOCS/DIVERGENCE_DETECTION.md +0 -23
- package/DOCS/JSON_APPROACHES_COMPARISON.md +0 -302
- package/DOCS/KIR_SPECIALIZATION_DESIGN.md +0 -472
- package/DOCS/Makefile +0 -99
- package/DOCS/NAMED_PATTERNS.md +0 -340
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +0 -343
- package/DOCS/PATTERNS_AND_TYPED_VALUES.md +0 -665
- package/DOCS/WEB_REPL_REQUIREMENTS.md +0 -136
- package/DOCS/book/01-introduction.md +0 -48
- package/DOCS/book/02-syntax-and-values.md +0 -110
- package/DOCS/book/03-types-as-finite-automata.md +0 -155
- package/DOCS/book/04-partial-functions-and-composition.md +0 -145
- package/DOCS/book/05-typing-filters-and-normalization.md +0 -109
- package/DOCS/book/06-values-in-memory.md +0 -1085
- package/DOCS/book/07-the-partial-function-abi.md +0 -143
- package/DOCS/book/08-operational-semantics-and-execution.md +0 -196
- package/DOCS/book/09-compiler-architecture.md +0 -160
- package/DOCS/book/10-from-ast-to-intermediate-representation.md +0 -166
- package/DOCS/book/14-canonical-serialization.md +0 -176
- package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +0 -156
- package/DOCS/book/overview.txt +0 -68
- package/DOCS/book.md +0 -50
- package/Examples/bnat.k +0 -308
- package/Examples/buda.k +0 -66
- package/Examples/byte.k +0 -35
- package/Examples/code.k +0 -43
- package/Examples/vhdl_to_k.k +0 -67
- package/JOURNAL.md +0 -288
- package/TODO.md +0 -107
- package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +0 -233
- package/backends/wasm/TECHNICAL-DEBT.md +0 -58
- package/backends/wasm/demo_ieee.mjs +0 -318
- package/browser/vfs-data.mjs +0 -21
- package/codecs/BINARY_FORMAT.md +0 -53
- package/codecs/POLYMORPHIC_BINARY_FORMAT.md +0 -303
- package/codecs/pandoc.css +0 -71
- package/tests/code-derivation/blowup.mjs +0 -25
- package/tests/code-derivation/bnat.mjs +0 -11
- package/tests/code-derivation/dot.mjs +0 -37
- package/tests/code-derivation/index.mjs +0 -20
- package/tests/code-derivation/loop.mjs +0 -17
- package/tests/code-derivation/nat.mjs +0 -10
- package/tests/code-derivation/not-not.mjs +0 -14
- package/tests/code-derivation/product.mjs +0 -22
- package/tests/code-derivation/rlz.mjs +0 -13
- package/tests/test-fingerprint.mjs +0 -38
package/DOCS/CONVERGENCE.md
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
# Convergence Strategies
|
|
2
|
-
|
|
3
|
-
This note describes the current structure of the JavaScript
|
|
4
|
-
type-derivation engine and the places intended for experimentation.
|
|
5
|
-
|
|
6
|
-
## Current default
|
|
7
|
-
|
|
8
|
-
Type derivation operates on the SCC DAG of relation dependencies.
|
|
9
|
-
|
|
10
|
-
- Singleton SCC with no self-reference:
|
|
11
|
-
- use `single_pass`
|
|
12
|
-
- clone already-derived callee boundary patterns once
|
|
13
|
-
- compact once
|
|
14
|
-
- Recursive SCC:
|
|
15
|
-
- use `fixed_point`
|
|
16
|
-
- iterate until signatures stabilize or the iteration budget is
|
|
17
|
-
exhausted
|
|
18
|
-
|
|
19
|
-
This split keeps acyclic modules fast while preserving the old
|
|
20
|
-
behavior for recursive polymorphic definitions.
|
|
21
|
-
|
|
22
|
-
## Public tuning hook
|
|
23
|
-
|
|
24
|
-
`annotate`, `compile`, and `run` accept:
|
|
25
|
-
|
|
26
|
-
```js
|
|
27
|
-
{
|
|
28
|
-
convergence: {
|
|
29
|
-
strategy: "auto" | "single_pass" | "fixed_point",
|
|
30
|
-
maxIterations?: number
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
The returned `annotate(...)` object includes:
|
|
36
|
-
|
|
37
|
-
```js
|
|
38
|
-
{
|
|
39
|
-
compileStats: {
|
|
40
|
-
sccCount,
|
|
41
|
-
sccs: [
|
|
42
|
-
{
|
|
43
|
-
members: ["relA", "relB"],
|
|
44
|
-
strategy: "fixed_point",
|
|
45
|
-
iterations: 3,
|
|
46
|
-
converged: true
|
|
47
|
-
}
|
|
48
|
-
]
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
This is the supported inspection surface for convergence experiments.
|
|
54
|
-
|
|
55
|
-
## Why the current fast path works
|
|
56
|
-
|
|
57
|
-
For an acyclic singleton SCC, every callee has already been fully
|
|
58
|
-
derived by the time the current relation is processed. There is no
|
|
59
|
-
need for repeated fixed-point propagation because:
|
|
60
|
-
|
|
61
|
-
1. Callee boundary patterns are stable.
|
|
62
|
-
2. The current relation only needs those stable input/output boundaries.
|
|
63
|
-
3. One propagation pass plus one compaction is sufficient.
|
|
64
|
-
|
|
65
|
-
`Examples/ieee.k` is exactly this shape: a large acyclic dependency
|
|
66
|
-
graph. The major speedup comes from avoiding unnecessary fixed-point
|
|
67
|
-
iteration there.
|
|
68
|
-
|
|
69
|
-
## Current implementation choices
|
|
70
|
-
|
|
71
|
-
Two implementation changes matter for performance:
|
|
72
|
-
|
|
73
|
-
1. Compaction now starts from the quotient graph of live
|
|
74
|
-
representatives instead of cloning the full historical union-find
|
|
75
|
-
forest.
|
|
76
|
-
2. Singleton-pattern registration is incremental and no longer
|
|
77
|
-
re-finalizes the entire global code repository on every compaction.
|
|
78
|
-
|
|
79
|
-
These two changes are what make `Examples/ieee.k` practical to
|
|
80
|
-
type-check.
|
|
81
|
-
|
|
82
|
-
## Role in Ahead-Of-Time (AOT) Compilation
|
|
83
|
-
|
|
84
|
-
Convergence strategies dictate the time spent during Ahead-Of-Time
|
|
85
|
-
(AOT) type checking. Because compilation resolves and converges these
|
|
86
|
-
dependency SCCs beforehand into `.ko`, `.klib`, and `.kvm` artifacts,
|
|
87
|
-
runtime execution does not incur any convergence overhead.
|
|
88
|
-
|
|
89
|
-
## Where to experiment next
|
|
90
|
-
|
|
91
|
-
If you want to continue convergence work, these are the main seams:
|
|
92
|
-
|
|
93
|
-
- `convergence.mjs`
|
|
94
|
-
- strategy selection
|
|
95
|
-
- recursive SCC processing
|
|
96
|
-
- future divergence detection
|
|
97
|
-
- `compression.mjs`
|
|
98
|
-
- quotient-graph construction
|
|
99
|
-
- equivalence partitioning
|
|
100
|
-
- alternative canonicalization schemes
|
|
101
|
-
- `typing.mjs`
|
|
102
|
-
- clone/unify behavior
|
|
103
|
-
- pattern-graph data structure
|
|
104
|
-
|
|
105
|
-
Promising next experiments:
|
|
106
|
-
|
|
107
|
-
- Replace signature-string stabilization in `fixed_point` with
|
|
108
|
-
structural change tracking.
|
|
109
|
-
- Add divergence heuristics for recursive polymorphic SCCs.
|
|
110
|
-
- Cache cloned callee boundary graphs for repeated call sites within
|
|
111
|
-
one relation.
|
|
112
|
-
- Compare the current partition refinement in `compression.mjs` with
|
|
113
|
-
a hash-consed canonical-form approach.
|
|
114
|
-
|
|
115
|
-
## Further Reading
|
|
116
|
-
|
|
117
|
-
- [DOCS/FILE_FORMATS.md](./FILE_FORMATS.md) — guide to
|
|
118
|
-
.k, .ko, .klib, and .kvm formats
|
|
119
|
-
- [DOCS/TYPE_DERIVATION.md](./TYPE_DERIVATION.md) — type derivation
|
|
120
|
-
algorithm and rules
|
|
121
|
-
- [DOCS/OBJECT_FILE_AND_PATTERN.md](./OBJECT_FILE_AND_PATTERN.md) —
|
|
122
|
-
object format and pattern encoding
|
|
123
|
-
- [DOCS/DICTIONARY.md](./DICTIONARY.md) — concept names and terminology
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
# Divergence detection (sketch)
|
|
2
|
-
|
|
3
|
-
We can detect divergence when there is a cycle of definitions:
|
|
4
|
-
|
|
5
|
-
```
|
|
6
|
-
X1 = D1;
|
|
7
|
-
X2 = D2;
|
|
8
|
-
...
|
|
9
|
-
Xn = Dn;
|
|
10
|
-
```
|
|
11
|
-
|
|
12
|
-
such that for each i, `Xi` is in `HEAD(Di-1)` (with indices modulo n).
|
|
13
|
-
|
|
14
|
-
Define `HEAD` as the set of direct first ref calls:
|
|
15
|
-
|
|
16
|
-
```
|
|
17
|
-
HEAD(ref(X)) = {X}
|
|
18
|
-
HEAD(?F E) = HEAD(E)
|
|
19
|
-
HEAD((E1 E2 ...)) = HEAD(E1)
|
|
20
|
-
HEAD({ E1 f1, E2 f2, ... }) = HEAD(E1) union HEAD(E2) union ...
|
|
21
|
-
HEAD(<E1, E2, ...>) = {} -- the head of a union is the empty set
|
|
22
|
-
HEAD(/ tag) = HEAD(. lab) = HEAD(| tag) = {}
|
|
23
|
-
```
|
|
@@ -1,302 +0,0 @@
|
|
|
1
|
-
# Comparison of JSON Approaches in k
|
|
2
|
-
|
|
3
|
-
> [!NOTE]
|
|
4
|
-
> **Resolution**: The **binary bit-trie** was selected and standardized
|
|
5
|
-
> as the sole dynamic JSON implementation in `codecs/djson.mjs` and
|
|
6
|
-
> `Examples/djson.k`. The cons-list and Patricia trie implementations
|
|
7
|
-
> were removed. This document preserves the architectural comparison
|
|
8
|
-
> and benchmark findings that guided this design decision.
|
|
9
|
-
|
|
10
|
-
This document compares the JSON codec and schema implementations:
|
|
11
|
-
|
|
12
|
-
1. **Schema-Specializing JSON** (`json`):
|
|
13
|
-
[`codecs/json.mjs`][json-mjs]
|
|
14
|
-
2. **Universal Dynamic JSON (Binary Bit-Trie)** (`djson`):
|
|
15
|
-
[`codecs/djson.mjs`][djson-mjs] & [`Examples/djson.k`][djson-k]
|
|
16
|
-
|
|
17
|
-
[json-mjs]: file:///home/wojtek/gits/K/k/codecs/json.mjs
|
|
18
|
-
[djson-mjs]: file:///home/wojtek/gits/K/k/codecs/djson.mjs
|
|
19
|
-
[djson-k]: file:///home/wojtek/gits/K/k/Examples/djson.k
|
|
20
|
-
|
|
21
|
-
---
|
|
22
|
-
|
|
23
|
-
## 1. Architectural Motivation
|
|
24
|
-
|
|
25
|
-
In **k**, all values are algebraic data types classified by closed type
|
|
26
|
-
patterns. External formats like JSON can be embedded into **k** via two
|
|
27
|
-
fundamentally distinct strategies:
|
|
28
|
-
|
|
29
|
-
### Static Schema Specialization (`json`)
|
|
30
|
-
- Infers a concrete closed pattern specialized to the exact shape and
|
|
31
|
-
field names of each input JSON document.
|
|
32
|
-
- Objects become concrete labeled products `{ "name": Str, "age": Num }`.
|
|
33
|
-
- Lists become homogeneous or heterogeneous tuples/cons-lists.
|
|
34
|
-
- **Advantage**: Zero interpretive overhead for known static schemas;
|
|
35
|
-
direct field projection in compiled code.
|
|
36
|
-
- **Limitation**: The pattern is dynamic and grows unboundedly with the
|
|
37
|
-
document. Cannot write universal JSON transformation relations (like a
|
|
38
|
-
generic JSON query engine or REPL printer) under a single fixed closed
|
|
39
|
-
pattern.
|
|
40
|
-
|
|
41
|
-
### Dynamic Uniform JSON (`djson*` family)
|
|
42
|
-
- Defines a single, fixed closed pattern `json?` once for all valid JSON
|
|
43
|
-
values. Any arbitrary JSON document parses into an injection of this
|
|
44
|
-
universal pattern.
|
|
45
|
-
- Allows authoring universal helper relations in `k`:
|
|
46
|
-
- `dot: {key, object} -> val`
|
|
47
|
-
- `has_key?: {key, object} -> ()`
|
|
48
|
-
- `nth: {index, list} -> val`
|
|
49
|
-
- `obj_size: object -> bits?`
|
|
50
|
-
- `list_size: list -> bits?`
|
|
51
|
-
- `remove: {key, object} -> object`
|
|
52
|
-
- `put: {key, val, object} -> object`
|
|
53
|
-
- Compiles once to Wasm or LLVM `-O3` binaries that execute over any
|
|
54
|
-
input JSON document without recompilation.
|
|
55
|
-
|
|
56
|
-
---
|
|
57
|
-
|
|
58
|
-
## 2. Structural Schema Comparison
|
|
59
|
-
|
|
60
|
-
| Dimension | `djson` (Cons) | `djson-btrie` | `djson-ptrie` |
|
|
61
|
-
| :--- | :--- | :--- | :--- |
|
|
62
|
-
| **Nodes** | 29 | 27 | 33 |
|
|
63
|
-
| **Numbers** | float64 | float64 | float64 |
|
|
64
|
-
| **Strings** | Cons-list | Cons-list | Cons-list |
|
|
65
|
-
| **Lists** | Cons-list | Bit-trie | Patricia trie |
|
|
66
|
-
| **Objects** | Sorted cons | Bit-trie | Radix trie |
|
|
67
|
-
| **Node** | `{car, cdr}` | `{val, 0, 1}` | `{pfx, sz, val, d}` |
|
|
68
|
-
| **Indexing**| $O(N)$ linear | $O(\log N)$ | $O(\log N)$ |
|
|
69
|
-
| **Lookup** | $O(K \cdot L)$ | $O(L \cdot 8)$ | $O(L)$ radix |
|
|
70
|
-
| **Size** | $O(N)$ walk | $O(N)$ walk | **$O(1)$** field |
|
|
71
|
-
|
|
72
|
-
### Schema Definitions
|
|
73
|
-
|
|
74
|
-
#### Dynamic Cons-List (`djson`)
|
|
75
|
-
```k
|
|
76
|
-
LIST = < {} nil, { JSON car, LIST cdr } cons >;
|
|
77
|
-
ENTRY = { Str key, JSON val };
|
|
78
|
-
OBJECT = < {} nil, { ENTRY car, OBJECT cdr } cons >;
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
#### Dynamic Binary Bit-Trie (`djson-btrie`)
|
|
82
|
-
```k
|
|
83
|
-
TRIE = <
|
|
84
|
-
{} nil,
|
|
85
|
-
{ < {} nil, JSON val > val, TRIE 0, TRIE 1 } trie
|
|
86
|
-
>;
|
|
87
|
-
LIST = TRIE;
|
|
88
|
-
OBJECT = TRIE;
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
#### Dynamic Patricia/Radix Trie (`djson-ptrie`)
|
|
92
|
-
```k
|
|
93
|
-
LIST = <
|
|
94
|
-
{} nil,
|
|
95
|
-
{
|
|
96
|
-
BITS prefix,
|
|
97
|
-
BITS size,
|
|
98
|
-
< {} nil, JSON val > val,
|
|
99
|
-
LIST 0,
|
|
100
|
-
LIST 1
|
|
101
|
-
} trie
|
|
102
|
-
>;
|
|
103
|
-
DICT = < {} nil, { { CHAR char, OBJECT trie } car, DICT cdr } cons >;
|
|
104
|
-
OBJECT = <
|
|
105
|
-
{} nil,
|
|
106
|
-
{ Str prefix, BITS size, < {} nil, JSON val > val, DICT dict } trie
|
|
107
|
-
>;
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
---
|
|
111
|
-
|
|
112
|
-
## 3. Algorithmic Mechanics
|
|
113
|
-
|
|
114
|
-
### 3.1 Object Key Lookup (`dot`)
|
|
115
|
-
- **`djson`**: Iterates through a linked list of `{key, val}` pairs.
|
|
116
|
-
On each entry, performs character-by-character string equality
|
|
117
|
-
`eq_string?`. For $N$ entries with key length $L$, worst-case time is
|
|
118
|
-
$O(N \cdot L)$.
|
|
119
|
-
- **`djson-btrie`**: Uses `step_char` to walk 8 to 24 bits directly down
|
|
120
|
-
the binary trie per character. Because character bits are decoded
|
|
121
|
-
destructively into branch selectors without allocating `bits?` nodes,
|
|
122
|
-
it executes with **zero heap allocations** in compiled LLVM code.
|
|
123
|
-
- **`djson-ptrie`**: Matches the query key against the node's `prefix`
|
|
124
|
-
run using `match_str_prefix`. If the prefix matches, it looks up the
|
|
125
|
-
next branching character in a small local `dict` cons-list.
|
|
126
|
-
Non-branching paths of arbitrary length are traversed in one step.
|
|
127
|
-
|
|
128
|
-
### 3.2 Key Membership (`has_key?`)
|
|
129
|
-
- **Hit**: All three navigate to the target key.
|
|
130
|
-
- **Miss**:
|
|
131
|
-
- `djson` scans all $N$ entries to the end of the cons-list.
|
|
132
|
-
- `djson-btrie` and `djson-ptrie` terminate immediately as soon as a bit
|
|
133
|
-
or character branch is missing, achieving sub-microsecond rejection
|
|
134
|
-
even on large documents.
|
|
135
|
-
|
|
136
|
-
### 3.3 Array Indexing (`nth`)
|
|
137
|
-
- **`djson`**: Decodes a `bits?` natural number with `dec_nat` and walks
|
|
138
|
-
cons cells one by one ($O(I)$).
|
|
139
|
-
- **`djson-btrie`**: Dispatches directly on the bits of the index from
|
|
140
|
-
most-significant to least-significant bit ($O(\log I)$).
|
|
141
|
-
- **`djson-ptrie`**: Compares the remaining index bits against the
|
|
142
|
-
node's `prefix: BITS` in chunks, descending along compressed paths
|
|
143
|
-
($O(\log I)$).
|
|
144
|
-
|
|
145
|
-
### 3.4 Subtree Cardinality (`obj_size`, `list_size`)
|
|
146
|
-
- **`djson` & `djson-btrie`**: Must recursively traverse the entire cons
|
|
147
|
-
list or bit-trie, adding 1 for each present entry using binary addition
|
|
148
|
-
(`inc_nat`). Complexity is $O(N \log N)$.
|
|
149
|
-
- **`djson-ptrie`**: Stores a precomputed binary natural number
|
|
150
|
-
`size: BITS` at every node. Size queries are simple $O(1)$ field
|
|
151
|
-
projections (`ptrie_obj? < /nil 0, /trie .size >`). In LLVM `-O3`, this
|
|
152
|
-
executes in **5 nanoseconds**, independent of document size.
|
|
153
|
-
|
|
154
|
-
### 3.5 Functional Updates (`remove`, `put`)
|
|
155
|
-
- **`djson`**: Copies the cons spine until the modified entry ($O(N)$).
|
|
156
|
-
- **`djson-btrie`**: Rebuilds the bit path down to the leaf. `put` uses
|
|
157
|
-
`str_to_bits` to construct the bit path, while `remove` clears `.val` to
|
|
158
|
-
`nil` along the path.
|
|
159
|
-
- **`djson-ptrie`**:
|
|
160
|
-
- `remove` checks `has_key?`, decrements `size` with `dec_nat` along
|
|
161
|
-
the path, and clears `.val` to `nil`.
|
|
162
|
-
- `put` uses `split_str_prefix` to find the common prefix between the
|
|
163
|
-
new key and existing node. If prefixes diverge, it splits the node
|
|
164
|
-
into a shared parent with a 2-entry branch dictionary in $O(L)$ time.
|
|
165
|
-
|
|
166
|
-
---
|
|
167
|
-
|
|
168
|
-
## 4. Empirical Benchmark Results
|
|
169
|
-
|
|
170
|
-
Measured with `npm run perf:djson` on Linux x86_64 across sizes
|
|
171
|
-
$N \in \{10, 100, 500\}$.
|
|
172
|
-
|
|
173
|
-
### 4.1 Wire Representation Sizes
|
|
174
|
-
|
|
175
|
-
#### Shared-Prefix Objects (`{"field_prefix_0000": 0, ...}`)
|
|
176
|
-
- **$N = 10$** (Raw: 230 B):
|
|
177
|
-
- `djson`: 1,029 B (4.47x)
|
|
178
|
-
- `djson-btrie`: **913 B (3.97x)**
|
|
179
|
-
- `djson-ptrie`: 1,032 B (4.49x)
|
|
180
|
-
- `json` (Spec): 370 B (1.61x)
|
|
181
|
-
- **$N = 100$** (Raw: 2,750 B):
|
|
182
|
-
- `djson`: 3,365 B (1.22x)
|
|
183
|
-
- `djson-btrie`: **2,096 B (0.76x)**
|
|
184
|
-
- `djson-ptrie`: 2,367 B (0.86x)
|
|
185
|
-
- `json` (Spec): 1,529 B (0.56x)
|
|
186
|
-
- **$N = 500$** (Raw: 14,750 B):
|
|
187
|
-
- `djson`: 14,357 B (0.97x)
|
|
188
|
-
- `djson-btrie`: **7,370 B (0.50x)**
|
|
189
|
-
- `djson-ptrie`: 8,241 B (0.56x)
|
|
190
|
-
- `json` (Spec): 7,229 B (0.49x)
|
|
191
|
-
|
|
192
|
-
#### Varied-Key Objects (`{"account_0_account_attr": 0, ...}`)
|
|
193
|
-
- **$N = 10$** (Raw: 270 B):
|
|
194
|
-
- `djson`: **1,114 B (4.13x)**
|
|
195
|
-
- `djson-btrie`: 1,460 B (5.41x)
|
|
196
|
-
- `djson-ptrie`: 1,236 B (4.58x)
|
|
197
|
-
- `json` (Spec): 496 B (1.84x)
|
|
198
|
-
- **$N = 100$** (Raw: 3,120 B):
|
|
199
|
-
- `djson`: 4,987 B (1.60x)
|
|
200
|
-
- `djson-btrie`: 6,729 B (2.16x)
|
|
201
|
-
- `djson-ptrie`: **4,309 B (1.38x)**
|
|
202
|
-
- `json` (Spec): 4,460 B (1.43x)
|
|
203
|
-
- **$N = 500$** (Raw: 16,598 B):
|
|
204
|
-
- `djson`: 22,581 B (1.36x)
|
|
205
|
-
- `djson-btrie`: 28,478 B (1.72x)
|
|
206
|
-
- `djson-ptrie`: **17,036 B (1.03x)**
|
|
207
|
-
- `json` (Spec): 22,104 B (1.33x)
|
|
208
|
-
|
|
209
|
-
#### Homogeneous Number Arrays (`[0, 3, 6, ...]`)
|
|
210
|
-
- **$N = 10$** (Raw: 27 B):
|
|
211
|
-
- `djson`: 880 B (32.6x)
|
|
212
|
-
- `djson-btrie`: **878 B (32.5x)**
|
|
213
|
-
- `djson-ptrie`: 999 B (37.0x)
|
|
214
|
-
- **$N = 100$** (Raw: 363 B):
|
|
215
|
-
- `djson`: **1,645 B (4.53x)**
|
|
216
|
-
- `djson-btrie`: 1,666 B (4.59x)
|
|
217
|
-
- `djson-ptrie`: 1,877 B (5.17x)
|
|
218
|
-
- **$N = 500$** (Raw: 2,129 B):
|
|
219
|
-
- `djson`: **5,045 B (2.37x)**
|
|
220
|
-
- `djson-btrie`: 5,166 B (2.43x)
|
|
221
|
-
- `djson-ptrie`: 5,776 B (2.71x)
|
|
222
|
-
|
|
223
|
-
---
|
|
224
|
-
|
|
225
|
-
### 4.2 Execution Latency Across Backends ($N=500$)
|
|
226
|
-
|
|
227
|
-
#### Object Lookup (`dot` on last key of 500-key object)
|
|
228
|
-
- **Shared-Prefix Keys**:
|
|
229
|
-
- `djson`: Wasm $580.4\ \mu\text{s}$, LLVM $23.6\ \mu\text{s}$
|
|
230
|
-
- `djson-btrie`: Wasm $7.4\ \mu\text{s}$, LLVM **$2.4\ \mu\text{s}$**
|
|
231
|
-
- `djson-ptrie`: Wasm $11.1\ \mu\text{s}$, LLVM $5.1\ \mu\text{s}$
|
|
232
|
-
- **Varied Keys**:
|
|
233
|
-
- `djson`: Wasm $26.8\ \mu\text{s}$, LLVM $4.2\ \mu\text{s}$
|
|
234
|
-
- `djson-btrie`: Wasm $10.9\ \mu\text{s}$, LLVM **$3.1\ \mu\text{s}$**
|
|
235
|
-
- `djson-ptrie`: Wasm $10.8\ \mu\text{s}$, LLVM $4.4\ \mu\text{s}$
|
|
236
|
-
|
|
237
|
-
#### Key Membership Filter (`has_key?` miss on nonexistent key)
|
|
238
|
-
- `djson`: Wasm $63.9\ \mu\text{s}$, LLVM $1,168\ \text{ns}$
|
|
239
|
-
- `djson-btrie`: Wasm $1,022\ \text{ns}$, LLVM **$466\ \text{ns}$**
|
|
240
|
-
- `djson-ptrie`: Wasm $1,908\ \text{ns}$, LLVM $1,228\ \text{ns}$
|
|
241
|
-
|
|
242
|
-
#### Array Element Lookup (`nth` on index 499 of 500 elements)
|
|
243
|
-
- `djson`:
|
|
244
|
-
kVM $10.74\ \text{ms}$, Wasm $97.2\ \mu\text{s}$,
|
|
245
|
-
LLVM $100.7\ \mu\text{s}$
|
|
246
|
-
- `djson-btrie`:
|
|
247
|
-
kVM $26.0\ \mu\text{s}$, Wasm $733\ \text{ns}$,
|
|
248
|
-
LLVM **$114\ \text{ns}$** (**881x speedup**)
|
|
249
|
-
- `djson-ptrie`:
|
|
250
|
-
kVM $35.7\ \mu\text{s}$, Wasm $1,267\ \text{ns}$,
|
|
251
|
-
LLVM $371\ \text{ns}$ (**271x speedup**)
|
|
252
|
-
|
|
253
|
-
#### Cardinality Queries (`obj_size` for $N=500$)
|
|
254
|
-
- `djson`:
|
|
255
|
-
kVM $13.57\ \text{ms}$, Wasm $89.2\ \mu\text{s}$,
|
|
256
|
-
LLVM $127.4\ \mu\text{s}$
|
|
257
|
-
- `djson-btrie`:
|
|
258
|
-
kVM $210.88\ \text{ms}$, Wasm $3.02\ \text{ms}$,
|
|
259
|
-
LLVM $8.06\ \text{ms}$
|
|
260
|
-
- `djson-ptrie`:
|
|
261
|
-
kVM $2,054\ \text{ns}$, Wasm $172\ \text{ns}$,
|
|
262
|
-
LLVM **$5\ \text{ns}$** (**26,106x speedup**)
|
|
263
|
-
|
|
264
|
-
#### Object Key Updates ($N=500$)
|
|
265
|
-
- **`remove`**:
|
|
266
|
-
- `djson`: LLVM $79.6\ \mu\text{s}$
|
|
267
|
-
- `djson-btrie`: LLVM **$9.5\ \mu\text{s}$** (8.4x speedup)
|
|
268
|
-
- `djson-ptrie`: LLVM $13.3\ \mu\text{s}$ (6.0x speedup)
|
|
269
|
-
- **`put`**:
|
|
270
|
-
- `djson`: LLVM $50.8\ \mu\text{s}$
|
|
271
|
-
- `djson-btrie`: LLVM $12.6\ \mu\text{s}$ (4.0x speedup)
|
|
272
|
-
- `djson-ptrie`: LLVM **$3.9\ \mu\text{s}$** (13.1x speedup)
|
|
273
|
-
|
|
274
|
-
---
|
|
275
|
-
|
|
276
|
-
## 5. Trade-off Analysis & Recommendation Matrix
|
|
277
|
-
|
|
278
|
-
| Feature | `json` | `djson` | `btrie` | `ptrie` |
|
|
279
|
-
| :--- | :---: | :---: | :---: | :---: |
|
|
280
|
-
| **Universal Pattern** | ❌ No | ✅ 29 | ✅ 27 | ✅ 33 |
|
|
281
|
-
| **REPL Inspection** | Native | Cons | Bit-tree | Radix |
|
|
282
|
-
| **Shared Prefix Wire**| Small | Med | **Small** | Small |
|
|
283
|
-
| **Varied Keys Wire** | Large | Med | Large | **Small** |
|
|
284
|
-
| **Array ($N=500$)** | Direct | $100\ \mu\text{s}$ | **114ns**| 371ns |
|
|
285
|
-
| **Dot ($N=500$)** | Direct | $23.6\ \mu\text{s}$| **2.4µs**| 5.1µs |
|
|
286
|
-
| **Put ($N=500$)** | N/A | $50.8\ \mu\text{s}$| 12.6µs | **3.9µs**|
|
|
287
|
-
| **Size ($N=500$)** | N/A | $127\ \mu\text{s}$ | 8.1ms | **5ns** |
|
|
288
|
-
|
|
289
|
-
### Recommendations
|
|
290
|
-
1. **Choose `djson-ptrie` for general-purpose dynamic JSON**:
|
|
291
|
-
It achieves the best wire compression on real-world varied keys
|
|
292
|
-
(1.03x of raw JSON), provides instant $O(1)$ cardinality queries
|
|
293
|
-
(5 ns in LLVM), and delivers the fastest updates (`put` in 3.9 µs).
|
|
294
|
-
2. **Choose `djson-btrie` for extreme lookup throughput on shared keys**:
|
|
295
|
-
Destructive bit-walking in `dot` avoids all node allocation,
|
|
296
|
-
achieving the lowest lookup latency (2.4 µs in LLVM).
|
|
297
|
-
3. **Keep `djson` for simple, lightweight tooling**:
|
|
298
|
-
Simple cons-list structure is human-readable in REPL raw outputs and
|
|
299
|
-
has minimal code complexity for small payloads ($N < 50$).
|
|
300
|
-
4. **Choose `json` when document schema is fixed at compile time**:
|
|
301
|
-
Avoids all dynamic dictionary lookups in exchange for a
|
|
302
|
-
document-specialized pattern.
|