From 623df388782dcfe70f4f147cb24197e535f9c89b Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Wed, 21 Jan 2026 15:48:25 +0100 Subject: 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. --- pkg/codesearch/codesearch.go | 9 ++++---- pkg/codesearch/database.go | 14 ++++++------ pkg/codesearch/testdata/mm/refs.c.json | 4 ++-- pkg/codesearch/testdata/refs.c.json | 40 +++++++++++++++++----------------- pkg/codesearch/testdata/source0.c.json | 40 +++++++++++++++++----------------- pkg/codesearch/testdata/source1.c.json | 2 +- pkg/codesearch/testdata/source2.c.json | 2 +- 7 files changed, 56 insertions(+), 55 deletions(-) (limited to 'pkg/codesearch') diff --git a/pkg/codesearch/codesearch.go b/pkg/codesearch/codesearch.go index 08018a403..33a87e712 100644 --- a/pkg/codesearch/codesearch.go +++ b/pkg/codesearch/codesearch.go @@ -266,6 +266,7 @@ func (index *Index) FindReferences(contextFile, name, srcPrefix string, contextL if srcPrefix != "" { srcPrefix = filepath.Clean(srcPrefix) } + contextLines = min(contextLines, 10000) totalCount := 0 var results []ReferenceInfo for _, def := range index.db.Definitions { @@ -289,8 +290,8 @@ func (index *Index) FindReferences(contextFile, name, srcPrefix string, contextL if contextLines > 0 { lines := LineRange{ File: def.Body.File, - StartLine: max(def.Body.StartLine, ref.Line-contextLines), - EndLine: min(def.Body.EndLine, ref.Line+contextLines), + StartLine: max(def.Body.StartLine, uint32(max(0, int(ref.Line)-contextLines))), + EndLine: min(def.Body.EndLine, ref.Line+uint32(contextLines)), } var err error snippet, err = index.formatSource(lines, true) @@ -303,7 +304,7 @@ func (index *Index) FindReferences(contextFile, name, srcPrefix string, contextL ReferencingEntityName: def.Name, ReferenceKind: ref.Kind.String(), SourceFile: def.Body.File, - SourceLine: ref.Line, + SourceLine: int(ref.Line), SourceSnippet: snippet, }) } @@ -342,7 +343,7 @@ func (index *Index) formatSource(lines LineRange, includeLines bool) (string, er if !osutil.IsExist(file) { continue } - return formatSourceFile(file, lines.StartLine, lines.EndLine, includeLines) + return formatSourceFile(file, int(lines.StartLine), int(lines.EndLine), includeLines) } return "", fmt.Errorf("codesearch: can't find %q file in any of %v", lines.File, index.srcDirs) } diff --git a/pkg/codesearch/database.go b/pkg/codesearch/database.go index eed853aaf..4abd9b690 100644 --- a/pkg/codesearch/database.go +++ b/pkg/codesearch/database.go @@ -24,9 +24,9 @@ type Database struct { } type Definition struct { - Kind EntityKind `json:"kind,omitempty"` Name string `json:"name,omitempty"` Type string `json:"type,omitempty"` + Kind EntityKind `json:"kind,omitempty"` IsStatic bool `json:"is_static,omitempty"` Body LineRange `json:"body,omitempty"` Comment LineRange `json:"comment,omitempty"` @@ -34,16 +34,16 @@ type Definition struct { } type Reference struct { + Name string `json:"name,omitempty"` Kind RefKind `json:"kind,omitempty"` EntityKind EntityKind `json:"entity_kind,omitempty"` - Name string `json:"name,omitempty"` - Line int `json:"line,omitempty"` + Line uint32 `json:"line,omitempty"` } type LineRange struct { File string `json:"file,omitempty"` - StartLine int `json:"start_line,omitempty"` - EndLine int `json:"end_line,omitempty"` + StartLine uint32 `json:"start_line,omitempty"` + EndLine uint32 `json:"end_line,omitempty"` } type EntityKind uint8 @@ -167,9 +167,9 @@ func (db *Database) Merge(other *Database, v *clangtool.Verifier) { } db.mergeCache[id] = def db.reverseCache[def] = id - v.LineRange(def.Body.File, def.Body.StartLine, def.Body.EndLine) + v.LineRange(def.Body.File, int(def.Body.StartLine), int(def.Body.EndLine)) if def.Comment.File != "" { - v.LineRange(def.Comment.File, def.Comment.StartLine, def.Comment.EndLine) + v.LineRange(def.Comment.File, int(def.Comment.StartLine), int(def.Comment.EndLine)) } db.intern(&def.Name) db.intern(&def.Type) diff --git a/pkg/codesearch/testdata/mm/refs.c.json b/pkg/codesearch/testdata/mm/refs.c.json index 09ac87f98..50aa676be 100644 --- a/pkg/codesearch/testdata/mm/refs.c.json +++ b/pkg/codesearch/testdata/mm/refs.c.json @@ -1,9 +1,9 @@ { "definitions": [ { - "kind": "function", "name": "ref_in_mm", "type": "void ()", + "kind": "function", "body": { "file": "mm/refs.c", "start_line": 3, @@ -12,9 +12,9 @@ "comment": {}, "refs": [ { + "name": "refs2", "kind": "calls", "entity_kind": "function", - "name": "refs2", "line": 5 } ] diff --git a/pkg/codesearch/testdata/refs.c.json b/pkg/codesearch/testdata/refs.c.json index 289ce7c30..d8a1034e2 100644 --- a/pkg/codesearch/testdata/refs.c.json +++ b/pkg/codesearch/testdata/refs.c.json @@ -1,9 +1,9 @@ { "definitions": [ { - "kind": "function", "name": "long_func_with_ref", "type": "void ()", + "kind": "function", "body": { "file": "refs.c", "start_line": 23, @@ -12,77 +12,77 @@ "comment": {}, "refs": [ { + "name": "refs0", "kind": "calls", "entity_kind": "function", - "name": "refs0", "line": 25 }, { + "name": "refs1", "kind": "calls", "entity_kind": "function", - "name": "refs1", "line": 26 }, { + "name": "refs0", "kind": "calls", "entity_kind": "function", - "name": "refs0", "line": 27 }, { + "name": "refs1", "kind": "calls", "entity_kind": "function", - "name": "refs1", "line": 28 }, { + "name": "refs2", "kind": "calls", "entity_kind": "function", - "name": "refs2", "line": 29 }, { + "name": "refs1", "kind": "takes-address-of", "entity_kind": "function", - "name": "refs1", "line": 29 }, { + "name": "refs0", "kind": "calls", "entity_kind": "function", - "name": "refs0", "line": 29 }, { + "name": "refs0", "kind": "calls", "entity_kind": "function", - "name": "refs0", "line": 30 }, { + "name": "refs1", "kind": "calls", "entity_kind": "function", - "name": "refs1", "line": 31 }, { + "name": "refs0", "kind": "calls", "entity_kind": "function", - "name": "refs0", "line": 32 }, { + "name": "refs1", "kind": "calls", "entity_kind": "function", - "name": "refs1", "line": 33 } ] }, { - "kind": "function", "name": "refs0", "type": "int ()", + "kind": "function", "body": { "file": "refs.c", "start_line": 4, @@ -95,9 +95,9 @@ } }, { - "kind": "function", "name": "refs1", "type": "void ()", + "kind": "function", "body": { "file": "refs.c", "start_line": 9, @@ -106,9 +106,9 @@ "comment": {} }, { - "kind": "function", "name": "refs2", "type": "void (void (*)(), int)", + "kind": "function", "body": { "file": "refs.c", "start_line": 13, @@ -117,9 +117,9 @@ "comment": {} }, { - "kind": "function", "name": "refs3", "type": "void ()", + "kind": "function", "body": { "file": "refs.c", "start_line": 17, @@ -128,27 +128,27 @@ "comment": {}, "refs": [ { + "name": "refs2", "kind": "calls", "entity_kind": "function", - "name": "refs2", "line": 19 }, { + "name": "refs1", "kind": "takes-address-of", "entity_kind": "function", - "name": "refs1", "line": 19 }, { + "name": "refs0", "kind": "calls", "entity_kind": "function", - "name": "refs0", "line": 19 }, { + "name": "refs2", "kind": "takes-address-of", "entity_kind": "function", - "name": "refs2", "line": 20 } ] diff --git a/pkg/codesearch/testdata/source0.c.json b/pkg/codesearch/testdata/source0.c.json index 1732d7c97..c2c2de3e4 100644 --- a/pkg/codesearch/testdata/source0.c.json +++ b/pkg/codesearch/testdata/source0.c.json @@ -1,8 +1,8 @@ { "definitions": [ { - "kind": "enum", "name": "some_enum", + "kind": "enum", "body": { "file": "source0.h", "start_line": 45, @@ -11,9 +11,9 @@ "comment": {} }, { - "kind": "function", "name": "close", "type": "int ()", + "kind": "function", "body": { "file": "source0.c", "start_line": 19, @@ -22,9 +22,9 @@ "comment": {} }, { - "kind": "function", "name": "func_accepting_a_struct", "type": "int (struct some_struct *)", + "kind": "function", "body": { "file": "source0.c", "start_line": 29, @@ -33,35 +33,35 @@ "comment": {}, "refs": [ { + "name": "some_struct", "kind": "uses", "entity_kind": "struct", - "name": "some_struct", "line": 29 }, { + "name": "some_struct_t", "kind": "uses", "entity_kind": "typedef", - "name": "some_struct_t", "line": 31 }, { + "name": "some_struct", "kind": "uses", "entity_kind": "struct", - "name": "some_struct", "line": 31 }, { + "name": "some_union", "kind": "uses", "entity_kind": "union", - "name": "some_union", "line": 32 } ] }, { - "kind": "function", "name": "func_in_header", "type": "int ()", + "kind": "function", "is_static": true, "body": { "file": "source0.h", @@ -71,9 +71,9 @@ "comment": {} }, { - "kind": "function", "name": "function_with_comment_in_header", "type": "void ()", + "kind": "function", "body": { "file": "source0.c", "start_line": 24, @@ -82,17 +82,17 @@ "comment": {}, "refs": [ { + "name": "same_name_in_several_files", "kind": "calls", "entity_kind": "function", - "name": "same_name_in_several_files", "line": 26 } ] }, { - "kind": "function", "name": "open", "type": "int ()", + "kind": "function", "body": { "file": "source0.c", "start_line": 11, @@ -105,8 +105,8 @@ } }, { - "kind": "struct", "name": "another_struct", + "kind": "struct", "body": { "file": "source0.h", "start_line": 36, @@ -115,8 +115,8 @@ "comment": {} }, { - "kind": "struct", "name": "some_struct", + "kind": "struct", "body": { "file": "source0.h", "start_line": 17, @@ -125,8 +125,8 @@ "comment": {} }, { - "kind": "struct", "name": "some_struct_with_a_comment", + "kind": "struct", "body": { "file": "source0.h", "start_line": 24, @@ -139,8 +139,8 @@ } }, { - "kind": "struct", "name": "struct_in_c_file", + "kind": "struct", "body": { "file": "source0.c", "start_line": 6, @@ -149,8 +149,8 @@ "comment": {} }, { - "kind": "typedef", "name": "another_struct_t", + "kind": "typedef", "body": { "file": "source0.h", "start_line": 36, @@ -159,8 +159,8 @@ "comment": {} }, { - "kind": "typedef", "name": "some_enum_t", + "kind": "typedef", "body": { "file": "source0.h", "start_line": 50, @@ -169,8 +169,8 @@ "comment": {} }, { - "kind": "typedef", "name": "some_struct_t", + "kind": "typedef", "body": { "file": "source0.h", "start_line": 22, @@ -179,8 +179,8 @@ "comment": {} }, { - "kind": "typedef", "name": "typedefed_struct_t", + "kind": "typedef", "body": { "file": "source0.h", "start_line": 32, @@ -189,8 +189,8 @@ "comment": {} }, { - "kind": "union", "name": "some_union", + "kind": "union", "body": { "file": "source0.h", "start_line": 40, diff --git a/pkg/codesearch/testdata/source1.c.json b/pkg/codesearch/testdata/source1.c.json index 72278a191..9a90d789f 100644 --- a/pkg/codesearch/testdata/source1.c.json +++ b/pkg/codesearch/testdata/source1.c.json @@ -1,9 +1,9 @@ { "definitions": [ { - "kind": "function", "name": "same_name_in_several_files", "type": "void ()", + "kind": "function", "is_static": true, "body": { "file": "source1.c", diff --git a/pkg/codesearch/testdata/source2.c.json b/pkg/codesearch/testdata/source2.c.json index 4407152db..5e75950c0 100644 --- a/pkg/codesearch/testdata/source2.c.json +++ b/pkg/codesearch/testdata/source2.c.json @@ -1,9 +1,9 @@ { "definitions": [ { - "kind": "function", "name": "same_name_in_several_files", "type": "void ()", + "kind": "function", "body": { "file": "source2.c", "start_line": 4, -- cgit mrf-deployment