mirror of https://github.com/etcd-io/bbolt.git
Add madvise() after mmap().
This commit advises the mmapped data file to use MADV_RANDOM to avoid readahead. This can provide a performance boost to Bolt databases that are larger than memory by avoiding unnecessary disk i/o.pull/34/head
parent
afceb316b9
commit
88f777f332
14
bolt_unix.go
14
bolt_unix.go
|
@ -63,6 +63,11 @@ func mmap(db *DB, sz int) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Advise the kernel that the mmap is accessed randomly.
|
||||
if err := madvise(b, syscall.MADV_RANDOM); err != nil {
|
||||
return fmt.Errorf("madvise: %s", err)
|
||||
}
|
||||
|
||||
// Save the original byte slice and convert to a byte array pointer.
|
||||
db.dataref = b
|
||||
db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0]))
|
||||
|
@ -84,3 +89,12 @@ func munmap(db *DB) error {
|
|||
db.datasz = 0
|
||||
return err
|
||||
}
|
||||
|
||||
// NOTE: This function is copied from stdlib because it is not available on darwin.
|
||||
func madvise(b []byte, advice int) (err error) {
|
||||
_, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), uintptr(advice))
|
||||
if e1 != 0 {
|
||||
err = e1
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue