Skip to content

Instantly share code, notes, and snippets.

@ifraixedes
Last active Aug 29, 2015
Embed
What would you like to do?
Profiling Golang Slices of Structs vs Pointers to Structs
License
(The MIT License)
Copyright (c) 2015 Ivan Fraixedes (http://ivan.fraixed.es)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime/pprof"
)
const numElems uint32 = 1 << 24
var transformMethod string
func init() {
flag.StringVar(&transformMethod, "method", "", "the method to execute to transform values from one slice type to another: struct, pointer, hybrid or no-pointers")
}
func profileMem(suffix string) {
f, err := os.Create(fmt.Sprintf("mem-%s.out", suffix))
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
}
func main() {
flag.Parse()
fss := generateFromStructSlice(numElems)
switch transformMethod {
case "struct":
f, err := os.Create("cpu-struct.out")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
res := fromStructSliceToStructSlice(fss)
profileMem("struct")
log.Printf("%d", len(res))
case "pointer":
f, err := os.Create("cpu-pointer.out")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
res := fromStructSliceToStructSliceP(fss)
profileMem("pointer")
log.Printf("%d", len(res))
case "hybrid":
f, err := os.Create("cpu-hybrid.out")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
res := fromStructSliceToStructSliceTmpP(fss)
profileMem("hybrid")
log.Printf("%d", len(res))
case "no-pointers":
f, err := os.Create("cpu-no-pointers.out")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
res := fromStructSliceToStructSliceNoPointers(fss)
profileMem("no-pointers")
log.Printf("%d", len(res))
default:
log.Fatal("invalid method")
}
}
package main
import "math/rand"
type fromStruct struct {
id uint64
}
type toStruct struct {
upper uint32
lower uint32
}
type fromStructSlice []fromStruct
type toStructSlice []toStruct
type toStructSliceP []*toStruct
func fromStructSliceToStructSlice(fss fromStructSlice) (tss toStructSlice) {
tss = make(toStructSlice, len(fss))
for i, fs := range fss {
tss[i] = *newToStruct(fs.id)
}
return tss
}
func fromStructSliceToStructSliceTmpP(fss fromStructSlice) (tss toStructSlice) {
tss = make(toStructSlice, len(fss))
var ts *toStruct
for i, fs := range fss {
ts = newToStruct(fs.id)
tss[i] = *ts
}
return tss
}
func fromStructSliceToStructSliceP(fss fromStructSlice) (tss toStructSliceP) {
tss = make(toStructSliceP, len(fss))
for i, fs := range fss {
tss[i] = newToStruct(fs.id)
}
return tss
}
func fromStructSliceToStructSliceNoPointers(fss fromStructSlice) (tss toStructSlice) {
tss = make(toStructSlice, len(fss))
for i, fs := range fss {
tss[i] = toAStruct(fs.id)
}
return tss
}
func newToStruct(num uint64) *toStruct {
return &toStruct{
upper: uint32(num >> 32),
lower: uint32(num),
}
}
func toAStruct(num uint64) toStruct {
return toStruct{
upper: uint32(num >> 32),
lower: uint32(num),
}
}
func generateFromStructSlice(nElems uint32) (fss fromStructSlice) {
fss = make(fromStructSlice, nElems)
for _, fs := range fss {
fs.id = uint64(rand.Int63())
}
return
}
package main
import "testing"
func TestNewToStruct(t *testing.T) {
expected := toStruct{1, 0}
got := newToStruct(4294967296)
if *got != expected {
t.Fatalf("%+v is not the same that expected: %+v", got, expected)
}
expected = toStruct{1, 2}
got = newToStruct(4294967298)
if *got != expected {
t.Fatalf("%+v is not the same that expected: %+v", got, expected)
}
expected = toStruct{1, 347}
got = newToStruct(4294967643)
if *got != expected {
t.Fatalf("%+v is not the same that expected: %+v", got, expected)
}
expected = toStruct{34, 125}
got = newToStruct(4294967296*34 + 125)
if *got != expected {
t.Fatalf("%+v is not the same that expected: %+v", got, expected)
}
}
func TestToAStruct(t *testing.T) {
expected := toStruct{1, 0}
got := toAStruct(4294967296)
if got != expected {
t.Fatalf("%+v is not the same that expected: %+v", got, expected)
}
expected = toStruct{1, 2}
got = toAStruct(4294967298)
if got != expected {
t.Fatalf("%+v is not the same that expected: %+v", got, expected)
}
expected = toStruct{1, 347}
got = toAStruct(4294967643)
if got != expected {
t.Fatalf("%+v is not the same that expected: %+v", got, expected)
}
expected = toStruct{34, 125}
got = toAStruct(4294967296*34 + 125)
if got != expected {
t.Fatalf("%+v is not the same that expected: %+v", got, expected)
}
}
func TestFromStructSliceToStructSlice(t *testing.T) {
fss := generateFromStructSlice(10)
tss := fromStructSliceToStructSlice(fss)
for i, fs := range fss {
if !compareFromStructToStruct(&fs, &tss[i]) {
t.Fatalf("%+v is not as: %+v", &fs, &tss[i])
}
}
}
func TestFromStructSliceToStructSliceP(t *testing.T) {
fss := generateFromStructSlice(10)
tss := fromStructSliceToStructSliceP(fss)
for i, fs := range fss {
if !compareFromStructToStruct(&fs, tss[i]) {
t.Fatalf("%+v is not as: %+v", &fs, tss[i])
}
}
}
func TestFromStructSliceToStructSliceTmpP(t *testing.T) {
fss := generateFromStructSlice(10)
tss := fromStructSliceToStructSliceTmpP(fss)
for i, fs := range fss {
if !compareFromStructToStruct(&fs, &tss[i]) {
t.Fatalf("%+v is not as: %+v", &fs, &tss[i])
}
}
}
func TestFromStructSliceToStructSliceNoPointers(t *testing.T) {
fss := generateFromStructSlice(10)
tss := fromStructSliceToStructSliceNoPointers(fss)
for i, fs := range fss {
if !compareFromStructToStruct(&fs, &tss[i]) {
t.Fatalf("%+v is not as: %+v", &fs, &tss[i])
}
}
}
func BenchmarkFromStructSliceToStructSlice(b *testing.B) {
fss := generateFromStructSlice(numElems)
b.ResetTimer()
for i := 0; i < b.N; i++ {
fromStructSliceToStructSlice(fss)
}
}
func BenchmarkFromStructSliceToStructSliceTmpP(b *testing.B) {
fss := generateFromStructSlice(numElems)
b.ResetTimer()
for i := 0; i < b.N; i++ {
fromStructSliceToStructSliceTmpP(fss)
}
}
func BenchmarkFromStructSliceToStructSliceP(b *testing.B) {
fss := generateFromStructSlice(numElems)
b.ResetTimer()
for i := 0; i < b.N; i++ {
fromStructSliceToStructSliceP(fss)
}
}
func BenchmarkFromStructSliceToStructSliceNotPointers(b *testing.B) {
fss := generateFromStructSlice(numElems)
b.ResetTimer()
for i := 0; i < b.N; i++ {
fromStructSliceToStructSliceNoPointers(fss)
}
}
func compareFromStructToStruct(fs *fromStruct, ts *toStruct) bool {
return fs.id == (uint64(ts.upper) + uint64(ts.lower))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment