Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(218)

Side by Side Diff: tools/slab/slab.go

Issue 1409173004: Remove usage of unsafe from gkvlite (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/gkvlite.git@master
Patch Set: get locks out of other lock Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « store_test.go ('k') | tools/slab/slab_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 package main 1 package main
2 2
3 // Test integration of gkvlite with go-slab, using gkvlite's optional 3 // Test integration of gkvlite with go-slab, using gkvlite's optional
4 // ItemAddRef/ItemDecRef() callbacks to integrate with a slab memory 4 // ItemAddRef/ItemDecRef() callbacks to integrate with a slab memory
5 // allocator. 5 // allocator.
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "flag" 9 "flag"
10 "fmt" 10 "fmt"
11 "io" 11 "io"
12 "log" 12 "log"
13 "math/rand" 13 "math/rand"
14 "os" 14 "os"
15 "sort" 15 "sort"
16 "testing" 16 "testing"
17 "unsafe"
18 17
19 "github.com/luci/gkvlite" 18 "github.com/luci/gkvlite"
20 "github.com/steveyen/go-slab" 19 "github.com/steveyen/go-slab"
21 ) 20 )
22 21
23 var maxOps = flag.Int("ops", 0, 22 var maxOps = flag.Int("ops", 0,
24 "max number of ops; 0 means run forever") 23 "max number of ops; 0 means run forever")
25 var maxItems = flag.Int("n", 10000, 24 var maxItems = flag.Int("n", 10000,
26 "max number of items") 25 "max number of items")
27 var maxItemBytes = flag.Int("maxItemBytes", 20000, 26 var maxItemBytes = flag.Int("maxItemBytes", 20000,
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 return nil 159 return nil
161 } 160 }
162 itemAlloc := func(c *gkvlite.Collection, keyLength uint32) *gkvlite.Item { 161 itemAlloc := func(c *gkvlite.Collection, keyLength uint32) *gkvlite.Item {
163 var n *ItemNode 162 var n *ItemNode
164 if freeItemNodes != nil { 163 if freeItemNodes != nil {
165 n = freeItemNodes 164 n = freeItemNodes
166 freeItemNodes = freeItemNodes.next 165 freeItemNodes = freeItemNodes.next
167 n.next = nil 166 n.next = nil
168 } else { 167 } else {
169 n = &ItemNode{} 168 n = &ItemNode{}
170 » » » n.item.Transient = unsafe.Pointer(n) 169 » » » n.item.Transient = n
171 } 170 }
172 if n.refs != 0 || 171 if n.refs != 0 ||
173 n.item.Key != nil || n.item.Val != nil || n.item.Priorit y != 0 || 172 n.item.Key != nil || n.item.Val != nil || n.item.Priorit y != 0 ||
174 » » » n.item.Transient != unsafe.Pointer(n) { 173 » » » n.item.Transient != n {
175 panic("unexpected ItemNode refs or item fields") 174 panic("unexpected ItemNode refs or item fields")
176 } 175 }
177 n.refs = 1 176 n.refs = 1
178 n.next = nil 177 n.next = nil
179 n.item.Key = arena.Alloc(int(keyLength)) 178 n.item.Key = arena.Alloc(int(keyLength))
180 return &n.item 179 return &n.item
181 } 180 }
182 itemAddRef := func(c *gkvlite.Collection, i *gkvlite.Item) { 181 itemAddRef := func(c *gkvlite.Collection, i *gkvlite.Item) {
183 » » n := (*ItemNode)(i.Transient) 182 » » n := i.Transient.(*ItemNode)
184 n.refs++ 183 n.refs++
185 } 184 }
186 itemDecRef := func(c *gkvlite.Collection, i *gkvlite.Item) { 185 itemDecRef := func(c *gkvlite.Collection, i *gkvlite.Item) {
187 » » n := (*ItemNode)(i.Transient) 186 » » n := i.Transient.(*ItemNode)
188 n.refs-- 187 n.refs--
189 if n.refs == 0 { 188 if n.refs == 0 {
190 if i.Key == nil { 189 if i.Key == nil {
191 panic("expected a Key") 190 panic("expected a Key")
192 } 191 }
193 arena.DecRef(i.Key) 192 arena.DecRef(i.Key)
194 i.Key = nil 193 i.Key = nil
195 if i.Val != nil { 194 if i.Val != nil {
196 arena.DecRef(i.Val) 195 arena.DecRef(i.Val)
197 i.Val = nil 196 i.Val = nil
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 for _, k := range mk { 350 for _, k := range mk {
352 log.Printf("%s = %d", k, arenaStats[k]) 351 log.Printf("%s = %d", k, arenaStats[k])
353 } 352 }
354 } 353 }
355 log.Printf("i: %d, numGets: %d, numSets: %d, numDeletes: %d"+ 354 log.Printf("i: %d, numGets: %d, numSets: %d, numDeletes: %d"+
356 ", numEvicts: %d, numReopens: %d, numFlushes: %d \n", 355 ", numEvicts: %d, numReopens: %d, numFlushes: %d \n",
357 i, numGets, numSets, numDeletes, numEvicts, numR eopens, numFlushes) 356 i, numGets, numSets, numDeletes, numEvicts, numR eopens, numFlushes)
358 } 357 }
359 } 358 }
360 } 359 }
OLDNEW
« no previous file with comments | « store_test.go ('k') | tools/slab/slab_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698