Skip to content
This repository has been archived by the owner on Mar 9, 2019. It is now read-only.

Commit

Permalink
Merge pull request #383 from benbjohnson/madvise
Browse files Browse the repository at this point in the history
Add madvise() after mmap().
  • Loading branch information
benbjohnson committed Jun 3, 2015
2 parents afceb31 + 88f777f commit 8f42da0
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions bolt_unix.go
Expand Up @@ -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]))
Expand All @@ -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
}

0 comments on commit 8f42da0

Please sign in to comment.