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
This commit is contained in:
Bob Badour 2022-01-10 18:44:59 -08:00
parent 67d8ae390a
commit 5446a6f8e1
6 changed files with 29 additions and 29 deletions

View File

@ -28,8 +28,8 @@ func NewLicenseConditionSet(conditions ...LicenseCondition) *LicenseConditionSet
// 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 @@ func (cs *LicenseConditionSet) Add(conditions ...LicenseCondition) {
}
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 @@ func (cs *LicenseConditionSet) AddSet(other *LicenseConditionSet) {
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 @@ func (cs *LicenseConditionSet) ByName(names ...ConditionNames) *LicenseCondition
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 @@ func (cs *LicenseConditionSet) ByOrigin(origin *TargetNode) *LicenseConditionSet
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) Count() int {
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 @@ func (cs *LicenseConditionSet) RemoveSet(other *LicenseConditionSet) {
// 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{}{}
}
}

View File

@ -387,12 +387,12 @@ type InstallMap struct {
// 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 @@ func (ea TargetEdgeAnnotations) AsList() []string {
// 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.

View File

@ -83,7 +83,7 @@ func ResolveTopDownConditions(lg *LicenseGraph) *ResolutionSet {
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 @@ func ResolveTopDownConditions(lg *LicenseGraph) *ResolutionSet {
}
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 @@ func resolveBottomUp(lg *LicenseGraph, priors map[*TargetNode]actionSet) *Resolu
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 @@ func resolveBottomUp(lg *LicenseGraph, priors map[*TargetNode]actionSet) *Resolu
delete(cmap, f)
}
if treatAsAggregate {
cmap[f] = true
cmap[f] = struct{}{}
}
// add all the conditions from all the dependencies

View File

@ -24,7 +24,7 @@ func ShippedNodes(lg *LicenseGraph) *TargetNodeSet {
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 @@ func ShippedNodes(lg *LicenseGraph) *TargetNodeSet {
return false
}
}
tset[tn] = true
tset[tn] = struct{}{}
return true
})

View File

@ -191,7 +191,7 @@ func addDependencies(edges *[]*dependencyEdge, target string, dependencies []*li
// 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})

View File

@ -117,10 +117,10 @@ func (rs *ResolutionSet) AttachesTo() TargetNodeList {
// 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 @@ func (rs *ResolutionSet) ActsOn() TargetNodeList {
// 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 @@ func (rs *ResolutionSet) ResolutionsByActsOn(actOn *TargetNode) ResolutionList {
// 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
}
}