Skip to content

Commit

Permalink
Amortize slice growth to avoid too many allocs
Browse files Browse the repository at this point in the history
  • Loading branch information
prashantv committed Sep 10, 2023
1 parent 39666df commit cd58493
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
34 changes: 17 additions & 17 deletions sorted_vs_heap/bench.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ goos: linux
goarch: amd64
pkg: github.com/prashantv/go-bench/sorted_vs_heap
cpu: 13th Gen Intel(R) Core(TM) i5-13600KF
BenchmarkSorted/1_shuffled_elems 78202698 16.18 ns/op 8 B/op 1 allocs/op
BenchmarkSorted/1_sorted_elems 74013729 16.00 ns/op 8 B/op 1 allocs/op
BenchmarkSorted/10_shuffled_elems 4999582 241.4 ns/op 160 B/op 2 allocs/op
BenchmarkSorted/10_sorted_elems 6413139 187.6 ns/op 160 B/op 2 allocs/op
BenchmarkSorted/100_shuffled_elems 57819 20819 ns/op 1536 B/op 1 allocs/op
BenchmarkSorted/100_sorted_elems 135454 8888 ns/op 1536 B/op 1 allocs/op
BenchmarkSorted/1000_shuffled_elems 1038 1166869 ns/op 17924 B/op 3 allocs/op
BenchmarkSorted/1000_sorted_elems 1886 639461 ns/op 17922 B/op 3 allocs/op
BenchmarkHeap/1_shuffled_elems 97691158 12.53 ns/op 0 B/op 0 allocs/op
BenchmarkHeap/1_sorted_elems 100000000 12.23 ns/op 0 B/op 0 allocs/op
BenchmarkHeap/10_shuffled_elems 6940164 172.5 ns/op 0 B/op 0 allocs/op
BenchmarkHeap/10_sorted_elems 7564102 159.1 ns/op 0 B/op 0 allocs/op
BenchmarkHeap/100_shuffled_elems 432127 2798 ns/op 0 B/op 0 allocs/op
BenchmarkHeap/100_sorted_elems 475987 2528 ns/op 0 B/op 0 allocs/op
BenchmarkHeap/1000_shuffled_elems 14500 83782 ns/op 11905 B/op 1488 allocs/op
BenchmarkHeap/1000_sorted_elems 16956 70644 ns/op 11905 B/op 1488 allocs/op
BenchmarkSorted/1_shuffled_elems 138947696 8.705 ns/op 8 B/op 0 allocs/op
BenchmarkSorted/1_sorted_elems 131542365 9.205 ns/op 8 B/op 0 allocs/op
BenchmarkSorted/10_shuffled_elems 5390264 227.3 ns/op 80 B/op 1 allocs/op
BenchmarkSorted/10_sorted_elems 7092080 171.7 ns/op 80 B/op 1 allocs/op
BenchmarkSorted/100_shuffled_elems 51919 23113 ns/op 1536 B/op 1 allocs/op
BenchmarkSorted/100_sorted_elems 117374 10087 ns/op 1536 B/op 1 allocs/op
BenchmarkSorted/1000_shuffled_elems 920 1315147 ns/op 17922 B/op 3 allocs/op
BenchmarkSorted/1000_sorted_elems 1621 749301 ns/op 17921 B/op 3 allocs/op
BenchmarkHeap/1_shuffled_elems 93447892 12.72 ns/op 0 B/op 0 allocs/op
BenchmarkHeap/1_sorted_elems 94744371 12.66 ns/op 0 B/op 0 allocs/op
BenchmarkHeap/10_shuffled_elems 6250802 192.5 ns/op 0 B/op 0 allocs/op
BenchmarkHeap/10_sorted_elems 6850843 175.8 ns/op 0 B/op 0 allocs/op
BenchmarkHeap/100_shuffled_elems 384812 3156 ns/op 0 B/op 0 allocs/op
BenchmarkHeap/100_sorted_elems 421676 2860 ns/op 0 B/op 0 allocs/op
BenchmarkHeap/1000_shuffled_elems 14072 85918 ns/op 11905 B/op 1488 allocs/op
BenchmarkHeap/1000_sorted_elems 16417 73107 ns/op 11905 B/op 1488 allocs/op
PASS
ok github.com/prashantv/go-bench/sorted_vs_heap 22.279s
ok github.com/prashantv/go-bench/sorted_vs_heap 24.100s
3 changes: 3 additions & 0 deletions sorted_vs_heap/sorted_vs_heap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func (ss sortedSlice) Less(i, j int) bool { return ss[i] < ss[j] }
func (ss sortedSlice) Swap(i, j int) { ss[i], ss[j] = ss[j], ss[i] }

func (ss *sortedSlice) PushV(v int) {
if cap(*ss) == 0 {
*ss = make(sortedSlice, 0, 10)
}
*ss = append(*ss, v)
sort.Sort(ss)
}
Expand Down

0 comments on commit cd58493

Please sign in to comment.