Skip to content

Commit 623df38

Browse files
committed
pkg/codesearch: reduce memory consumption a bit more
Use uint32 instead of int for line numbers (2G lines should be enough for everyone). Reorder fields to remove unnecessary paddings.
1 parent c1de322 commit 623df38

File tree

7 files changed

+56
-55
lines changed

7 files changed

+56
-55
lines changed

pkg/codesearch/codesearch.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ func (index *Index) FindReferences(contextFile, name, srcPrefix string, contextL
266266
if srcPrefix != "" {
267267
srcPrefix = filepath.Clean(srcPrefix)
268268
}
269+
contextLines = min(contextLines, 10000)
269270
totalCount := 0
270271
var results []ReferenceInfo
271272
for _, def := range index.db.Definitions {
@@ -289,8 +290,8 @@ func (index *Index) FindReferences(contextFile, name, srcPrefix string, contextL
289290
if contextLines > 0 {
290291
lines := LineRange{
291292
File: def.Body.File,
292-
StartLine: max(def.Body.StartLine, ref.Line-contextLines),
293-
EndLine: min(def.Body.EndLine, ref.Line+contextLines),
293+
StartLine: max(def.Body.StartLine, uint32(max(0, int(ref.Line)-contextLines))),
294+
EndLine: min(def.Body.EndLine, ref.Line+uint32(contextLines)),
294295
}
295296
var err error
296297
snippet, err = index.formatSource(lines, true)
@@ -303,7 +304,7 @@ func (index *Index) FindReferences(contextFile, name, srcPrefix string, contextL
303304
ReferencingEntityName: def.Name,
304305
ReferenceKind: ref.Kind.String(),
305306
SourceFile: def.Body.File,
306-
SourceLine: ref.Line,
307+
SourceLine: int(ref.Line),
307308
SourceSnippet: snippet,
308309
})
309310
}
@@ -342,7 +343,7 @@ func (index *Index) formatSource(lines LineRange, includeLines bool) (string, er
342343
if !osutil.IsExist(file) {
343344
continue
344345
}
345-
return formatSourceFile(file, lines.StartLine, lines.EndLine, includeLines)
346+
return formatSourceFile(file, int(lines.StartLine), int(lines.EndLine), includeLines)
346347
}
347348
return "", fmt.Errorf("codesearch: can't find %q file in any of %v", lines.File, index.srcDirs)
348349
}

pkg/codesearch/database.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,26 @@ type Database struct {
2424
}
2525

2626
type Definition struct {
27-
Kind EntityKind `json:"kind,omitempty"`
2827
Name string `json:"name,omitempty"`
2928
Type string `json:"type,omitempty"`
29+
Kind EntityKind `json:"kind,omitempty"`
3030
IsStatic bool `json:"is_static,omitempty"`
3131
Body LineRange `json:"body,omitempty"`
3232
Comment LineRange `json:"comment,omitempty"`
3333
Refs []Reference `json:"refs,omitempty"`
3434
}
3535

3636
type Reference struct {
37+
Name string `json:"name,omitempty"`
3738
Kind RefKind `json:"kind,omitempty"`
3839
EntityKind EntityKind `json:"entity_kind,omitempty"`
39-
Name string `json:"name,omitempty"`
40-
Line int `json:"line,omitempty"`
40+
Line uint32 `json:"line,omitempty"`
4141
}
4242

4343
type LineRange struct {
4444
File string `json:"file,omitempty"`
45-
StartLine int `json:"start_line,omitempty"`
46-
EndLine int `json:"end_line,omitempty"`
45+
StartLine uint32 `json:"start_line,omitempty"`
46+
EndLine uint32 `json:"end_line,omitempty"`
4747
}
4848

4949
type EntityKind uint8
@@ -167,9 +167,9 @@ func (db *Database) Merge(other *Database, v *clangtool.Verifier) {
167167
}
168168
db.mergeCache[id] = def
169169
db.reverseCache[def] = id
170-
v.LineRange(def.Body.File, def.Body.StartLine, def.Body.EndLine)
170+
v.LineRange(def.Body.File, int(def.Body.StartLine), int(def.Body.EndLine))
171171
if def.Comment.File != "" {
172-
v.LineRange(def.Comment.File, def.Comment.StartLine, def.Comment.EndLine)
172+
v.LineRange(def.Comment.File, int(def.Comment.StartLine), int(def.Comment.EndLine))
173173
}
174174
db.intern(&def.Name)
175175
db.intern(&def.Type)

pkg/codesearch/testdata/mm/refs.c.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"definitions": [
33
{
4-
"kind": "function",
54
"name": "ref_in_mm",
65
"type": "void ()",
6+
"kind": "function",
77
"body": {
88
"file": "mm/refs.c",
99
"start_line": 3,
@@ -12,9 +12,9 @@
1212
"comment": {},
1313
"refs": [
1414
{
15+
"name": "refs2",
1516
"kind": "calls",
1617
"entity_kind": "function",
17-
"name": "refs2",
1818
"line": 5
1919
}
2020
]

pkg/codesearch/testdata/refs.c.json

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"definitions": [
33
{
4-
"kind": "function",
54
"name": "long_func_with_ref",
65
"type": "void ()",
6+
"kind": "function",
77
"body": {
88
"file": "refs.c",
99
"start_line": 23,
@@ -12,77 +12,77 @@
1212
"comment": {},
1313
"refs": [
1414
{
15+
"name": "refs0",
1516
"kind": "calls",
1617
"entity_kind": "function",
17-
"name": "refs0",
1818
"line": 25
1919
},
2020
{
21+
"name": "refs1",
2122
"kind": "calls",
2223
"entity_kind": "function",
23-
"name": "refs1",
2424
"line": 26
2525
},
2626
{
27+
"name": "refs0",
2728
"kind": "calls",
2829
"entity_kind": "function",
29-
"name": "refs0",
3030
"line": 27
3131
},
3232
{
33+
"name": "refs1",
3334
"kind": "calls",
3435
"entity_kind": "function",
35-
"name": "refs1",
3636
"line": 28
3737
},
3838
{
39+
"name": "refs2",
3940
"kind": "calls",
4041
"entity_kind": "function",
41-
"name": "refs2",
4242
"line": 29
4343
},
4444
{
45+
"name": "refs1",
4546
"kind": "takes-address-of",
4647
"entity_kind": "function",
47-
"name": "refs1",
4848
"line": 29
4949
},
5050
{
51+
"name": "refs0",
5152
"kind": "calls",
5253
"entity_kind": "function",
53-
"name": "refs0",
5454
"line": 29
5555
},
5656
{
57+
"name": "refs0",
5758
"kind": "calls",
5859
"entity_kind": "function",
59-
"name": "refs0",
6060
"line": 30
6161
},
6262
{
63+
"name": "refs1",
6364
"kind": "calls",
6465
"entity_kind": "function",
65-
"name": "refs1",
6666
"line": 31
6767
},
6868
{
69+
"name": "refs0",
6970
"kind": "calls",
7071
"entity_kind": "function",
71-
"name": "refs0",
7272
"line": 32
7373
},
7474
{
75+
"name": "refs1",
7576
"kind": "calls",
7677
"entity_kind": "function",
77-
"name": "refs1",
7878
"line": 33
7979
}
8080
]
8181
},
8282
{
83-
"kind": "function",
8483
"name": "refs0",
8584
"type": "int ()",
85+
"kind": "function",
8686
"body": {
8787
"file": "refs.c",
8888
"start_line": 4,
@@ -95,9 +95,9 @@
9595
}
9696
},
9797
{
98-
"kind": "function",
9998
"name": "refs1",
10099
"type": "void ()",
100+
"kind": "function",
101101
"body": {
102102
"file": "refs.c",
103103
"start_line": 9,
@@ -106,9 +106,9 @@
106106
"comment": {}
107107
},
108108
{
109-
"kind": "function",
110109
"name": "refs2",
111110
"type": "void (void (*)(), int)",
111+
"kind": "function",
112112
"body": {
113113
"file": "refs.c",
114114
"start_line": 13,
@@ -117,9 +117,9 @@
117117
"comment": {}
118118
},
119119
{
120-
"kind": "function",
121120
"name": "refs3",
122121
"type": "void ()",
122+
"kind": "function",
123123
"body": {
124124
"file": "refs.c",
125125
"start_line": 17,
@@ -128,27 +128,27 @@
128128
"comment": {},
129129
"refs": [
130130
{
131+
"name": "refs2",
131132
"kind": "calls",
132133
"entity_kind": "function",
133-
"name": "refs2",
134134
"line": 19
135135
},
136136
{
137+
"name": "refs1",
137138
"kind": "takes-address-of",
138139
"entity_kind": "function",
139-
"name": "refs1",
140140
"line": 19
141141
},
142142
{
143+
"name": "refs0",
143144
"kind": "calls",
144145
"entity_kind": "function",
145-
"name": "refs0",
146146
"line": 19
147147
},
148148
{
149+
"name": "refs2",
149150
"kind": "takes-address-of",
150151
"entity_kind": "function",
151-
"name": "refs2",
152152
"line": 20
153153
}
154154
]

0 commit comments

Comments
 (0)