-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathintern.go
More file actions
75 lines (68 loc) · 2.05 KB
/
Copy pathintern.go
File metadata and controls
75 lines (68 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package helium
import "github.com/lestrrat-go/helium/internal/lexicon"
// globalNames is a pre-built map of well-known XML name strings.
// Looking up a scanned name here returns the compile-time constant,
// avoiding a heap allocation for the string.
var globalNames map[string]string
var globalNameCandidateMask [256]uint32
func init() {
globalNames = make(map[string]string, len(lexicon.WellKnownNames))
for _, s := range lexicon.WellKnownNames {
globalNames[s] = s
if len(s) < 32 {
globalNameCandidateMask[s[0]] |= 1 << len(s)
}
}
}
func couldBeGlobalNameBytes(b []byte) bool {
if len(b) == 0 || len(b) >= 32 {
return false
}
return globalNameCandidateMask[b[0]]&(1<<len(b)) != 0
}
func couldBeGlobalNameString(s string) bool {
if len(s) == 0 || len(s) >= 32 {
return false
}
return globalNameCandidateMask[s[0]]&(1<<len(s)) != 0
}
// internName returns a deduplicated version of s. It checks the global
// well-known table first (zero allocation on hit), then the per-parse table.
func (pctx *parserCtx) internName(s string) string {
// Tier 1: global well-known names (zero alloc on hit).
if couldBeGlobalNameString(s) {
if interned, ok := globalNames[s]; ok {
return interned
}
}
// Tier 2: per-parse deduplication.
if pctx.nameCache == nil {
pctx.nameCache = make(map[string]string)
}
if interned, ok := pctx.nameCache[s]; ok {
return interned
}
pctx.nameCache[s] = s
return s
}
// internNameBytes returns a deduplicated string for the given byte slice.
// Uses Go's map optimization: map[string]([]byte) lookups don't allocate
// when the key is a []byte→string conversion used only for the lookup.
func (pctx *parserCtx) internNameBytes(b []byte) string {
// Tier 1: global well-known names.
if couldBeGlobalNameBytes(b) {
if interned, ok := globalNames[string(b)]; ok {
return interned
}
}
// Tier 2: per-parse deduplication.
if pctx.nameCache == nil {
pctx.nameCache = make(map[string]string)
}
if interned, ok := pctx.nameCache[string(b)]; ok {
return interned
}
s := string(b)
pctx.nameCache[s] = s
return s
}