x - allocation number
y - diff allocated at iteration between old and new memory
Last active
April 5, 2023 20:00
-
-
Save sshaplygin/3536f68907a29dc9c4cd7a5ae091a71d to your computer and use it in GitHub Desktop.
How increase memory allocation by append to ints slice in golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"reflect" | |
"unsafe" | |
) | |
func main() { | |
var x []int | |
var prev uintptr | |
var prevIdx int | |
var count int | |
for i := 0; i < 10_000_000; i++ { | |
x = append(x, i) | |
p := (*reflect.SliceHeader)(unsafe.Pointer(&x)).Data | |
if prev != p { | |
fmt.Printf("%d;%d\n", count, i-prevIdx) | |
prev = p | |
prevIdx = i | |
count++ | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Num;Memory;Diff | |
1;1;1 | |
2;2;1 | |
3;4;2 | |
4;8;4 | |
5;16;8 | |
6;32;16 | |
7;64;32 | |
8;128;64 | |
9;256;128 | |
10;512;256 | |
11;848;336 | |
12;1280;432 | |
13;1792;512 | |
14;2560;768 | |
15;3408;848 | |
16;5120;1712 | |
17;7168;2048 | |
18;9216;2048 | |
19;12288;3072 | |
20;16384;4096 | |
21;21504;5120 | |
22;27648;6144 | |
23;34816;7168 | |
24;44032;9216 | |
25;55296;11264 | |
26;69632;14336 | |
27;88064;18432 | |
28;110592;22528 | |
29;139264;28672 | |
30;175104;35840 | |
31;219136;44032 | |
32;274432;55296 | |
33;344064;69632 | |
34;431104;87040 | |
35;539648;108544 | |
36;674816;135168 | |
37;843776;168960 | |
38;1055744;211968 | |
39;1319936;264192 | |
40;1650688;330752 | |
41;2064384;413696 | |
42;2581504;517120 | |
43;3227648;646144 | |
44;4035584;807936 | |
45;5045248;1009664 | |
46;6306816;1261568 | |
47;7883776;1576960 | |
48;9854976;1971200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment