Skip to content

Commit c1151f9

Browse files
committed
pkg/codesearch: reduce memory consumption more
Use uint8 enums instead of strings to store entity/reference kind. String is 16 bytes and is slower to work with.
1 parent 69ea5ad commit c1151f9

File tree

2 files changed

+104
-12
lines changed

2 files changed

+104
-12
lines changed

pkg/codesearch/codesearch.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func (index *Index) FileIndex(file string) ([]Entity, error) {
206206
for _, def := range index.db.Definitions {
207207
if def.Body.File == file {
208208
entities = append(entities, Entity{
209-
Kind: def.Kind,
209+
Kind: def.Kind.String(),
210210
Name: def.Name,
211211
})
212212
}
@@ -243,7 +243,7 @@ func (index *Index) definitionSource(contextFile, name string, comment, includeL
243243
}
244244
return &EntityInfo{
245245
File: def.Body.File,
246-
Kind: def.Kind,
246+
Kind: def.Kind.String(),
247247
Body: src,
248248
}, nil
249249
}
@@ -299,9 +299,9 @@ func (index *Index) FindReferences(contextFile, name, srcPrefix string, contextL
299299
}
300300
}
301301
results = append(results, ReferenceInfo{
302-
ReferencingEntityKind: def.Kind,
302+
ReferencingEntityKind: def.Kind.String(),
303303
ReferencingEntityName: def.Name,
304-
ReferenceKind: ref.Kind,
304+
ReferenceKind: ref.Kind.String(),
305305
SourceFile: def.Body.File,
306306
SourceLine: ref.Line,
307307
SourceSnippet: snippet,

pkg/codesearch/database.go

Lines changed: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package codesearch
55

66
import (
7+
"bytes"
78
"fmt"
89
"maps"
910
"slices"
@@ -23,7 +24,7 @@ type Database struct {
2324
}
2425

2526
type Definition struct {
26-
Kind string `json:"kind,omitempty"`
27+
Kind EntityKind `json:"kind,omitempty"`
2728
Name string `json:"name,omitempty"`
2829
Type string `json:"type,omitempty"`
2930
IsStatic bool `json:"is_static,omitempty"`
@@ -33,10 +34,10 @@ type Definition struct {
3334
}
3435

3536
type Reference struct {
36-
Kind string `json:"kind,omitempty"`
37-
EntityKind string `json:"entity_kind,omitempty"`
38-
Name string `json:"name,omitempty"`
39-
Line int `json:"line,omitempty"`
37+
Kind RefKind `json:"kind,omitempty"`
38+
EntityKind EntityKind `json:"entity_kind,omitempty"`
39+
Name string `json:"name,omitempty"`
40+
Line int `json:"line,omitempty"`
4041
}
4142

4243
type LineRange struct {
@@ -45,6 +46,100 @@ type LineRange struct {
4546
EndLine int `json:"end_line,omitempty"`
4647
}
4748

49+
type EntityKind uint8
50+
51+
const (
52+
entityKindInvalid EntityKind = iota
53+
EntityKindFunction
54+
EntityKindStruct
55+
EntityKindUnion
56+
EntityKindVariable
57+
EntityKindMacro
58+
EntityKindEnum
59+
EntityKindTypedef
60+
entityKindLast
61+
)
62+
63+
var entityKindNames = [...]string{
64+
EntityKindFunction: "function",
65+
EntityKindStruct: "struct",
66+
EntityKindUnion: "union",
67+
EntityKindVariable: "variable",
68+
EntityKindMacro: "macro",
69+
EntityKindEnum: "enum",
70+
EntityKindTypedef: "typedef",
71+
}
72+
73+
var entityKindBytes = func() [entityKindLast][]byte {
74+
var ret [entityKindLast][]byte
75+
for k, v := range entityKindNames {
76+
ret[k] = []byte("\"" + v + "\"")
77+
}
78+
return ret
79+
}()
80+
81+
func (v *EntityKind) String() string {
82+
return entityKindNames[*v]
83+
}
84+
85+
func (v *EntityKind) MarshalJSON() ([]byte, error) {
86+
return entityKindBytes[*v], nil
87+
}
88+
89+
func (v *EntityKind) UnmarshalJSON(data []byte) error {
90+
*v = entityKindInvalid
91+
for k, val := range entityKindBytes {
92+
if bytes.Equal(data, val) {
93+
*v = EntityKind(k)
94+
break
95+
}
96+
}
97+
return nil
98+
}
99+
100+
type RefKind uint8
101+
102+
const (
103+
refKindInvalid RefKind = iota
104+
RefKindUses
105+
RefKindCall
106+
RefKindTakesAddr
107+
refKindLast
108+
)
109+
110+
var refKindNames = [...]string{
111+
RefKindUses: "uses",
112+
RefKindCall: "calls",
113+
RefKindTakesAddr: "takes-address-of",
114+
}
115+
116+
var refKindBytes = func() [refKindLast][]byte {
117+
var ret [refKindLast][]byte
118+
for k, v := range refKindNames {
119+
ret[k] = []byte("\"" + v + "\"")
120+
}
121+
return ret
122+
}()
123+
124+
func (v *RefKind) String() string {
125+
return refKindNames[*v]
126+
}
127+
128+
func (v *RefKind) MarshalJSON() ([]byte, error) {
129+
return refKindBytes[*v], nil
130+
}
131+
132+
func (v *RefKind) UnmarshalJSON(data []byte) error {
133+
*v = refKindInvalid
134+
for k, val := range refKindBytes {
135+
if bytes.Equal(data, val) {
136+
*v = RefKind(k)
137+
break
138+
}
139+
}
140+
return nil
141+
}
142+
48143
// DatabaseFormatHash contains a hash uniquely identifying format of the database.
49144
// In covers both structure and semantics of the data, and is supposed to be used
50145
// for caching of the database files.
@@ -76,15 +171,12 @@ func (db *Database) Merge(other *Database, v *clangtool.Verifier) {
76171
if def.Comment.File != "" {
77172
v.LineRange(def.Comment.File, def.Comment.StartLine, def.Comment.EndLine)
78173
}
79-
db.intern(&def.Kind)
80174
db.intern(&def.Name)
81175
db.intern(&def.Type)
82176
db.intern(&def.Body.File)
83177
db.intern(&def.Comment.File)
84178
for _, ref := range def.Refs {
85-
db.intern(&ref.Kind)
86179
db.intern(&ref.Name)
87-
db.intern(&ref.EntityKind)
88180
}
89181
}
90182
}

0 commit comments

Comments
 (0)