36

In C++ I can initialize an array with some value using memset:

const int MAX = 1000000;
int is_prime[MAX]

memset(is_prime, 1, sizeof(is_prime))

What memset does, crudely can be described as filling the array with some value, but doing this really really fast.

In go I can do is_prime := make([]int, 1000000), but this will create a slice with all 0, in the similar manner I can use new([1000000]int), but nothing will allow me to create an array/slice with all 1 or any other non-zero element.

Of course I can use a loop to populate it with the value later, but the main purpose of memset is that it is way way faster than the loop.

So do Go programmers have a memset analog (fast way of initializing array to some non-zero value)?

5
  • You could use runtime.duffzero with some other value than 0 in AX. Also see mkduff.go.
    – thwd
    Jun 3, 2015 at 8:49
  • With any good compiler the loop is as fast as memset
    – edc65
    Jun 3, 2015 at 12:29
  • 5
    Your memset doesn't initialize all elements to 1 either. Jun 3, 2015 at 12:47
  • Just out of curiosity, do you have timings for performance of memset vs loop?
    – Akavall
    Jun 3, 2015 at 14:54
  • @edc65 it is a common myth that a loop will be as fast as memset. In a modern world with special cache control and SIMD instructions, memset can run significantly faster on large buffers than any simple loop over scalar registers. This started being true with the PowerPC in the '90s, and moved to Intel with the Pentium III, and is also true on ARM with NEON instructions. That being said: The go compiler is explicitly NOT spending time optimizing much, because it's supposed to prioritize fast compile times.
    – Jon Watte
    Mar 1, 2020 at 0:20

2 Answers 2

31

The simplest solution with a loop would look like this:

func memsetLoop(a []int, v int) {
    for i := range a {
        a[i] = v
    }
}

There is no memset support in the standard library, but we can make use of the built-in copy() which is highly optimized.

With repeated copy()

We can set the first element manually, and start copying the already set part to the unset part using copy(); where the already set part gets bigger and bigger every time (doubles), so the number of iterations is log(n):

func memsetRepeat(a []int, v int) {
    if len(a) == 0 {
        return
    }
    a[0] = v
    for bp := 1; bp < len(a); bp *= 2 {
        copy(a[bp:], a[:bp])
    }
}

This solution was inspired by the implementation of bytes.Repeat(). If you just want to create a new []byte filled with the same values, you can use the bytes.Repeat() function. You can't use that for an existing slice or slices other than []byte, for that you can use the presented memsetRepeat().

In case of small slices memsetRepeat() may be slower than memsetLoop() (but in case of small slices it doesn't really matter, it will run in an instant).

Due to using the fast copy(), memsetRepeat() will be much faster if the number of elements grows.

Benchmarking these 2 solutions:

var a = make([]int, 1000) // Size will vary

func BenchmarkLoop(b *testing.B) {
    for i := 0; i < b.N; i++ {
        memsetLoop(a, 10)
    }
}

func BenchmarkRepeat(b *testing.B) {
    for i := 0; i < b.N; i++ {
        memsetRepeat(a, 11)
    }
}

Benchmark results

100 elements: ~1.15 times faster

BenchmarkLoop   20000000                81.6 ns/op
BenchmarkRepeat 20000000                71.0 ns/op

1,000 elements: ~2.5 times faster

BenchmarkLoop    2000000               706 ns/op
BenchmarkRepeat  5000000               279 ns/op

10,000 elements: ~2 times faster

BenchmarkLoop     200000              7029 ns/op
BenchmarkRepeat   500000              3544 ns/op

100,000 elements: ~1.5 times faster

BenchmarkLoop      20000             70671 ns/op
BenchmarkRepeat    30000             45213 ns/op

The highest performance gain is around 3800-4000 elements where it is ~3.2 times faster.

4
  • 1
    thanks for the answer. It looks strange that theoretically you can get log(n) but practically approximately only 2 times faster. Jun 3, 2015 at 8:45
  • 5
    @SalvadorDali The number of iterations is log(n), but each copy() in the subsequent iteration has to do twice as many work (copy twice as many elements). So it's not only the iteration count that counts.
    – icza
    Jun 3, 2015 at 9:50
  • 4
    A small note here, there is this optimisation that exists in the Go compiler.
    – Awn
    May 10, 2018 at 16:50
  • Run at Linux 3.10.0 with amd64 go 1.14 and result is: goos: linux goarch: amd64 pkg: - BenchmarkLoop-4 1949619 573 ns/op BenchmarkRepeat-4 785382 1359 ns/op
    – Liqang Liu
    Mar 9, 2020 at 8:59
7

According to this bug titled "optimize memset idiom" there is no way to do this in Go other than with a loop. The issue was closed on 9 Jan 2013 with this post

I consider this fixed. Optimizing non-zero cases isn't very interesting.

We can open another bug if people feel strongly about doing more.

So the solution is to use a loop as already covered by icza.

There is bytes.Repeat but that also just uses a loop:

func Repeat(b []byte, count int) []byte {
    nb := make([]byte, len(b)*count)
    bp := copy(nb, b)
    for bp < len(nb) {
        copy(nb[bp:], nb[:bp])
        bp *= 2
    }
    return nb
}
2
  • Note that C's implementation is also "just" a loop. Only it makes sure that it's word-aligned and writes word-by-word instead of the width of the data type in question.
    – thwd
    Jun 3, 2015 at 8:36
  • In C it will probably compile down to a specific instruction on x86 though, so not really a loop - i.e the compiler intrinsic for memset
    – paulm
    Jun 3, 2015 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.