-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: runtime: dynamically de-duplicate strings #61063
Comments
I believe you are looking for sync pool it predates generics so unfortunately you’ll be casting to an interface but you could write generic wrapper around it. |
@jaloren |
@namewxt1220 maybe you are looking for string interning? |
@atdiar |
This proposal needs more detail.
|
I think what this proposal is really trying to achieve is string deduplication, not string interning. Can string deduplication be applied after the creation of the string, a.k.a we change the pointer in |
@bcmills |
If it's just your example, then they will be the same address package main
import "unsafe"
func main() {
str1 := "hello world"
str2 := "hello world2"
str3 := "hello world3"
str4 := "hello world"
println(unsafe.StringData(str1))
println(unsafe.StringData(str2))
println(unsafe.StringData(str3))
println(unsafe.StringData(str4))
// 0xc34e3d <-----
// 0xc3518c
// 0xc35198
// 0xc34e3d <-----
} |
@alresvor package main
import (
"reflect"
"unsafe"
)
func main() {
var buf1 = []byte("hello world")
var buf2 = []byte("hello world")
str1 := decode(buf1)
str2 := decode(buf2)
println((*reflect.StringHeader)(unsafe.Pointer(&str1)).Data)
println((*reflect.StringHeader)(unsafe.Pointer(&str2)).Data)
}
func decode(buf []byte) string {
return string(buf)
} buf1 and buf2 are the two array spaces generated at runtime, str1 and str2 is pointing to different instances, and I want str1 and str2 to point to the same instance. |
Timed out in state WaitingForInfo. Closing. (I am just a bot, though. Please speak up if this is a mistake or you have the requested information.) |
str1 := "hello world"
str2 := "hello world"
At runtime, it is desirable to point str1 and str2 to a single instance in the same string pool, since they contain the same string constants.
This optimization technique reduces the pressure on memory usage and garbage collection, while also improving the performance of the program.
Is it possible to do this in go?
The text was updated successfully, but these errors were encountered: