-
Notifications
You must be signed in to change notification settings - Fork 3
/
cloner.go
71 lines (63 loc) · 1.33 KB
/
cloner.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
package frozen
import (
"math/bits"
"os"
"strconv"
"strings"
)
type cloner struct {
parallelDepth int
mutate bool
}
var (
theCopier = &cloner{mutate: false, parallelDepth: -1}
theMutator = &cloner{mutate: true, parallelDepth: -1}
)
func newCloner(mutate bool, capacity int) *cloner {
frozenConcurrency := os.Getenv("FROZEN_CONCURRENCY")
var maxConcurrency int
var err error
if strings.ToLower(frozenConcurrency) == "off" {
maxConcurrency = 1<<(bits.UintSize-1) - 1
} else {
maxConcurrency, err = strconv.Atoi(frozenConcurrency)
if err != nil {
maxConcurrency = 15
}
}
return &cloner{
mutate: mutate,
// Give parallel workers O(32k) elements each to process. If
// parallelDepth < 0, it won't parallelise.
parallelDepth: (bits.Len64(uint64(capacity)) - maxConcurrency) / 3,
}
}
func (c *cloner) node(n *node, prepared **node) *node {
switch {
case c.mutate:
return n
case *prepared != nil:
return *prepared
default:
result := *n
*prepared = &result
return &result
}
}
func (c *cloner) leaf(l *leaf) *leaf {
switch {
case c.mutate:
return l
default:
result := *l
return &result
}
}
func (c *cloner) extras(l *leaf, capacityIncrease int) extraLeafElems {
if c.mutate {
return l.extras()
}
x := l.extras()
x = append(make([]interface{}, 0, len(x)+capacityIncrease), x...)
return x
}