Skip to content

Commit c0009df

Browse files
authored
Ignore complexity of children in maps/slices (#382)
When considering inlining maps/slices/arrays, apply a fixed cost and ignore the cost of the children. If children are too expensive, they will not be inlined, but it shouldn't affect whether the map itself is inlined. This only really applies when a map or slice type is aliased, otherwise it will not be considered for inlining. Fixes #381
1 parent 901ed00 commit c0009df

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

Diff for: _generated/def.go

+16
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,19 @@ type NumberJSONSample struct {
308308
Map map[string]json.Number
309309
OE json.Number `msg:",omitempty"`
310310
}
311+
312+
type Flobbity struct {
313+
A Flobs `msg:"a,omitempty"`
314+
B Flobs `msg:"b,omitempty"`
315+
}
316+
317+
type Flobs []Flob
318+
319+
type Flob struct {
320+
X Numberwang `msg:"x"`
321+
Y int8 `msg:"y"`
322+
Z int8 `msg:"z"`
323+
W int32 `msg:"w"`
324+
}
325+
326+
type Numberwang int8

Diff for: gen/elem.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,10 @@ func (a *Array) Copy() Elem {
268268
return &b
269269
}
270270

271-
func (a *Array) Complexity() int { return 1 + a.Els.Complexity() }
271+
func (a *Array) Complexity() int {
272+
// We consider the complexity constant and leave the children to decide on their own.
273+
return 2
274+
}
272275

273276
// ZeroExpr returns the zero/empty expression or empty string if not supported. Unsupported for this case.
274277
func (a *Array) ZeroExpr() string { return "" }
@@ -313,7 +316,10 @@ func (m *Map) Copy() Elem {
313316
return &g
314317
}
315318

316-
func (m *Map) Complexity() int { return 2 + m.Value.Complexity() }
319+
func (m *Map) Complexity() int {
320+
// Complexity of maps are considered constant. Children should decide on their own.
321+
return 3
322+
}
317323

318324
// ZeroExpr returns the zero/empty expression or empty string if not supported. Always "nil" for this case.
319325
func (m *Map) ZeroExpr() string { return "nil" }
@@ -360,7 +366,8 @@ func (s *Slice) Copy() Elem {
360366
}
361367

362368
func (s *Slice) Complexity() int {
363-
return 1 + s.Els.Complexity()
369+
// We leave the inlining decision to the slice children.
370+
return 2
364371
}
365372

366373
// ZeroExpr returns the zero/empty expression or empty string if not supported. Always "nil" for this case.

0 commit comments

Comments
 (0)