2022-09-30 04:34:14 +00:00
package bytesutil
2022-08-26 21:12:39 +00:00
import (
2023-01-24 07:28:10 +00:00
"flag"
2022-12-12 22:31:16 +00:00
"strings"
2022-08-26 21:12:39 +00:00
"sync"
"sync/atomic"
2023-02-27 22:15:49 +00:00
"time"
2022-12-12 22:31:16 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
2024-06-13 13:02:55 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/timeutil"
2022-08-26 21:12:39 +00:00
)
2023-02-27 22:15:49 +00:00
var (
2023-07-19 08:10:51 +00:00
internStringMaxLen = flag . Int ( "internStringMaxLen" , 500 , "The maximum length for strings to intern. A lower limit may save memory at the cost of higher CPU usage. " +
2023-02-27 22:15:49 +00:00
"See https://en.wikipedia.org/wiki/String_interning . See also -internStringDisableCache and -internStringCacheExpireDuration" )
disableCache = flag . Bool ( "internStringDisableCache" , false , "Whether to disable caches for interned strings. This may reduce memory usage at the cost of higher CPU usage. " +
"See https://en.wikipedia.org/wiki/String_interning . See also -internStringCacheExpireDuration and -internStringMaxLen" )
2023-05-10 07:50:41 +00:00
cacheExpireDuration = flag . Duration ( "internStringCacheExpireDuration" , 6 * time . Minute , "The expiry duration for caches for interned strings. " +
2023-02-27 22:15:49 +00:00
"See https://en.wikipedia.org/wiki/String_interning . See also -internStringMaxLen and -internStringDisableCache" )
)
2024-06-10 15:58:02 +00:00
type internStringMap struct {
2024-06-13 13:02:55 +00:00
mutableLock sync . Mutex
2024-06-10 15:58:02 +00:00
mutable map [ string ] string
mutableReads uint64
readonly atomic . Pointer [ map [ string ] internStringMapEntry ]
2023-02-27 22:15:49 +00:00
}
2023-01-24 07:28:10 +00:00
2024-06-10 15:58:02 +00:00
type internStringMapEntry struct {
deadline uint64
s string
2023-01-04 06:14:20 +00:00
}
2024-06-10 15:58:02 +00:00
func newInternStringMap ( ) * internStringMap {
2024-06-11 15:50:32 +00:00
m := & internStringMap {
2024-06-10 15:58:02 +00:00
mutable : make ( map [ string ] string ) ,
}
readonly := make ( map [ string ] internStringMapEntry )
2024-06-11 15:50:32 +00:00
m . readonly . Store ( & readonly )
2024-06-13 13:02:55 +00:00
go func ( ) {
cleanupInterval := timeutil . AddJitterToDuration ( * cacheExpireDuration ) / 2
ticker := time . NewTicker ( cleanupInterval )
for range ticker . C {
m . cleanup ( )
}
} ( )
2024-06-11 15:50:32 +00:00
return m
2024-06-10 15:58:02 +00:00
}
func ( m * internStringMap ) getReadonly ( ) map [ string ] internStringMapEntry {
return * m . readonly . Load ( )
}
func ( m * internStringMap ) intern ( s string ) string {
2024-06-11 15:50:32 +00:00
if isSkipCache ( s ) {
2023-02-27 22:15:49 +00:00
return strings . Clone ( s )
}
2024-06-10 15:58:02 +00:00
readonly := m . getReadonly ( )
e , ok := readonly [ s ]
if ok {
2024-06-13 13:02:55 +00:00
// Fast path - the string has been found in readonly map.
2022-12-12 22:31:16 +00:00
return e . s
2022-08-26 21:12:39 +00:00
}
2024-06-10 15:58:02 +00:00
2024-06-11 15:50:32 +00:00
// Slower path - search for the string in mutable map under the lock.
2024-06-13 13:02:55 +00:00
m . mutableLock . Lock ( )
2024-06-10 15:58:02 +00:00
sInterned , ok := m . mutable [ s ]
if ! ok {
2024-06-11 15:50:32 +00:00
// Verify whether s has been already registered by concurrent goroutines in m.readonly
2024-06-10 15:58:02 +00:00
readonly = m . getReadonly ( )
e , ok = readonly [ s ]
if ! ok {
// Slowest path - register the string in mutable map.
// Make a new copy for s in order to remove references from possible bigger string s refers to.
sInterned = strings . Clone ( s )
m . mutable [ sInterned ] = sInterned
} else {
sInterned = e . s
}
2022-12-12 22:31:16 +00:00
}
2024-06-10 15:58:02 +00:00
m . mutableReads ++
if m . mutableReads > uint64 ( len ( readonly ) ) {
m . migrateMutableToReadonlyLocked ( )
m . mutableReads = 0
2022-08-26 21:12:39 +00:00
}
2024-06-13 13:02:55 +00:00
m . mutableLock . Unlock ( )
2022-12-12 22:31:16 +00:00
2024-06-10 15:58:02 +00:00
return sInterned
2022-08-26 21:12:39 +00:00
}
2024-06-10 15:58:02 +00:00
func ( m * internStringMap ) migrateMutableToReadonlyLocked ( ) {
readonly := m . getReadonly ( )
readonlyCopy := make ( map [ string ] internStringMapEntry , len ( readonly ) + len ( m . mutable ) )
for k , e := range readonly {
readonlyCopy [ k ] = e
}
deadline := fasttime . UnixTimestamp ( ) + uint64 ( cacheExpireDuration . Seconds ( ) + 0.5 )
for k , s := range m . mutable {
readonlyCopy [ k ] = internStringMapEntry {
s : s ,
deadline : deadline ,
}
}
m . mutable = make ( map [ string ] string )
m . readonly . Store ( & readonlyCopy )
2022-12-12 22:31:16 +00:00
}
2024-06-10 15:58:02 +00:00
func ( m * internStringMap ) cleanup ( ) {
readonly := m . getReadonly ( )
currentTime := fasttime . UnixTimestamp ( )
needCleanup := false
for _ , e := range readonly {
if e . deadline <= currentTime {
needCleanup = true
break
}
}
if ! needCleanup {
return
}
readonlyCopy := make ( map [ string ] internStringMapEntry , len ( readonly ) )
for k , e := range readonly {
if e . deadline > currentTime {
readonlyCopy [ k ] = e
}
}
m . readonly . Store ( & readonlyCopy )
}
func isSkipCache ( s string ) bool {
return * disableCache || len ( s ) > * internStringMaxLen
}
// InternBytes interns b as a string
func InternBytes ( b [ ] byte ) string {
s := ToUnsafeString ( b )
return InternString ( s )
}
// InternString returns interned s.
//
// This may be needed for reducing the amounts of allocated memory.
func InternString ( s string ) string {
return ism . intern ( s )
}
var ism = newInternStringMap ( )