go-jsonnet/ast/identifier_set.go
Leandro López eeca44cd27 Allocate resulting slice size instead of dynamically growing it
We know for sure how big the resulting slice will be, so instead of
having Go perform checks and reallocations whenever the slice grows
too small by using `append` we calculate the resulting size earlier
and directly index each element into the output slice. This result in
only two allocations all the time, no matter how big the resulting
slice will be. With it also come big performance improvements:

name                                        old time/op    new time/op    delta
ToOrderedSlice/1_unique_identifiers-8          173ns ±10%     150ns ± 4%  -13.16%  (p=0.000 n=10+9)
ToOrderedSlice/10_unique_identifiers-8        1.24µs ± 6%    0.51µs ± 5%  -58.95%  (p=0.000 n=9+9)
ToOrderedSlice/100_unique_identifiers-8       20.4µs ±20%     4.0µs ± 5%  -80.25%  (p=0.000 n=10+9)
ToOrderedSlice/1000_unique_identifiers-8       253µs ± 9%      40µs ± 5%  -84.19%  (p=0.000 n=10+10)
ToOrderedSlice/10000_unique_identifiers-8     3.44ms ± 9%    0.37ms ± 5%  -89.32%  (p=0.000 n=9+9)
ToOrderedSlice/100000_unique_identifiers-8    48.6ms ±13%     3.6ms ± 4%  -92.49%  (p=0.000 n=10+10)

name                                        old alloc/op   new alloc/op   delta
ToOrderedSlice/1_unique_identifiers-8          40.0B ± 0%     40.0B ± 0%     ~     (all equal)
ToOrderedSlice/10_unique_identifiers-8          520B ± 0%      184B ± 0%  -64.62%  (p=0.000 n=10+10)
ToOrderedSlice/100_unique_identifiers-8       4.10kB ± 0%    1.82kB ± 0%  -55.75%  (p=0.000 n=10+10)
ToOrderedSlice/1000_unique_identifiers-8      32.8kB ± 0%    16.4kB ± 0%  -49.95%  (p=0.000 n=10+10)
ToOrderedSlice/10000_unique_identifiers-8      826kB ± 0%     164kB ± 0%  -80.16%  (p=0.000 n=10+10)
ToOrderedSlice/100000_unique_identifiers-8    9.25MB ± 0%    1.61MB ± 0%  -82.64%  (p=0.000 n=10+10)

name                                        old allocs/op  new allocs/op  delta
ToOrderedSlice/1_unique_identifiers-8           2.00 ± 0%      2.00 ± 0%     ~     (all equal)
ToOrderedSlice/10_unique_identifiers-8          6.00 ± 0%      2.00 ± 0%  -66.67%  (p=0.000 n=10+10)
ToOrderedSlice/100_unique_identifiers-8         9.00 ± 0%      2.00 ± 0%  -77.78%  (p=0.000 n=10+10)
ToOrderedSlice/1000_unique_identifiers-8        12.0 ± 0%       2.0 ± 0%  -83.33%  (p=0.000 n=10+10)
ToOrderedSlice/10000_unique_identifiers-8       21.0 ± 0%       2.0 ± 0%  -90.48%  (p=0.000 n=10+10)
ToOrderedSlice/100000_unique_identifiers-8      31.0 ± 0%       2.0 ± 0%  -93.55%  (p=0.000 n=10+10)

For ToSLice we use the same changes except the sort operation. In this
case the results are even more impressive: allocations are stable at
just 1 per operation, regardless of set size, and performance is
orders of magnitude faster:

name                                 old time/op    new time/op    delta
ToSlice/1_unique_identifiers-8         83.0ns ± 1%    92.4ns ± 0%    +11.37%  (p=0.000 n=9+8)
ToSlice/10_unique_identifiers-8         295ns ± 4%     749ns ± 3%   +154.21%  (p=0.000 n=10+10)
ToSlice/100_unique_identifiers-8       2.33µs ± 2%    4.28µs ± 2%    +83.62%  (p=0.000 n=9+8)
ToSlice/1000_unique_identifiers-8      25.4µs ±10%    34.7µs ± 1%    +36.51%  (p=0.000 n=10+10)
ToSlice/10000_unique_identifiers-8      215µs ± 2%     543µs ± 1%   +152.15%  (p=0.000 n=9+10)
ToSlice/100000_unique_identifiers-8    2.05ms ± 2%    7.17ms ± 1%   +249.53%  (p=0.000 n=9+9)

name                                 old alloc/op   new alloc/op   delta
ToSlice/1_unique_identifiers-8          16.0B ± 0%     16.0B ± 0%       ~     (all equal)
ToSlice/10_unique_identifiers-8          160B ± 0%      496B ± 0%   +210.00%  (p=0.000 n=10+10)
ToSlice/100_unique_identifiers-8       1.79kB ± 0%    4.08kB ± 0%   +127.68%  (p=0.000 n=10+10)
ToSlice/1000_unique_identifiers-8      16.4kB ± 0%    32.8kB ± 0%    +99.90%  (p=0.000 n=10+10)
ToSlice/10000_unique_identifiers-8      164kB ± 0%     826kB ± 0%   +404.12%  (p=0.000 n=8+10)
ToSlice/100000_unique_identifiers-8    1.61MB ± 0%    9.25MB ± 0%   +475.93%  (p=0.000 n=10+10)

