github.com/decred/dcrlnd@v0.7.6/buffer/utils.go (about)

     1  package buffer
     2  
     3  // RecycleSlice zeroes byte slice, making it fresh for another use.
     4  // Zeroing the buffer using a logarithmic number of calls to the optimized copy
     5  // method.  Benchmarking shows this to be ~30 times faster than a for loop that
     6  // sets each index to 0 for ~65KB buffers use for wire messages. Inspired by:
     7  // https://stackoverflow.com/questions/30614165/is-there-analog-of-memset-in-go
     8  func RecycleSlice(b []byte) {
     9  	if len(b) == 0 {
    10  		return
    11  	}
    12  
    13  	b[0] = 0
    14  	for i := 1; i < len(b); i *= 2 {
    15  		copy(b[i:], b[:i])
    16  	}
    17  }