-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
173 lines (147 loc) · 4.63 KB
/
Copy pathexample_test.go
File metadata and controls
173 lines (147 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Package memory_test demonstrates the off-heap memory allocator API
// via runnable, testable examples for pkg.go.dev.
package memory_test
import (
"fmt"
"github.com/xDarkicex/memory"
)
// Example_pool demonstrates the basic Pool lifecycle: create, allocate,
// use off-heap memory, and bulk-free with Reset. Shows both the raw API
// and the typed PoolAlloc helper.
func Example_pool() {
cfg := memory.AllocatorConfig{
PoolSize: 1024 * 1024, // 1MB
SlabSize: 64 * 1024, // 64KB slabs
SlabCount: 1,
Prealloc: true,
}
pool, err := memory.NewPool(cfg, 64)
if err != nil {
panic(err)
}
defer pool.Free()
// Raw API: allocate a byte slice.
buf, err := pool.Allocate(64)
if err != nil {
panic(err)
}
copy(buf, "hello")
fmt.Printf("allocated %d bytes: %s\n", len(buf), string(buf[:5]))
// Typed helper: PoolAlloc allocates a zeroed struct directly off-heap.
type User struct{ ID int64; Name [32]byte }
u := memory.MustPoolAlloc[User](pool)
u.ID = 42
copy(u.Name[:], "alice")
fmt.Printf("User{ID: %d, Name: %s}\n", u.ID, string(u.Name[:5]))
// Output:
// allocated 64 bytes: hello
// User{ID: 42, Name: alice}
}
// Example_arena demonstrates Arena: a bump-pointer allocator backed by a
// single mmap'd region. Reset reuses the backing memory; Free releases it.
// Shows both the raw API and the typed ArenaAlloc helper.
func Example_arena() {
arena, err := memory.NewArena(4096, 64)
if err != nil {
panic(err)
}
defer arena.Free()
// Raw API: allocate a fixed number of bytes.
_, err = arena.Alloc(256)
if err != nil {
panic(err)
}
fmt.Println("allocated 256 bytes, remaining:", arena.Remaining())
arena.Reset()
fmt.Println("after reset, remaining:", arena.Remaining())
// Typed helper: ArenaAlloc allocates a zeroed struct directly off-heap.
type Point struct{ X, Y float64 }
p := memory.MustArenaAlloc[Point](arena)
p.X, p.Y = 3.0, 4.0
fmt.Printf("Point{X: %.0f, Y: %.0f}\n", p.X, p.Y)
// Output:
// allocated 256 bytes, remaining: 3840
// after reset, remaining: 4096
// Point{X: 3, Y: 4}
}
// Example_freelist demonstrates FreeList: a fixed-size lock-free allocator.
// Shows both the raw []byte API and the typed FreeListAlloc helper.
func Example_freelist() {
cfg := memory.DefaultFreeListConfig()
cfg.SlotSize = 64
cfg.SlabSize = 64 * 1024 // 64KB slab
cfg.SlabCount = 1
cfg.PoolSize = 1024 * 1024
cfg.Prealloc = true
fl, err := memory.NewFreeList(cfg, 64)
if err != nil {
panic(err)
}
defer fl.Free()
// Raw API: allocate a []byte slot, copy data into it.
slot, _ := fl.Allocate()
copy(slot, "hello from freelist")
fmt.Printf("slot size: %d, content: %s\n", len(slot), string(slot[:19]))
fl.Deallocate(slot)
// Typed helper: FreeListAlloc returns a *Record directly — no unsafe,
// no offset arithmetic, no []byte tracking.
type Record struct{ ID uint64; Name [32]byte }
rec, _ := memory.FreeListAlloc[Record](fl)
rec.ID = 7
copy(rec.Name[:], "widget")
fmt.Printf("Record{ID: %d, Name: %s}\n", rec.ID, string(rec.Name[:6]))
memory.FreeListDealloc(fl, rec)
// Output:
// slot size: 64, content: hello from freelist
// Record{ID: 7, Name: widget}
}
// Example_shardedFreelist demonstrates ShardedFreeList: a sharded wrapper
// around FreeList with per-goroutine caches for near-zero contention under
// concurrent allocation. The API is identical to FreeList.
func Example_shardedFreelist() {
cfg := memory.DefaultFreeListConfig()
cfg.SlotSize = 64
cfg.SlabSize = 64 * 1024
cfg.SlabCount = 1
cfg.PoolSize = 1024 * 1024
cfg.Prealloc = true
sfl, err := memory.NewShardedFreeList(cfg, 64, 4)
if err != nil {
panic(err)
}
defer sfl.Free()
slot, err := sfl.Allocate()
if err != nil {
panic(err)
}
copy(slot, "hello from sharded freelist")
fmt.Printf("slot size: %d, content: %s\n", len(slot), string(slot[:27]))
sfl.Deallocate(slot)
// Output:
// slot size: 64, content: hello from sharded freelist
}
// Example_poolScoped demonstrates the bulk-free pattern: allocate multiple
// buffers for a logical scope (request, frame, batch), then free them all
// with a single Reset call.
func Example_poolScoped() {
cfg := memory.AllocatorConfig{
PoolSize: 1024 * 1024, // 1MB
SlabSize: 64 * 1024, // 64KB slabs
SlabCount: 2,
Prealloc: true,
}
pool, err := memory.NewPool(cfg, 64)
if err != nil {
panic(err)
}
defer pool.Free()
// Allocate three scratch buffers for a single logical operation.
header, _ := pool.Allocate(16)
body, _ := pool.Allocate(64)
trailer, _ := pool.Allocate(8)
copy(header, "HTTP/1.1 200 OK\r\n")
copy(body, `{"status":"ok"}`)
copy(trailer, "0\r\n\r\n")
fmt.Printf("used %d buffers, %d bytes total\n", 3, 16+64+8)
// Output: used 3 buffers, 88 bytes total
}