// 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/pkg/util/hack" "github.com/pingcap/tidb/pkg/util/memory" ) // StringSetWithMemoryUsage is a string set with memory usage. type StringSetWithMemoryUsage struct { StringSet bInMap int64 // For tracking large memory usage in time. // If tracker is non-nil, memDelta will track immediately and reset to 0. Otherwise, memDelta will return and lazy track. tracker *memory.Tracker } // 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< (1< (1<