Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to allocate a large string in Go?

Tags:

go

I need to create a string in Go that is 1048577 characters (1MB + 1 byte). The content of the string is totally unimportant. Is there a way to allocate this directly without concatenating or using buffers?

Also, it's worth noting that the value of string will not change. It's for a unit test to verify that strings that are too long will return an error.

like image 977
Elliot Chance Avatar asked Dec 18 '22 23:12

Elliot Chance


2 Answers

Use strings.Builder to allocate a string without using extra buffers.

var b strings.Builder
b.Grow(1048577)
for i := 0; i < 1048577; i++ {
  b.WriteByte(0)
}
s := b.String()

The call to the Grow method allocates a slice with capacity 1048577. The WriteByte calls fill the slice to capacity. The String() method uses unsafe to convert that slice to a string.

The cost of the loop can be reduced by writing chunks of N bytes at a time and filling single bytes at the end.

If you are not opposed to using the unsafe package, then use this:

p := make([]byte, 1048577)
s := *(*string)(unsafe.Pointer(&p))

If you are asking about how to do this with the simplest code, then use the following:

s := string(make([]byte, 1048577)

This approach does not meet the requirements set forth in the question. It uses an extra buffer instead of allocating the string directly.

like image 134
Bayta Darell Avatar answered Dec 28 '22 23:12

Bayta Darell


I ended up using this:

string(make([]byte, 1048577))

https://play.golang.org/p/afPukPc1Esr

like image 25
Elliot Chance Avatar answered Dec 28 '22 23:12

Elliot Chance