name                                 old allocs/op  new allocs/op  delta
ToSlice/1_unique_identifiers-8           1.00 ± 0%      1.00 ± 0%       ~     (all equal)
ToSlice/10_unique_identifiers-8          1.00 ± 0%      5.00 ± 0%   +400.00%  (p=0.000 n=10+10)
ToSlice/100_unique_identifiers-8         1.00 ± 0%      8.00 ± 0%   +700.00%  (p=0.000 n=10+10)
ToSlice/1000_unique_identifiers-8        1.00 ± 0%     11.00 ± 0%  +1000.00%  (p=0.000 n=10+10)
ToSlice/10000_unique_identifiers-8       1.00 ± 0%     20.00 ± 0%  +1900.00%  (p=0.000 n=10+10)
ToSlice/100000_unique_identifiers-8      1.00 ± 0%     30.00 ± 0%  +2900.00%  (p=0.000 n=10+10)

Signed-off-by: Leandro López <leandro.lopez@grafana.com>
2021-03-25 15:44:43 +01:00

175 lines
4.2 KiB
Go

// Generated by: main
// TypeWriter: set
// Directive: +gen on identifier
package ast
// Set is a modification of https://github.com/deckarep/golang-set
// The MIT License (MIT)
// Copyright (c) 2013 Ralph Caraveo (deckarep@gmail.com)
// IdentifierSet is the primary type that represents a set
type IdentifierSet map[Identifier]struct{}
// NewIdentifierSet creates and returns a reference to an empty set.
func NewIdentifierSet(a ...Identifier) IdentifierSet {
s := make(IdentifierSet)
for _, i := range a {
s.Add(i)
}
return s
}
// ToSlice returns the elements of the current set as a slice
func (set IdentifierSet) ToSlice() []Identifier {
s := make([]Identifier, len(set), len(set))
j := 0
for v := range set {
s[j] = v
j++
}
return s
}
// Add adds an item to the current set if it doesn't already exist in the set.
func (set IdentifierSet) Add(i Identifier) bool {
_, found := set[i]
set[i] = struct{}{}
return !found //False if it existed already
}
// Contains determines if a given item is already in the set.
func (set IdentifierSet) Contains(i Identifier) bool {
_, found := set[i]
return found
}
// ContainsAll determines if the given items are all in the set
func (set IdentifierSet) ContainsAll(i ...Identifier) bool {
for _, v := range i {
if !set.Contains(v) {
return false
}
}
return true
}
// IsSubset determines if every item in the other set is in this set.
func (set IdentifierSet) IsSubset(other IdentifierSet) bool {
for elem := range set {
if !other.Contains(elem) {
return false
}
}
return true
}
// IsSuperset determines if every item of this set is in the other set.
func (set IdentifierSet) IsSuperset(other IdentifierSet) bool {
return other.IsSubset(set)
}
// Union returns a new set with all items in both sets.
func (set IdentifierSet) Union(other IdentifierSet) IdentifierSet {
unionedSet := NewIdentifierSet()
for elem := range set {
unionedSet.Add(elem)
}
for elem := range other {
unionedSet.Add(elem)
}
return unionedSet
}
// Intersect returns a new set with items that exist only in both sets.
func (set IdentifierSet) Intersect(other IdentifierSet) IdentifierSet {
intersection := NewIdentifierSet()
// loop over smaller set
if set.Cardinality() < other.Cardinality() {
for elem := range set {
if other.Contains(elem) {
intersection.Add(elem)
}
}
} else {
for elem := range other {
if set.Contains(elem) {
intersection.Add(elem)
}
}
}
return intersection
}
// Difference returns a new set with items in the current set but not in the other set
func (set IdentifierSet) Difference(other IdentifierSet) IdentifierSet {
differencedSet := NewIdentifierSet()
for elem := range set {
if !other.Contains(elem) {
differencedSet.Add(elem)
}
}
return differencedSet
}
// SymmetricDifference returns a new set with items in the current set or the other set but not in both.
func (set IdentifierSet) SymmetricDifference(other IdentifierSet) IdentifierSet {
aDiff := set.Difference(other)
bDiff := other.Difference(set)
return aDiff.Union(bDiff)
}
// Clear clears the entire set to be the empty set.
func (set *IdentifierSet) Clear() {
*set = make(IdentifierSet)
}
// Remove allows the removal of a single item in the set.
func (set IdentifierSet) Remove(i Identifier) {
delete(set, i)
}
// Cardinality returns how many items are currently in the set.
func (set IdentifierSet) Cardinality() int {
return len(set)
}
// Iter returns a channel of type identifier that you can range over.
func (set IdentifierSet) Iter() <-chan Identifier {
ch := make(chan Identifier)
go func() {
for elem := range set {
ch <- elem
}
close(ch)
}()
return ch
}
// Equal determines if two sets are equal to each other.
// If they both are the same size and have the same items they are considered equal.
// Order of items is not relevent for sets to be equal.
func (set IdentifierSet) Equal(other IdentifierSet) bool {
if set.Cardinality() != other.Cardinality() {
return false
}
for elem := range set {
if !other.Contains(elem) {
return false
}
}
return true
}
// Clone returns a clone of the set.
// Does NOT clone the underlying elements.
func (set IdentifierSet) Clone() IdentifierSet {
clonedSet := NewIdentifierSet()
for elem := range set {
clonedSet.Add(elem)
}
return clonedSet
}