pool.go raw
1 package lattice
2
3 import "sort"
4
5 // In-memory second and third layers (Meta, Lex, StringPool) and the adjacency
6 // list (Adj), carried from go-ref. iskra does not use them: it keeps its meta
7 // beside the .iskr in .meta and its strings in the 48-byte Record. These are
8 // the only structures MESH stores that .iskr does not.
9
10 // AllocMeta appends an empty MetaEntry and returns its index.
11 func (t *Tree) AllocMeta() uint32 {
12 t.Meta = append(t.Meta, MetaEntry{})
13 return uint32(len(t.Meta) - 1)
14 }
15
16 // AllocLex appends an empty LexEntry and returns its index.
17 func (t *Tree) AllocLex() uint32 {
18 t.Lex = append(t.Lex, LexEntry{})
19 return uint32(len(t.Lex) - 1)
20 }
21
22 // MetaAt returns the MetaEntry at idx; the caller has checked the bounds.
23 func (t *Tree) MetaAt(idx uint32) *MetaEntry { return &t.Meta[idx] }
24
25 // LexAt returns the LexEntry at idx.
26 func (t *Tree) LexAt(idx uint32) *LexEntry { return &t.Lex[idx] }
27
28 // PoolPtr returns the string pool for LexEntry.SetForm.
29 func (t *Tree) PoolPtr() *[]byte { return &t.StringPool }
30
31 // RegisterKey records a key's meta and lex indices for MESH's key map.
32 func (t *Tree) RegisterKey(key Key, metaIdx, lexIdx uint32) {
33 if t.KeyMeta == nil {
34 t.KeyMeta = make(map[Key][2]uint32)
35 }
36 t.KeyMeta[key] = [2]uint32{metaIdx, lexIdx}
37 }
38
39 // LookupMetaByKey returns the meta and lex indices registered for a key.
40 func (t *Tree) LookupMetaByKey(key Key) (metaIdx, lexIdx uint32, ok bool) {
41 v, ok := t.KeyMeta[key]
42 return v[0], v[1], ok
43 }
44
45 // SetCounts sets the block, meta and lex counts of a freshly loaded MESH tree.
46 func (t *Tree) SetCounts(nodes, meta, lex uint32) {
47 t.nCount = nodes
48 t.MetaLen = meta
49 t.LexLen = lex
50 if t.MetaLen == 0 && len(t.Meta) > 0 {
51 t.MetaLen = uint32(len(t.Meta))
52 }
53 if t.LexLen == 0 && len(t.Lex) > 0 {
54 t.LexLen = uint32(len(t.Lex))
55 }
56 }
57
58 // pendingAdj stages AddAdj calls before FinalizeAdj lays them out contiguously.
59 func (t *Tree) AddAdj(srcMeta, tgtMeta uint32) {
60 if t.pendingAdj == nil {
61 t.pendingAdj = make(map[uint32][]uint32)
62 }
63 t.pendingAdj[srcMeta] = append(t.pendingAdj[srcMeta], tgtMeta)
64 }
65
66 // FinalizeAdj writes the staged adjacency block contiguously, source by source
67 // in ascending order, and sets AdjOffset and AdjCount on every source entry.
68 // go-ref iterated a map here, which orders the block differently on every run;
69 // the order is part of the MESH bytes, so it is sorted.
70 func (t *Tree) FinalizeAdj() {
71 t.Adj = t.Adj[:0]
72 for i := range t.Meta {
73 t.Meta[i].AdjOffset = 0
74 t.Meta[i].AdjCount = 0
75 }
76 srcs := make([]uint32, 0, len(t.pendingAdj))
77 for src := range t.pendingAdj {
78 srcs = append(srcs, src)
79 }
80 sort.Slice(srcs, func(i, j int) bool { return srcs[i] < srcs[j] })
81 for _, src := range srcs {
82 targets := t.pendingAdj[src]
83 if int(src) >= len(t.Meta) {
84 continue
85 }
86 meta := &t.Meta[src]
87 meta.AdjOffset = uint32(len(t.Adj))
88 if len(targets) > 0xFFFF {
89 panic("lattice: more than 65535 adjacency links for one entry")
90 }
91 meta.AdjCount = uint16(len(targets))
92 for _, tgt := range targets {
93 t.Adj = append(t.Adj, AdjLink{TargetMeta: tgt})
94 }
95 }
96 t.pendingAdj = nil
97 }
98
99 // L3Size and MaxNodesInL3 are go-ref's cache-pressure thresholds, reported by
100 // NeedsRepack. Nothing in iskra uses them.
101 const (
102 L3Size = 4 << 20
103 MaxNodesInL3 = L3Size / NodeSize
104 )
105
106 // NodeBytes is the in-memory block array size.
107 func (t *Tree) NodeBytes() int { return int(t.nCount) * NodeSize }
108
109 // NeedsRepack reports whether the block array exceeds the L3 threshold.
110 func (t *Tree) NeedsRepack() bool { return t.NodeBytes() > L3Size }
111
112 // NewMeshTree builds the tree a MESH file describes: one key space in block 0,
113 // the three pools as read, and no record table, because MESH stores meta and
114 // lex indices rather than records.
115 func NewMeshTree(nodes []Node, meta []MetaEntry, lex []LexEntry, adj []AdjLink, pool []byte) *Tree {
116 t := &Tree{
117 Nodes: nodes,
118 Meta: meta,
119 Lex: lex,
120 Adj: adj,
121 StringPool: pool,
122 FreeHead: NullIndex,
123 RecKey: make(map[uint32]Key),
124 KeyMeta: make(map[Key][2]uint32),
125 nCount: uint32(len(nodes)),
126 MetaLen: uint32(len(meta)),
127 LexLen: uint32(len(lex)),
128 }
129 if len(meta) > 0 || len(lex) > 0 {
130 // MESH reserves index 0 of a populated pool as the null pointer.
131 t.MetaLen = uint32(len(meta))
132 }
133 return t
134 }
135
136 // EntryCount is the number of entries in a MESH-style tree: index 0 of every
137 // pool is the null sentinel.
138 func (t *Tree) EntryCount() int {
139 if t.MetaLen <= 1 {
140 return 0
141 }
142 return int(t.MetaLen) - 1
143 }
144
145 // NewMeshRootTree returns a tree with a single empty root block at index 0 and
146 // no other branch roots: the shape MESH requires.
147 func NewMeshRootTree() *Tree {
148 t := &Tree{
149 Nodes: make([]Node, 1),
150 FreeHead: NullIndex,
151 nCount: 1,
152 RecKey: make(map[uint32]Key),
153 KeyMeta: make(map[Key][2]uint32),
154 }
155 initNodeChildren(&t.Nodes[0])
156 return t
157 }
158
159 // NewRecordPoolTree returns a tree with a single root at block 0, record index
160 // 0 held as MESH's null pointer, and meta/lex pools starting at index 1. Entries
161 // go in from record 1.
162 func NewRecordPoolTree(capacity int) *Tree {
163 t := NewMeshRootTree()
164 t.Records = make([]Record, 1, capacity+1)
165 t.Records[0].Link = [2]uint32{NullRec, NullRec}
166 t.rCount = 1
167 t.Meta = make([]MetaEntry, 1)
168 t.Lex = make([]LexEntry, 1)
169 t.MetaLen = 1
170 t.LexLen = 1
171 return t
172 }
173