forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_with_memory_usage.go
109 lines (98 loc) · 3.37 KB
/
set_with_memory_usage.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
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package set
import (
"github.com/pingcap/tidb/util/hack"
)
// StringSetWithMemoryUsage is a string set with memory usage.
type StringSetWithMemoryUsage struct {
StringSet
bInMap int64
}
// NewStringSetWithMemoryUsage builds a string set.
func NewStringSetWithMemoryUsage(ss ...string) (setWithMemoryUsage StringSetWithMemoryUsage, memDelta int64) {
set := make(StringSet, len(ss))
setWithMemoryUsage = StringSetWithMemoryUsage{
StringSet: set,
bInMap: 0,
}
memDelta = hack.DefBucketMemoryUsageForSetString * (1 << setWithMemoryUsage.bInMap)
for _, s := range ss {
memDelta += setWithMemoryUsage.Insert(s)
}
return setWithMemoryUsage, memDelta
}
// Insert inserts `val` into `s` and return memDelta.
func (s *StringSetWithMemoryUsage) Insert(val string) (memDelta int64) {
s.StringSet.Insert(val)
if s.Count() > (1<<s.bInMap)*hack.LoadFactorNum/hack.LoadFactorDen {
memDelta = hack.DefBucketMemoryUsageForSetString * (1 << s.bInMap)
s.bInMap++
}
return memDelta
}
// Float64SetWithMemoryUsage is a float64 set with memory usage.
type Float64SetWithMemoryUsage struct {
Float64Set
bInMap int64
}
// NewFloat64SetWithMemoryUsage builds a float64 set.
func NewFloat64SetWithMemoryUsage(ss ...float64) (setWithMemoryUsage Float64SetWithMemoryUsage, memDelta int64) {
set := make(Float64Set, len(ss))
setWithMemoryUsage = Float64SetWithMemoryUsage{
Float64Set: set,
bInMap: 0,
}
memDelta = hack.DefBucketMemoryUsageForSetFloat64 * (1 << setWithMemoryUsage.bInMap)
for _, s := range ss {
memDelta += setWithMemoryUsage.Insert(s)
}
return setWithMemoryUsage, memDelta
}
// Insert inserts `val` into `s` and return memDelta.
func (s *Float64SetWithMemoryUsage) Insert(val float64) (memDelta int64) {
s.Float64Set.Insert(val)
if s.Count() > (1<<s.bInMap)*hack.LoadFactorNum/hack.LoadFactorDen {
memDelta = hack.DefBucketMemoryUsageForSetFloat64 * (1 << s.bInMap)
s.bInMap++
}
return memDelta
}
// Int64SetWithMemoryUsage is a int set with memory usage.
type Int64SetWithMemoryUsage struct {
Int64Set
bInMap int64
}
// NewInt64SetWithMemoryUsage builds an int64 set.
func NewInt64SetWithMemoryUsage(ss ...int64) (setWithMemoryUsage Int64SetWithMemoryUsage, memDelta int64) {
set := make(Int64Set, len(ss))
setWithMemoryUsage = Int64SetWithMemoryUsage{
Int64Set: set,
bInMap: 0,
}
memDelta = hack.DefBucketMemoryUsageForSetInt64 * (1 << setWithMemoryUsage.bInMap)
for _, s := range ss {
memDelta += setWithMemoryUsage.Insert(s)
}
return setWithMemoryUsage, memDelta
}
// Insert inserts `val` into `s` and return memDelta.
func (s *Int64SetWithMemoryUsage) Insert(val int64) (memDelta int64) {
s.Int64Set.Insert(val)
if s.Count() > (1<<s.bInMap)*hack.LoadFactorNum/hack.LoadFactorDen {
memDelta = hack.DefBucketMemoryUsageForSetInt64 * (1 << s.bInMap)
s.bInMap++
}
return memDelta
}