Use struct{}
Using struct{}{} as the payload for set maps reduces memory use for
large sets.
Bug: 68860345
Bug: 151177513
Bug: 151953481
Test: m all
Test: m systemlicense
Test: m listshare; out/soong/host/linux-x86/bin/listshare ...
Test: m checkshare; out/soong/host/linux-x86/bin/checkshare ...
Test: m dumpgraph; out/soong/host/linux-x86/dumpgraph ...
Test: m dumpresolutions; out/soong/host/linux-x86/dumpresolutions ...
where ... is the path to the .meta_lic file for the system image. In my
case if
$ export PRODUCT=$(realpath $ANDROID_PRODUCT_OUT --relative-to=$PWD)
... can be expressed as:
${PRODUCT}/gen/META/lic_intermediates/${PRODUCT}/system.img.meta_lic
Change-Id: Ibc831ae80fc50f35e1000348fb28fc0167d0ebed
diff --git a/tools/compliance/conditionset.go b/tools/compliance/conditionset.go
index 1ad15ca..102e35a 100644
--- a/tools/compliance/conditionset.go
+++ b/tools/compliance/conditionset.go
@@ -28,8 +28,8 @@
// LicenseConditionSet describes a mutable set of immutable license conditions.
type LicenseConditionSet struct {
// conditions describes the set of license conditions i.e. (condition name, origin target) pairs
- // by mapping condition name -> origin target -> true.
- conditions map[string]map[*TargetNode]bool
+ // by mapping condition name -> origin target -> struct{}{}.
+ conditions map[string]map[*TargetNode]struct{}
}
// Add makes all `conditions` members of the set if they were not previously.
@@ -39,9 +39,9 @@
}
for _, lc := range conditions {
if _, ok := cs.conditions[lc.name]; !ok {
- cs.conditions[lc.name] = make(map[*TargetNode]bool)
+ cs.conditions[lc.name] = make(map[*TargetNode]struct{})
}
- cs.conditions[lc.name][lc.origin] = true
+ cs.conditions[lc.name][lc.origin] = struct{}{}
}
}
@@ -55,7 +55,7 @@
continue
}
if _, ok := cs.conditions[name]; !ok {
- cs.conditions[name] = make(map[*TargetNode]bool)
+ cs.conditions[name] = make(map[*TargetNode]struct{})
}
for origin := range origins {
cs.conditions[name][origin] = other.conditions[name][origin]
@@ -69,9 +69,9 @@
for _, cn := range names {
for _, name := range cn {
if origins, ok := cs.conditions[name]; ok {
- other.conditions[name] = make(map[*TargetNode]bool)
+ other.conditions[name] = make(map[*TargetNode]struct{})
for origin := range origins {
- other.conditions[name][origin] = true
+ other.conditions[name][origin] = struct{}{}
}
}
}
@@ -111,8 +111,8 @@
other := newLicenseConditionSet()
for name, origins := range cs.conditions {
if _, ok := origins[origin]; ok {
- other.conditions[name] = make(map[*TargetNode]bool)
- other.conditions[name][origin] = true
+ other.conditions[name] = make(map[*TargetNode]struct{})
+ other.conditions[name][origin] = struct{}{}
}
}
return other
@@ -172,7 +172,7 @@
func (cs *LicenseConditionSet) Copy() *LicenseConditionSet {
other := newLicenseConditionSet()
for name := range cs.conditions {
- other.conditions[name] = make(map[*TargetNode]bool)
+ other.conditions[name] = make(map[*TargetNode]struct{})
for origin := range cs.conditions[name] {
other.conditions[name][origin] = cs.conditions[name][origin]
}
@@ -241,16 +241,16 @@
// newLicenseConditionSet constructs a set of `conditions`.
func newLicenseConditionSet() *LicenseConditionSet {
- return &LicenseConditionSet{make(map[string]map[*TargetNode]bool)}
+ return &LicenseConditionSet{make(map[string]map[*TargetNode]struct{})}
}
// add changes the set to include each element of `conditions` originating at `origin`.
func (cs *LicenseConditionSet) add(origin *TargetNode, conditions ...string) {
for _, name := range conditions {
if _, ok := cs.conditions[name]; !ok {
- cs.conditions[name] = make(map[*TargetNode]bool)
+ cs.conditions[name] = make(map[*TargetNode]struct{})
}
- cs.conditions[name][origin] = true
+ cs.conditions[name][origin] = struct{}{}
}
}
diff --git a/tools/compliance/graph.go b/tools/compliance/graph.go
index 9dcfa66..f26e7e8 100644
--- a/tools/compliance/graph.go
+++ b/tools/compliance/graph.go
@@ -387,12 +387,12 @@
// Annotations typically distinguish between static linkage versus dynamic
// versus tools that are used at build time but are not linked in any way.
type TargetEdgeAnnotations struct {
- annotations map[string]bool
+ annotations map[string]struct{}
}
// newEdgeAnnotations creates a new instance of TargetEdgeAnnotations.
func newEdgeAnnotations() TargetEdgeAnnotations {
- return TargetEdgeAnnotations{make(map[string]bool)}
+ return TargetEdgeAnnotations{make(map[string]struct{})}
}
// HasAnnotation returns true if an annotation `ann` is in the set.
@@ -439,7 +439,7 @@
// TargetNodeSet describes a set of distinct nodes in a license graph.
type TargetNodeSet struct {
- nodes map[*TargetNode]bool
+ nodes map[*TargetNode]struct{}
}
// Contains returns true when `target` is an element of the set.
diff --git a/tools/compliance/policy/resolve.go b/tools/compliance/policy/resolve.go
index 58547f8..c107520 100644
--- a/tools/compliance/policy/resolve.go
+++ b/tools/compliance/policy/resolve.go
@@ -83,7 +83,7 @@
rmap := make(map[*TargetNode]actionSet)
// cmap contains the set of targets walked as pure aggregates. i.e. containers
- cmap := make(map[*TargetNode]bool)
+ cmap := make(map[*TargetNode]struct{})
var walk func(fnode *TargetNode, cs *LicenseConditionSet, treatAsAggregate bool)
@@ -93,7 +93,7 @@
}
rmap[fnode].add(fnode, cs)
if treatAsAggregate {
- cmap[fnode] = true
+ cmap[fnode] = struct{}{}
}
// add conditions attached to `fnode`
cs = cs.Copy()
@@ -179,7 +179,7 @@
rs := newResolutionSet()
// cmap contains an entry for every target that was previously walked as a pure aggregate only.
- cmap := make(map[string]bool)
+ cmap := make(map[string]struct{})
var walk func(f string, treatAsAggregate bool) actionSet
@@ -205,7 +205,7 @@
delete(cmap, f)
}
if treatAsAggregate {
- cmap[f] = true
+ cmap[f] = struct{}{}
}
// add all the conditions from all the dependencies
diff --git a/tools/compliance/policy/shipped.go b/tools/compliance/policy/shipped.go
index 74eb343..6fbbbd6 100644
--- a/tools/compliance/policy/shipped.go
+++ b/tools/compliance/policy/shipped.go
@@ -24,7 +24,7 @@
return shipped
}
- tset := make(map[*TargetNode]bool)
+ tset := make(map[*TargetNode]struct{})
WalkTopDown(lg, func(lg *LicenseGraph, tn *TargetNode, path TargetEdgePath) bool {
if _, alreadyWalked := tset[tn]; alreadyWalked {
@@ -35,7 +35,7 @@
return false
}
}
- tset[tn] = true
+ tset[tn] = struct{}{}
return true
})
diff --git a/tools/compliance/readgraph.go b/tools/compliance/readgraph.go
index face775..c88b3e6 100644
--- a/tools/compliance/readgraph.go
+++ b/tools/compliance/readgraph.go
@@ -191,7 +191,7 @@
// look up a common constant annotation string from a small map
// instead of creating 1000's of copies of the same 3 strings.
if ann, ok := RecognizedAnnotations[a]; ok {
- annotations.annotations[ann] = true
+ annotations.annotations[ann] = struct{}{}
}
}
*edges = append(*edges, &dependencyEdge{target, dependency, annotations})
diff --git a/tools/compliance/resolutionset.go b/tools/compliance/resolutionset.go
index ea49db9..29f3212 100644
--- a/tools/compliance/resolutionset.go
+++ b/tools/compliance/resolutionset.go
@@ -117,10 +117,10 @@
// ActsOn identifies the list of targets to act on (share, give notice etc.)
// to resolve conditions. (unordered)
func (rs *ResolutionSet) ActsOn() TargetNodeList {
- tset := make(map[*TargetNode]bool)
+ tset := make(map[*TargetNode]struct{})
for _, as := range rs.resolutions {
for actsOn := range as {
- tset[actsOn] = true
+ tset[actsOn] = struct{}{}
}
}
targets := make(TargetNodeList, 0, len(tset))
@@ -133,12 +133,12 @@
// Origins identifies the list of targets originating conditions to resolve.
// (unordered)
func (rs *ResolutionSet) Origins() TargetNodeList {
- tset := make(map[*TargetNode]bool)
+ tset := make(map[*TargetNode]struct{})
for _, as := range rs.resolutions {
for _, cs := range as {
for _, origins := range cs.conditions {
for origin := range origins {
- tset[origin] = true
+ tset[origin] = struct{}{}
}
}
}
@@ -189,11 +189,11 @@
// AttachesToByOrigin identifies the list of targets requiring action to
// resolve conditions originating at `origin`. (unordered)
func (rs *ResolutionSet) AttachesToByOrigin(origin *TargetNode) TargetNodeList {
- tset := make(map[*TargetNode]bool)
+ tset := make(map[*TargetNode]struct{})
for attachesTo, as := range rs.resolutions {
for _, cs := range as {
if cs.HasAnyByOrigin(origin) {
- tset[attachesTo] = true
+ tset[attachesTo] = struct{}{}
break
}
}