This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
/
container_archetypes.go
193 lines (186 loc) · 5.15 KB
/
container_archetypes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2022 Molecula Corp. (DBA FeatureBase).
// SPDX-License-Identifier: Apache-2.0
package roaring
import (
"fmt"
"math/rand"
"sort"
"strconv"
"strings"
"sync"
"github.com/molecula/apophenia"
)
// ContainerArchetypeNames is the list of supported container archetypes
// used in some testing. This is exported for use in a benchmark-analysis
// tool.
var ContainerArchetypeNames = []string{
"Empty",
"Ary1",
"Ary16",
"Ary256",
"Ary512",
"Ary1024",
"Ary4096",
"RunFull",
"RunSplit",
"Run16",
"Run16Small",
"Run256",
"Run256Small",
"Run1024",
"BM512",
"BM1024",
"BM4096",
"BM4097",
"BM32768",
"BM65000",
}
var containerArchetypes [][]*Container
var containerArchetypesErr error
var initContainerArchetypes sync.Once
func makeArchetypalContainer(rng *rand.Rand, name string) (*Container, error) {
var c *Container
switch {
case name == "Empty":
c = NewContainerArray(nil)
case strings.HasPrefix(name, "Ary"):
size, err := strconv.Atoi(name[3:])
if err != nil {
return nil, fmt.Errorf("can't parse array size: %v", err)
}
array := make([]uint16, size)
seq := apophenia.NewSequence(rng.Int63())
perm, err := apophenia.NewPermutation(65536, 0, seq)
if err != nil {
return nil, err
}
for i := 0; i < size; i++ {
array[i] = uint16(perm.Next())
}
sort.Slice(array, func(a, b int) bool { return array[a] < array[b] })
c = NewContainerArray(array)
case name == "RunFull":
c = NewContainerRun([]Interval16{{Start: 0, Last: 65535}})
case name == "RunSplit":
runs := []Interval16{
{Start: 0, Last: 32700 + uint16(rng.Intn(30))},
{Start: 32768 + uint16(rng.Intn(30)), Last: 65535},
}
c = NewContainerRun(runs)
case strings.HasPrefix(name, "Run"):
countEnd := len(name)
small := strings.HasSuffix(name, "Small")
if small {
countEnd -= 5
}
count, err := strconv.Atoi(name[3:countEnd])
if err != nil {
return nil, fmt.Errorf("can't parse run count in '%s': %v", name, err)
}
runs := make([]Interval16, count)
// For size intervals, we want to divvy up the total
// space around count+1 points, and populate the space between
// those points with a run smaller than that.
stride := int32(65535 / (count + 1))
upper := stride - 10
lower := stride / 10
if small {
lower = 3
upper = lower + (stride / 20)
}
variance := upper - lower
next := int32(0)
prev := int32(0)
for i := 0; i < count; i++ {
next += stride
middle := (prev + next) / 2
runSize := rng.Int31n(variance) + lower
offset := rng.Int31n(variance)
runs[i].Start = uint16(middle + offset - (runSize / 2))
runs[i].Last = runs[i].Start + uint16(runSize)
if runs[i].Last < runs[i].Start {
return nil, fmt.Errorf("fatal, run %d starts at %d, tries to end at %d", i, runs[i].Start, runs[i].Last)
}
prev = next
if i > 0 {
if runs[i].Start <= runs[i-1].Last {
if runs[i-1].Last > 65533 {
return nil, fmt.Errorf("fatal, run %d starts at %d, previous run ended at %d",
i, runs[i].Start, runs[i-1].Last)
} else {
runs[i].Start = runs[i-1].Last + 2
if runs[i].Last < runs[i].Start {
runs[i].Last = runs[i].Start
}
}
}
}
}
c = NewContainerRun(runs)
case strings.HasPrefix(name, "BM"):
size, err := strconv.ParseInt(name[2:], 10, 32)
if err != nil {
return nil, fmt.Errorf("can't parse bitmap size: %v", err)
}
bitmap := make([]uint64, bitmapN)
n := int32(0)
flip := false
// Picking random bits sometimes overlaps, so we want to
// keep trying until we get the requested number. But that's
// really slow for N close to the maximum, so if we want more
// than half the bits set, we'll do it backwards and then
// invert the bits.
bits := int32(size)
if size > 32768 {
flip = true
bits = 65536 - bits
}
for n < bits {
pos := (rng.Uint64() & 65535)
bit := uint64(1 << (pos & 63))
if bitmap[pos/64]&bit == 0 {
n++
bitmap[pos/64] |= bit
}
}
if flip {
for i := 0; i < bitmapN; i++ {
bitmap[i] = ^bitmap[i]
}
n = 65536 - n
}
c = NewContainerBitmap(int(n), bitmap)
count := c.count()
if count != n {
return nil, fmt.Errorf("bitmap should have %d bits, has %d", n, count)
}
}
return c, nil
}
// InitContainerArchetypes ensures that createContainerArchetypes has been
// called, and returns the results of that one call.
func InitContainerArchetypes() ([][]*Container, error) {
initContainerArchetypes.Do(func() {
containerArchetypes, containerArchetypesErr = createContainerArchetypes(8)
})
return containerArchetypes, containerArchetypesErr
}
// createContainerArchetypes creates a slice of *roaring.Container corresponding
// to each container archetype, or reports an error.
func createContainerArchetypes(count int) (cats [][]*Container, err error) {
cats = make([][]*Container, len(ContainerArchetypeNames))
// seed is arbitrary, but picking a seed means we don't get different
// behavior for each run
rng := rand.New(rand.NewSource(23))
for i, name := range ContainerArchetypeNames {
cats[i] = make([]*Container, count)
for j := 0; j < count; j++ {
c, err := makeArchetypalContainer(rng, name)
if err != nil {
return nil, err
}
cats[i][j] = c
}
}
return cats, nil
}