github.com/m3db/m3@v1.5.0/src/query/util/memset.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package util 22 23 // Memset is a faster way to initialize a float64 array. 24 // NB: Inspired from https://github.com/tmthrgd/go-memset, which works 25 // directly on the byte interface. The 0 case is optimized due to 26 // https://github.com/golang/go/issues/5373 but for non the zero case, 27 // we use the copy() optimization. 28 // BenchmarkMemsetZeroValues-4 1000000 1344 ns/op 29 // BenchmarkLoopZeroValues-4 500000 3217 ns/op 30 // BenchmarkMemsetNonZeroValues-4 1000000 1537 ns/op 31 // BenchmarkLoopNonZeroValues-4 500000 3236 ns/op 32 func Memset(data []float64, value float64) { 33 if value == 0 { 34 for i := range data { 35 data[i] = 0 36 } 37 } else if len(data) != 0 { 38 data[0] = value 39 40 for i := 1; i < len(data); i *= 2 { 41 copy(data[i:], data[:i]) 42 } 43 } 44 } 45 46 // MemsetInt is a faster way to initialize an int array. 47 func MemsetInt(data []int, value int) { 48 if value == 0 { 49 for i := range data { 50 data[i] = 0 51 } 52 } else if len(data) != 0 { 53 data[0] = value 54 55 for i := 1; i < len(data); i *= 2 { 56 copy(data[i:], data[:i]) 57 } 58 } 59 }