mirror of https://github.com/etcd-io/bbolt.git
move page pool to db
parent
9145d586f2
commit
f5f0f7af77
21
db.go
21
db.go
|
@ -110,6 +110,8 @@ type DB struct {
|
|||
freelist *freelist
|
||||
stats Stats
|
||||
|
||||
pagePool sync.Pool
|
||||
|
||||
batchMu sync.Mutex
|
||||
batch *batch
|
||||
|
||||
|
@ -209,6 +211,13 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// Initialize page pool.
|
||||
db.pagePool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, db.pageSize)
|
||||
},
|
||||
}
|
||||
|
||||
// Memory map the data file.
|
||||
if err := db.mmap(options.InitialMmapSize); err != nil {
|
||||
_ = db.close()
|
||||
|
@ -324,7 +333,7 @@ func (db *DB) mmapSize(size int) (int, error) {
|
|||
// init creates a new database file and initializes its meta pages.
|
||||
func (db *DB) init() error {
|
||||
// Set the page size to the OS page size.
|
||||
db.pageSize = defaultPageSize
|
||||
db.pageSize = os.Getpagesize()
|
||||
|
||||
// Create two meta pages on a buffer.
|
||||
buf := make([]byte, db.pageSize*4)
|
||||
|
@ -787,18 +796,12 @@ func (db *DB) meta() *meta {
|
|||
return db.meta1
|
||||
}
|
||||
|
||||
var pagePool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, defaultPageSize)
|
||||
},
|
||||
}
|
||||
|
||||
// allocate returns a contiguous block of memory starting at a given page.
|
||||
func (db *DB) allocate(count int) (*page, error) {
|
||||
// Allocate a temporary buffer for the page.
|
||||
var buf []byte
|
||||
if count == 1 && db.pageSize == defaultPageSize {
|
||||
buf = pagePool.Get().([]byte)
|
||||
if count == 1 {
|
||||
buf = db.pagePool.Get().([]byte)
|
||||
} else {
|
||||
buf = make([]byte, count*db.pageSize)
|
||||
}
|
||||
|
|
12
tx.go
12
tx.go
|
@ -519,17 +519,21 @@ func (tx *Tx) write() error {
|
|||
}
|
||||
}
|
||||
|
||||
// put small pages back to sync.Pool
|
||||
// Put small pages back to page pool.
|
||||
for _, p := range pages {
|
||||
if int(p.overflow) != 0 || tx.db.pageSize != defaultPageSize {
|
||||
// Ignore page sizes over 1 page.
|
||||
// These are allocated using make() instead of the page pool.
|
||||
if int(p.overflow) != 0 {
|
||||
continue
|
||||
}
|
||||
buf := (*[maxAllocSize]byte)(unsafe.Pointer(p))[:defaultPageSize]
|
||||
|
||||
buf := (*[maxAllocSize]byte)(unsafe.Pointer(p))[:tx.db.pageSize]
|
||||
|
||||
// See https://go.googlesource.com/go/+/f03c9202c43e0abb130669852082117ca50aa9b1
|
||||
for i := range buf {
|
||||
buf[i] = 0
|
||||
}
|
||||
pagePool.Put(buf)
|
||||
tx.db.pagePool.Put(buf)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
Loading…
Reference in New Issue