-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapimpl.go
More file actions
205 lines (195 loc) · 5.27 KB
/
mapimpl.go
File metadata and controls
205 lines (195 loc) · 5.27 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
package sortprop
import "sync"
// Map-based implementations that avoid sorting and preserve original order.
// Optimized to reduce allocations by pre-sizing maps and result slices and reusing map objects via sync.Pool.
var (
propMapPool = sync.Pool{New: func() interface{} { return make(map[string]Property) }}
seenMapPool = sync.Pool{New: func() interface{} { return make(map[string]struct{}) }}
// result slice pools to reuse allocations for common sizes
kpPool = sync.Pool{New: func() interface{} { return make(KeyProperties, 0, 0) }}
vpPool = sync.Pool{New: func() interface{} { return make(ValueProperties, 0, 0) }}
// avoid returning huge backing arrays from pools; cap threshold
poolCapThreshold = 16384 // elements
// if unique count estimate exceeds this, fall back to sort-based implementation
// Exported as FallbackThreshold so callers can tune at runtime.
FallbackThreshold = 8192
)
func getPropMap(capacity int) map[string]Property {
if capacity > 0 {
return make(map[string]Property, capacity)
}
m := propMapPool.Get().(map[string]Property)
for k := range m {
delete(m, k)
}
return m
}
func putPropMap(m map[string]Property) {
for k := range m {
delete(m, k)
}
propMapPool.Put(m)
}
func getSeenMap(capacity int) map[string]struct{} {
if capacity > 0 {
return make(map[string]struct{}, capacity)
}
m := seenMapPool.Get().(map[string]struct{})
for k := range m {
delete(m, k)
}
return m
}
func putSeenMap(m map[string]struct{}) {
for k := range m {
delete(m, k)
}
seenMapPool.Put(m)
}
// UniqueKeysMap returns a slice with only one instance of each unique key.
// If keeplast is true, the last occurrence is kept; otherwise the first occurrence is kept.
// The input is not mutated and the original order is preserved (first-seen order or last-seen order).
func UniqueKeysMap(kp KeyProperties, keeplast bool) KeyProperties {
if len(kp) == 0 {
return KeyProperties{}
}
if len(kp) > FallbackThreshold {
m := make(map[string]struct{}, 128)
unique := 0
for _, p := range kp {
if _, ok := m[p.Key]; !ok {
m[p.Key] = struct{}{}
unique++
if unique > FallbackThreshold {
return UniqueKeys(kp, keeplast)
}
}
}
}
if keeplast {
// keep last: record last occurrence in a map
last := getPropMap(len(kp))
for _, p := range kp {
last[p.Key] = p
}
// get a result slice from pool and ensure capacity
res := kpPool.Get().(KeyProperties)
res = res[:0]
if cap(res) < len(last) {
res = make(KeyProperties, 0, len(last))
}
seen := getSeenMap(len(last))
for _, p := range kp {
if _, ok := seen[p.Key]; ok {
continue
}
res = append(res, last[p.Key])
seen[p.Key] = struct{}{}
}
putPropMap(last)
putSeenMap(seen)
// if result backing cap is reasonable, return it directly; otherwise copy to shrink-to-fit
if cap(res) <= poolCapThreshold {
out := res[:len(res)]
kpPool.Put(res[:0])
return out
}
out := make(KeyProperties, len(res))
copy(out, res)
kpPool.Put(res[:0])
return out
}
// keep first: iterate and add first occurrence
seen := getSeenMap(len(kp))
res := kpPool.Get().(KeyProperties)
res = res[:0]
if cap(res) < len(kp) {
res = make(KeyProperties, 0, len(kp))
}
for _, p := range kp {
if _, ok := seen[p.Key]; ok {
continue
}
seen[p.Key] = struct{}{}
res = append(res, p)
}
putSeenMap(seen)
out := make(KeyProperties, len(res))
copy(out, res)
kpPool.Put(res)
return out
}
// UniqueValuesMap returns a slice with only one instance of each unique value.
// If keeplast is true, the last occurrence is kept; otherwise the first occurrence is kept.
// The input is not mutated and original order is preserved.
func UniqueValuesMap(vp ValueProperties, keeplast bool) ValueProperties {
if len(vp) == 0 {
return ValueProperties{}
}
if len(vp) > FallbackThreshold {
m := make(map[string]struct{}, 128)
unique := 0
for _, p := range vp {
if _, ok := m[p.Value]; !ok {
m[p.Value] = struct{}{}
unique++
if unique > FallbackThreshold {
return UniqueValues(vp, keeplast)
}
}
}
}
if keeplast {
last := getPropMap(len(vp))
for _, p := range vp {
last[p.Value] = p
}
res := vpPool.Get().(ValueProperties)
res = res[:0]
if cap(res) < len(last) {
res = make(ValueProperties, 0, len(last))
}
seen := getSeenMap(len(last))
for _, p := range vp {
if _, ok := seen[p.Value]; ok {
continue
}
res = append(res, last[p.Value])
seen[p.Value] = struct{}{}
}
putPropMap(last)
putSeenMap(seen)
if cap(res) <= poolCapThreshold {
out := res[:len(res)]
vpPool.Put(res[:0])
return out
}
out := make(ValueProperties, len(res))
copy(out, res)
vpPool.Put(res[:0])
return out
}
seen := getSeenMap(len(vp))
res := vpPool.Get().(ValueProperties)
res = res[:0]
if cap(res) < len(vp) {
res = make(ValueProperties, 0, len(vp))
}
for _, p := range vp {
if _, ok := seen[p.Value]; ok {
continue
}
seen[p.Value] = struct{}{}
res = append(res, p)
}
putSeenMap(seen)
out := make(ValueProperties, len(res))
copy(out, res)
vpPool.Put(res)
return out
}
// Short aliases for concise names requested by the user.
func UniqueMap(kp KeyProperties, keeplast bool) KeyProperties { return UniqueKeysMap(kp, keeplast) }
func UniqueValueMap(vp ValueProperties, keeplast bool) ValueProperties {
return UniqueValuesMap(vp, keeplast)
}