1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 // print writes all the data structures as well as the code necessary to use the
17 func (b *builder) print(w io.Writer) error {
18 b.Stats.NValueEntries = len(b.ValueBlocks) * blockSize
19 b.Stats.NValueBytes = len(b.ValueBlocks) * blockSize * b.ValueSize
20 b.Stats.NIndexEntries = len(b.IndexBlocks) * blockSize
21 b.Stats.NIndexBytes = len(b.IndexBlocks) * blockSize * b.IndexSize
22 b.Stats.NHandleBytes = len(b.Trie) * 2 * b.IndexSize
24 // If we only have one root trie, all starter blocks are at position 0 and
25 // we can access the arrays directly.
27 // At this point we cannot refer to the generated tables directly.
28 b.ASCIIBlock = b.Name + "Values"
29 b.StarterBlock = b.Name + "Index"
31 // Otherwise we need to have explicit starter indexes in the trie
33 b.ASCIIBlock = "t.ascii"
34 b.StarterBlock = "t.utf8Start"
37 b.SourceType = "[]byte"
38 if err := lookupGen.Execute(w, b); err != nil {
42 b.SourceType = "string"
43 if err := lookupGen.Execute(w, b); err != nil {
47 if err := trieGen.Execute(w, b); err != nil {
51 for _, c := range b.Compactions {
52 if err := c.c.Print(w); err != nil {
60 func printValues(n int, values []uint64) string {
63 fmt.Fprintf(w, "\t// Block %#x, offset %#x", n, boff)
65 for i, v := range values {
74 fmt.Fprintf(w, "\t%#02x:%#04x, ", boff+i, v)
80 func printIndex(b *builder, nr int, n *node) string {
82 boff := nr * blockSize
83 fmt.Fprintf(w, "\t// Block %#x, offset %#x", nr, boff)
85 for i, c := range n.children {
90 v := b.Compactions[c.index.compaction].Offset + uint32(c.index.index)
96 fmt.Fprintf(w, "\t%#02x:%#02x, ", boff+i, v)
104 trieGen = template.Must(template.New("trie").Funcs(template.FuncMap{
105 "printValues": printValues,
106 "printIndex": printIndex,
107 "title": strings.Title,
108 "dec": func(x int) int { return x - 1 },
109 "psize": func(n int) string {
110 return fmt.Sprintf("%d bytes (%.2f KiB)", n, float64(n)/1024)
112 }).Parse(trieTemplate))
113 lookupGen = template.Must(template.New("lookup").Parse(lookupTemplate))
116 // TODO: consider the return type of lookup. It could be uint64, even if the
117 // internal value type is smaller. We will have to verify this with the
118 // performance of unicode/norm, which is very sensitive to such changes.
119 const trieTemplate = `{{$b := .}}{{$multi := gt (len .Trie) 1}}
120 // {{.Name}}Trie. Total size: {{psize .Size}}. Checksum: {{printf "%08x" .Checksum}}.
121 type {{.Name}}Trie struct { {{if $multi}}
122 ascii []{{.ValueType}} // index for ASCII bytes
123 utf8Start []{{.IndexType}} // index for UTF-8 bytes >= 0xC0
126 func new{{title .Name}}Trie(i int) *{{.Name}}Trie { {{if $multi}}
127 h := {{.Name}}TrieHandles[i]
128 return &{{.Name}}Trie{ {{.Name}}Values[uint32(h.ascii)<<6:], {{.Name}}Index[uint32(h.multi)<<6:] }
131 type {{.Name}}TrieHandle struct {
132 ascii, multi {{.IndexType}}
135 // {{.Name}}TrieHandles: {{len .Trie}} handles, {{.Stats.NHandleBytes}} bytes
136 var {{.Name}}TrieHandles = [{{len .Trie}}]{{.Name}}TrieHandle{
137 {{range .Trie}} { {{.ASCIIIndex}}, {{.StarterIndex}} }, // {{printf "%08x" .Checksum}}: {{.Name}}
139 return &{{.Name}}Trie{}
142 // lookupValue determines the type of block n and looks up the value for b.
143 func (t *{{.Name}}Trie) lookupValue(n uint32, b byte) {{.ValueType}}{{$last := dec (len .Compactions)}} {
144 switch { {{range $i, $c := .Compactions}}
145 {{if eq $i $last}}default{{else}}case n < {{$c.Cutoff}}{{end}}:{{if ne $i 0}}
146 n -= {{$c.Offset}}{{end}}
147 return {{print $b.ValueType}}({{$c.Handler}}){{end}}
151 // {{.Name}}Values: {{len .ValueBlocks}} blocks, {{.Stats.NValueEntries}} entries, {{.Stats.NValueBytes}} bytes
152 // The third block is the zero block.
153 var {{.Name}}Values = [{{.Stats.NValueEntries}}]{{.ValueType}} {
154 {{range $i, $v := .ValueBlocks}}{{printValues $i $v}}
157 // {{.Name}}Index: {{len .IndexBlocks}} blocks, {{.Stats.NIndexEntries}} entries, {{.Stats.NIndexBytes}} bytes
158 // Block 0 is the zero block.
159 var {{.Name}}Index = [{{.Stats.NIndexEntries}}]{{.IndexType}} {
160 {{range $i, $v := .IndexBlocks}}{{printIndex $b $i $v}}
164 // TODO: consider allowing zero-length strings after evaluating performance with
166 const lookupTemplate = `
167 // lookup{{if eq .SourceType "string"}}String{{end}} returns the trie value for the first UTF-8 encoding in s and
168 // the width in bytes of this encoding. The size will be 0 if s does not
169 // hold enough bytes to complete the encoding. len(s) must be greater than 0.
170 func (t *{{.Name}}Trie) lookup{{if eq .SourceType "string"}}String{{end}}(s {{.SourceType}}) (v {{.ValueType}}, sz int) {
173 case c0 < 0x80: // is ASCII
174 return {{.ASCIIBlock}}[c0], 1
176 return 0, 1 // Illegal UTF-8: not a starter, not ASCII.
177 case c0 < 0xE0: // 2-byte UTF-8
181 i := {{.StarterBlock}}[c0]
183 if c1 < 0x80 || 0xC0 <= c1 {
184 return 0, 1 // Illegal UTF-8: not a continuation byte.
186 return t.lookupValue(uint32(i), c1), 2
187 case c0 < 0xF0: // 3-byte UTF-8
191 i := {{.StarterBlock}}[c0]
193 if c1 < 0x80 || 0xC0 <= c1 {
194 return 0, 1 // Illegal UTF-8: not a continuation byte.
196 o := uint32(i)<<6 + uint32(c1)
197 i = {{.Name}}Index[o]
199 if c2 < 0x80 || 0xC0 <= c2 {
200 return 0, 2 // Illegal UTF-8: not a continuation byte.
202 return t.lookupValue(uint32(i), c2), 3
203 case c0 < 0xF8: // 4-byte UTF-8
207 i := {{.StarterBlock}}[c0]
209 if c1 < 0x80 || 0xC0 <= c1 {
210 return 0, 1 // Illegal UTF-8: not a continuation byte.
212 o := uint32(i)<<6 + uint32(c1)
213 i = {{.Name}}Index[o]
215 if c2 < 0x80 || 0xC0 <= c2 {
216 return 0, 2 // Illegal UTF-8: not a continuation byte.
218 o = uint32(i)<<6 + uint32(c2)
219 i = {{.Name}}Index[o]
221 if c3 < 0x80 || 0xC0 <= c3 {
222 return 0, 3 // Illegal UTF-8: not a continuation byte.
224 return t.lookupValue(uint32(i), c3), 4
230 // lookup{{if eq .SourceType "string"}}String{{end}}Unsafe returns the trie value for the first UTF-8 encoding in s.
231 // s must start with a full and valid UTF-8 encoded rune.
232 func (t *{{.Name}}Trie) lookup{{if eq .SourceType "string"}}String{{end}}Unsafe(s {{.SourceType}}) {{.ValueType}} {
234 if c0 < 0x80 { // is ASCII
235 return {{.ASCIIBlock}}[c0]
237 i := {{.StarterBlock}}[c0]
238 if c0 < 0xE0 { // 2-byte UTF-8
239 return t.lookupValue(uint32(i), s[1])
241 i = {{.Name}}Index[uint32(i)<<6+uint32(s[1])]
242 if c0 < 0xF0 { // 3-byte UTF-8
243 return t.lookupValue(uint32(i), s[2])
245 i = {{.Name}}Index[uint32(i)<<6+uint32(s[2])]
246 if c0 < 0xF8 { // 4-byte UTF-8
247 return t.lookupValue(uint32(i), s[3])