aboutsummaryrefslogtreecommitdiff
path: root/context_test.go
diff options
context:
space:
mode:
authorColin Cross <ccross@android.com>2023-10-31 15:15:30 -0700
committerColin Cross <ccross@android.com>2023-11-01 15:15:15 -0700
commit95bec3331cc42db65c048a475b28022c7a303ec9 (patch)
treec138124ecd1358cf738e97248af77ff06d7d1eee /context_test.go
parent098c09334fefc9e061d061ec5cb491b4bbc83a4f (diff)
downloadblueprint-95bec3331cc42db65c048a475b28022c7a303ec9.tar.gz
Use strings instead of simpleNinjaStrings where possible
Storing every string without ninja variable references through simpleNinjaString costs 24 bytes and a heap allocation. 16 bytes is used for the ninjaString.str string, 8 bytes for the ninjaString.variables *[]variableReference. An additional 8 bytes is used for the resulting pointer into the heap. The vast majority of calls to simpleNinjaString originate in blueprint.parseBuildParams, which converts all of the parameters passed to ctx.Build into ninjaStrings. All together this was allocating 1.575 GB of *ninjaString objects. Add a parseNinjaOrSimpleStrings function that converts input strings into ninjaStrings if they have ninja variable references, but also returns a slice of plain strings for input strings without any ninja variable references. That still results in 1.39 GB of allocations just for the output string slice, so also add an optimization that reuses the input string slice as the output slice if all of the strings had no variable references. Plumb the resulting strings through everywhere that the []*ninjaStrings were used. This reduces the total memory allocations inside blueprint.parseBuildParams in my AOSP aosp_cf_x86_64_phone-userdebug build from 3.337 GB to 1.786 GB. Test: ninja_strings_test.go Change-Id: I51bc138a2a6b1cc7383c7df0a483ccb067ffa02b
Diffstat (limited to 'context_test.go')
-rw-r--r--context_test.go43
1 files changed, 18 insertions, 25 deletions
diff --git a/context_test.go b/context_test.go
index a10f681..521d163 100644
--- a/context_test.go
+++ b/context_test.go
@@ -1159,18 +1159,11 @@ func TestPackageIncludes(t *testing.T) {
}
func TestDeduplicateOrderOnlyDeps(t *testing.T) {
- outputs := func(names ...string) []*ninjaString {
- r := make([]*ninjaString, len(names))
- for i, name := range names {
- r[i] = simpleNinjaString(name)
- }
- return r
- }
b := func(output string, inputs []string, orderOnlyDeps []string) *buildDef {
return &buildDef{
- Outputs: outputs(output),
- Inputs: outputs(inputs...),
- OrderOnly: outputs(orderOnlyDeps...),
+ OutputStrings: []string{output},
+ InputStrings: inputs,
+ OrderOnlyStrings: orderOnlyDeps,
}
}
m := func(bs ...*buildDef) *moduleInfo {
@@ -1179,7 +1172,7 @@ func TestDeduplicateOrderOnlyDeps(t *testing.T) {
type testcase struct {
modules []*moduleInfo
expectedPhonys []*buildDef
- conversions map[string][]*ninjaString
+ conversions map[string][]string
}
testCases := []testcase{{
modules: []*moduleInfo{
@@ -1189,9 +1182,9 @@ func TestDeduplicateOrderOnlyDeps(t *testing.T) {
expectedPhonys: []*buildDef{
b("dedup-GKw-c0PwFokMUQ6T-TUmEWnZ4_VlQ2Qpgw-vCTT0-OQ", []string{"d"}, nil),
},
- conversions: map[string][]*ninjaString{
- "A": outputs("dedup-GKw-c0PwFokMUQ6T-TUmEWnZ4_VlQ2Qpgw-vCTT0-OQ"),
- "B": outputs("dedup-GKw-c0PwFokMUQ6T-TUmEWnZ4_VlQ2Qpgw-vCTT0-OQ"),
+ conversions: map[string][]string{
+ "A": []string{"dedup-GKw-c0PwFokMUQ6T-TUmEWnZ4_VlQ2Qpgw-vCTT0-OQ"},
+ "B": []string{"dedup-GKw-c0PwFokMUQ6T-TUmEWnZ4_VlQ2Qpgw-vCTT0-OQ"},
},
}, {
modules: []*moduleInfo{
@@ -1205,10 +1198,10 @@ func TestDeduplicateOrderOnlyDeps(t *testing.T) {
m(b("C", nil, []string{"a"})),
},
expectedPhonys: []*buildDef{b("dedup-ypeBEsobvcr6wjGzmiPcTaeG7_gUfE5yuYB3ha_uSLs", []string{"a"}, nil)},
- conversions: map[string][]*ninjaString{
- "A": outputs("dedup-ypeBEsobvcr6wjGzmiPcTaeG7_gUfE5yuYB3ha_uSLs"),
- "B": outputs("b"),
- "C": outputs("dedup-ypeBEsobvcr6wjGzmiPcTaeG7_gUfE5yuYB3ha_uSLs"),
+ conversions: map[string][]string{
+ "A": []string{"dedup-ypeBEsobvcr6wjGzmiPcTaeG7_gUfE5yuYB3ha_uSLs"},
+ "B": []string{"b"},
+ "C": []string{"dedup-ypeBEsobvcr6wjGzmiPcTaeG7_gUfE5yuYB3ha_uSLs"},
},
}, {
modules: []*moduleInfo{
@@ -1220,11 +1213,11 @@ func TestDeduplicateOrderOnlyDeps(t *testing.T) {
expectedPhonys: []*buildDef{
b("dedup--44g_C5MPySMYMOb1lLzwTRymLuXe4tNWQO4UFViBgM", []string{"a", "b"}, nil),
b("dedup-9F3lHN7zCZFVHkHogt17VAR5lkigoAdT9E_JZuYVP8E", []string{"a", "c"}, nil)},
- conversions: map[string][]*ninjaString{
- "A": outputs("dedup--44g_C5MPySMYMOb1lLzwTRymLuXe4tNWQO4UFViBgM"),
- "B": outputs("dedup--44g_C5MPySMYMOb1lLzwTRymLuXe4tNWQO4UFViBgM"),
- "C": outputs("dedup-9F3lHN7zCZFVHkHogt17VAR5lkigoAdT9E_JZuYVP8E"),
- "D": outputs("dedup-9F3lHN7zCZFVHkHogt17VAR5lkigoAdT9E_JZuYVP8E"),
+ conversions: map[string][]string{
+ "A": []string{"dedup--44g_C5MPySMYMOb1lLzwTRymLuXe4tNWQO4UFViBgM"},
+ "B": []string{"dedup--44g_C5MPySMYMOb1lLzwTRymLuXe4tNWQO4UFViBgM"},
+ "C": []string{"dedup-9F3lHN7zCZFVHkHogt17VAR5lkigoAdT9E_JZuYVP8E"},
+ "D": []string{"dedup-9F3lHN7zCZFVHkHogt17VAR5lkigoAdT9E_JZuYVP8E"},
},
}}
for index, tc := range testCases {
@@ -1253,7 +1246,7 @@ func TestDeduplicateOrderOnlyDeps(t *testing.T) {
find := func(k string) *buildDef {
for _, m := range tc.modules {
for _, b := range m.actionDefs.buildDefs {
- if reflect.DeepEqual(b.Outputs, outputs(k)) {
+ if reflect.DeepEqual(b.OutputStrings, []string{k}) {
return b
}
}
@@ -1265,7 +1258,7 @@ func TestDeduplicateOrderOnlyDeps(t *testing.T) {
if actual == nil {
t.Errorf("Couldn't find %s", k)
}
- if !reflect.DeepEqual(actual.OrderOnly, conversion) {
+ if !reflect.DeepEqual(actual.OrderOnlyStrings, conversion) {
t.Errorf("expected %s.OrderOnly = %v but got %v", k, conversion, actual.OrderOnly)
}
}