Skip to content

Commit

Permalink
task: add slice on stack
Browse files Browse the repository at this point in the history
  • Loading branch information
turisap committed May 27, 2024
1 parent d28cfa9 commit 8fc3e33
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go/2024/other/optimize/s10.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct sorting fields
preallocate map (or GC ballast)
use ints instead of floats
Boundary checks
Allocate the backing array of a slice on stack even if its size is larger than or equal to 64K (but not larger than 10M)
Allocate the backing array of a slice on stack even if its size is larger than or equal to 64K (but not larger than 10M)
Avoid unnecessary pointer dereferences in a loop
Use index tables instead of maps which key types have only a small set of possible
*/
Expand Down
15 changes: 15 additions & 0 deletions go/2024/other/optimize/slice_on_stack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

const N = 10 * 1024 * 1024 // 10M
var n = N / 2

func f() byte {
var a [N]byte // allocated on stack
for i := range a {
a[i] = byte(i)
}
var s = a[:] // a slice with 10M elements

var p = &a
return s[n] + p[n]
}

0 comments on commit 8fc3e33

Please sign in to comment.