this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

internal/core/adt: remove Env and CloseInfo from StructInfo

These are only needed for tools/trim, but it can reconstruct a conjunct
by iterating over LeafConjuncts to find a match by StructLit instead.

Now that StructInfo is much smaller, we also stop using pointers
for slices of it, which significantly reduces the number of allocations.
In the future we might add more small fields, so we keep the type.

│ old │ new │
│ B/op │ B/op vs base │
Roman 6.471Gi ± ∞ ¹ 6.383Gi ± ∞ ¹ -1.35% (p=1.000 n=1)

│ old │ new │
│ allocs/op │ allocs/op vs base │
Roman 67.97M ± ∞ ¹ 66.58M ± ∞ ¹ -2.04% (p=1.000 n=1)

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: If7196b76fd500ce314a3febfe009af9d825ad0bd
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1226965
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+31 -22
+4 -11
internal/core/adt/composite.go
··· 258 258 259 259 // Structs is a slice of struct literals that contributed to this value. 260 260 // This information is used to compute the topological sort of arcs. 261 - Structs []*StructInfo 261 + Structs []StructInfo 262 262 } 263 263 264 264 func deref(v *Vertex) *Vertex { ··· 478 478 479 479 type StructInfo struct { 480 480 *StructLit 481 - 482 - Env *Environment 483 - 484 - CloseInfo 485 481 486 482 // Repeats tracks how many additional times this struct appeared via [Vertex.AddStruct]. 487 483 // This is used by toposort to give proper weight to repeated structs. ··· 1414 1410 } 1415 1411 1416 1412 func (v *Vertex) AddStruct(s *StructLit, env *Environment, ci CloseInfo) { 1417 - for _, t := range v.Structs { 1413 + for i, t := range v.Structs { 1418 1414 if t.StructLit == s { 1419 - t.Repeats++ 1415 + v.Structs[i].Repeats++ 1420 1416 return 1421 1417 } 1422 1418 } 1423 1419 info := StructInfo{ 1424 1420 StructLit: s, 1425 - Env: env, 1426 - CloseInfo: ci, 1427 1421 } 1428 - t := &info 1429 - v.Structs = append(v.Structs, t) 1422 + v.Structs = append(v.Structs, info) 1430 1423 } 1431 1424 1432 1425 // Path computes the sequence of Features leading from the root to of the
+2 -2
internal/core/export/expr.go
··· 306 306 values *adt.Vertex 307 307 embed []ast.Expr 308 308 conjuncts []ast.Expr 309 - structs []*adt.StructInfo 309 + structs []adt.StructInfo 310 310 fields map[adt.Feature]field 311 311 attrs []*ast.Attribute 312 312 hasEllipsis bool ··· 377 377 return 378 378 } 379 379 // Used for sorting. 380 - e.structs = append(e.structs, &adt.StructInfo{StructLit: x, Env: env}) 380 + e.structs = append(e.structs, adt.StructInfo{StructLit: x}) 381 381 382 382 env = &adt.Environment{Up: env, Vertex: e.node()} 383 383
+1 -1
internal/core/export/toposort.go
··· 29 29 return toposort.VertexFeatures(c, v) 30 30 } 31 31 32 - func extractFeatures(in []*adt.StructInfo) (a [][]adt.Feature) { 32 + func extractFeatures(in []adt.StructInfo) (a [][]adt.Feature) { 33 33 // Calculate total entries accounting for repeats. 34 34 // Total occurrences = 1 + Repeats for each StructInfo. 35 35 totalEntries := 0
+2 -5
internal/core/toposort/vertex.go
··· 145 145 ) 146 146 147 147 type structMeta struct { 148 - structInfo *adt.StructInfo 148 + structInfo adt.StructInfo 149 149 pos token.Pos 150 150 151 151 // Should this struct be considered to be part of an explicit ··· 154 154 } 155 155 156 156 func (sMeta *structMeta) String() string { 157 - var sl *adt.StructLit 158 - if sMeta.structInfo != nil { 159 - sl = sMeta.structInfo.StructLit 160 - } 157 + sl := sMeta.structInfo.StructLit 161 158 return fmt.Sprintf("{%p sl:%p %v (explicit? %v)}", 162 159 sMeta, sl, sMeta.pos, sMeta.isExplicit) 163 160 }
+22 -3
tools/trim/trimv3.go
··· 616 616 }, nil) 617 617 } 618 618 619 + // findConjunctForStruct finds the conjunct in v that contains the given StructLit 620 + // and returns a new conjunct with the proper child environment and close info. 621 + func (t *trimmerV3) findConjunctForStruct(v *adt.Vertex, s *adt.StructLit) (adt.Conjunct, bool) { 622 + for c := range v.LeafConjuncts() { 623 + if expr := c.Expr(); expr == s { 624 + childEnv := &adt.Environment{ 625 + Up: c.Env, 626 + Vertex: v, 627 + } 628 + return adt.MakeConjunct(childEnv, s, c.CloseInfo), true 629 + } 630 + } 631 + return adt.Conjunct{}, false 632 + } 633 + 619 634 // Once we have identified, and masked out, call expressions, 620 635 // embeddings, patterns, and disjunctions, we can finally work 621 636 // recursively through the vertices, testing their conjuncts to find ··· 650 665 Parent: v.Parent, 651 666 Label: v.Label, 652 667 } 653 - c := adt.MakeConjunct(si.Env, si.StructLit, si.CloseInfo) 668 + c, found := t.findConjunctForStruct(v, si.StructLit) 669 + if !found { 670 + t.logf("could not find conjunct for struct lit, skipping: %#v", si) 671 + continue 672 + } 654 673 v1.InsertConjunct(c) 655 674 v1.Finalize(t.ctx) 656 - t.logf("exploring disj struct lit %p (src %v): start", si, src.Pos()) 675 + t.logf("exploring disj struct lit %#v (src %v): start", si, src.Pos()) 657 676 t.findRedundancies(v1, keepAll) 658 - t.logf("exploring disj struct lit %p (src %v): end", si, src.Pos()) 677 + t.logf("exploring disj struct lit %#v (src %v): end", si, src.Pos()) 659 678 } 660 679 } 661 680