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"
2022-12-12 22:31:16 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
2022-08-26 21:12:39 +00:00
)
2023-01-31 18:55:31 +00:00
var internStringMaxLen = flag . Int ( "internStringMaxLen" , 500 , "The maximum length for strings to intern. Lower limit may save memory at the cost of higher CPU usage. " +
2023-01-24 07:28:10 +00:00
"See https://en.wikipedia.org/wiki/String_interning" )
2023-01-04 06:14:20 +00:00
// InternBytes interns b as a string
func InternBytes ( b [ ] byte ) string {
s := ToUnsafeString ( b )
return InternString ( s )
}
2022-08-26 21:12:39 +00:00
// InternString returns interned s.
//
// This may be needed for reducing the amounts of allocated memory.
func InternString ( s string ) string {
2022-12-12 22:31:16 +00:00
ct := fasttime . UnixTimestamp ( )
if v , ok := internStringsMap . Load ( s ) ; ok {
e := v . ( * ismEntry )
if atomic . LoadUint64 ( & e . lastAccessTime ) + 10 < ct {
// Reduce the frequency of e.lastAccessTime update to once per 10 seconds
// in order to improve the fast path speed on systems with many CPU cores.
atomic . StoreUint64 ( & e . lastAccessTime , ct )
}
return e . s
2022-08-26 21:12:39 +00:00
}
// Make a new copy for s in order to remove references from possible bigger string s refers to.
2022-12-12 22:31:16 +00:00
sCopy := strings . Clone ( s )
2023-01-24 07:28:10 +00:00
if len ( sCopy ) > * internStringMaxLen {
// Do not intern long strings, since this may result in high memory usage
// like in https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3692
return sCopy
}
2022-12-12 22:31:16 +00:00
e := & ismEntry {
lastAccessTime : ct ,
s : sCopy ,
}
internStringsMap . Store ( sCopy , e )
2022-12-21 20:57:28 +00:00
if needCleanup ( & internStringsMapLastCleanupTime , ct ) {
// Perform a global cleanup for internStringsMap by removing items, which weren't accessed during the last 5 minutes.
2022-12-12 22:31:16 +00:00
m := & internStringsMap
m . Range ( func ( k , v interface { } ) bool {
e := v . ( * ismEntry )
if atomic . LoadUint64 ( & e . lastAccessTime ) + 5 * 60 < ct {
m . Delete ( k )
}
return true
} )
2022-08-26 21:12:39 +00:00
}
2022-12-12 22:31:16 +00:00
2022-08-26 21:12:39 +00:00
return sCopy
}
2022-12-12 22:31:16 +00:00
type ismEntry struct {
lastAccessTime uint64
s string
}
2022-08-26 21:12:39 +00:00
var (
2022-12-12 22:31:16 +00:00
internStringsMap sync . Map
internStringsMapLastCleanupTime uint64
2022-08-26 21:12:39 +00:00
)