aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--db.go21
-rw-r--r--tx.go12
2 files changed, 20 insertions, 13 deletions
diff --git a/db.go b/db.go
index 9f80681..9048f6c 100644
--- a/db.go
+++ b/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)
}
diff --git a/tx.go b/tx.go
index 73538d9..1cfb4cd 100644
--- a/tx.go
+++ b/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