Performance and scale.

Defer edge creation.

Don't create edges until the count is known to avoid repeated allocate+
copy operatios.

Limit resolutions.

Allow only a single resolution condition set per target, and overwrite
intermediate results. Reduces memory and obviates allocations.

Propagate fewer conditions.

Instead of propagating notice conditions to parents in graph during
initial resolve, leave them on leaf node, and attach to ancestors in
the final walk. Reduces copies.

Parallelize resolutions.

Use goroutines, mutexes, and waitgroups to resolve branches of the
graph in parallel. Makes better use of available cores.

Don't accumulate resolutions inside non-containers.

During the final resolution walk, only attach actions to ancestors from
the root down until the 1st non-aggregate. Prevents an explosion of
copies in the lower levels of the graph.

Drop origin for scale.

Tracking the origin of every potential origin for every restricted
condition does not scale. By dropping origin, propagating from top
to bottom can prune many redundant paths avoiding an exponential
explosion.

Conditions as bitmask.

Use bit masks for license conditions and condition sets. Reduces maps
and allocations.

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: Ia2ec1b818de6122c239fbd0824754f1d65daffd3
diff --git a/tools/compliance/resolution.go b/tools/compliance/resolution.go
index 0865ecd..6f15ca3 100644
--- a/tools/compliance/resolution.go
+++ b/tools/compliance/resolution.go
@@ -32,7 +32,7 @@
 // resolve the restricted condition originating from the GPL code.
 type Resolution struct {
 	attachesTo, actsOn *TargetNode
-	cs                 *LicenseConditionSet
+	cs                 LicenseConditionSet
 }
 
 // AttachesTo returns the target node the resolution attaches to.
@@ -48,16 +48,16 @@
 }
 
 // Resolves returns the set of license condition the resolution satisfies.
-func (r Resolution) Resolves() *LicenseConditionSet {
-	return r.cs.Copy()
+func (r Resolution) Resolves() LicenseConditionSet {
+	return r.cs
 }
 
 // asString returns a string representation of the resolution.
 func (r Resolution) asString() string {
 	var sb strings.Builder
-	cl := r.cs.AsList()
-	sort.Sort(cl)
-	fmt.Fprintf(&sb, "%s -> %s -> %s", r.attachesTo.name, r.actsOn.name, cl.String())
+	names := r.cs.Names()
+	sort.Strings(names)
+	fmt.Fprintf(&sb, "%s -> %s{%s}", r.attachesTo.name, r.actsOn.name, strings.Join(names, ", "))
 	return sb.String()
 }
 
@@ -94,67 +94,32 @@
 
 // AllConditions returns the union of all license conditions resolved by any
 // element of the list.
-func (rl ResolutionList) AllConditions() *LicenseConditionSet {
-	result := newLicenseConditionSet()
+func (rl ResolutionList) AllConditions() LicenseConditionSet {
+	result := NewLicenseConditionSet()
 	for _, r := range rl {
-		result.AddSet(r.cs)
+		result = result.Union(r.cs)
 	}
 	return result
 }
 
 // ByName returns the sub-list of resolutions resolving conditions matching
 // `names`.
-func (rl ResolutionList) ByName(names ConditionNames) ResolutionList {
-	result := make(ResolutionList, 0, rl.CountByName(names))
+func (rl ResolutionList) Matching(conditions LicenseConditionSet) ResolutionList {
+	result := make(ResolutionList, 0, rl.CountMatching(conditions))
 	for _, r := range rl {
-		if r.Resolves().HasAnyByName(names) {
-			result = append(result, Resolution{r.attachesTo, r.actsOn, r.cs.ByName(names)})
+		if r.Resolves().MatchesAnySet(conditions) {
+			result = append(result, Resolution{r.attachesTo, r.actsOn, r.cs.MatchingAnySet(conditions)})
 		}
 	}
 	return result
 }
 
-// CountByName returns the number of resolutions resolving conditions matching
-// `names`.
-func (rl ResolutionList) CountByName(names ConditionNames) int {
+// CountMatching returns the number of resolutions resolving conditions matching
+// `conditions`.
+func (rl ResolutionList) CountMatching(conditions LicenseConditionSet) int {
 	c := 0
 	for _, r := range rl {
-		if r.Resolves().HasAnyByName(names) {
-			c++
-		}
-	}
-	return c
-}
-
-// CountConditionsByName returns a count of distinct resolution/conditions
-// pairs matching `names`.
-//
-// A single resolution might resolve multiple conditions matching `names`.
-func (rl ResolutionList) CountConditionsByName(names ConditionNames) int {
-	c := 0
-	for _, r := range rl {
-		c += r.Resolves().CountByName(names)
-	}
-	return c
-}
-
-// ByAttachesTo returns the sub-list of resolutions attached to `attachesTo`.
-func (rl ResolutionList) ByAttachesTo(attachesTo *TargetNode) ResolutionList {
-	result := make(ResolutionList, 0, rl.CountByActsOn(attachesTo))
-	for _, r := range rl {
-		if r.attachesTo == attachesTo {
-			result = append(result, r)
-		}
-	}
-	return result
-}
-
-// CountByAttachesTo returns the number of resolutions attached to
-// `attachesTo`.
-func (rl ResolutionList) CountByAttachesTo(attachesTo *TargetNode) int {
-	c := 0
-	for _, r := range rl {
-		if r.attachesTo == attachesTo {
+		if r.Resolves().MatchesAnySet(conditions) {
 			c++
 		}
 	}
@@ -182,27 +147,3 @@
 	}
 	return c
 }
-
-// ByOrigin returns the sub-list of resolutions resolving license conditions
-// originating at `origin`.
-func (rl ResolutionList) ByOrigin(origin *TargetNode) ResolutionList {
-	result := make(ResolutionList, 0, rl.CountByOrigin(origin))
-	for _, r := range rl {
-		if r.Resolves().HasAnyByOrigin(origin) {
-			result = append(result, Resolution{r.attachesTo, r.actsOn, r.cs.ByOrigin(origin)})
-		}
-	}
-	return result
-}
-
-// CountByOrigin returns the number of resolutions resolving license conditions
-// originating at `origin`.
-func (rl ResolutionList) CountByOrigin(origin *TargetNode) int {
-	c := 0
-	for _, r := range rl {
-		if r.Resolves().HasAnyByOrigin(origin) {
-			c++
-		}
-	}
-	return c
-}