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.
Ben Johnson
8 years ago
62 | 62 | return err |
63 | 63 | } |
64 | 64 | |
65 | // Advise the kernel that the mmap is accessed randomly. | |
66 | if err := madvise(b, syscall.MADV_RANDOM); err != nil { | |
67 | return fmt.Errorf("madvise: %s", err) | |
68 | } | |
69 | ||
65 | 70 | // Save the original byte slice and convert to a byte array pointer. |
66 | 71 | db.dataref = b |
67 | 72 | db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0])) |
83 | 88 | db.datasz = 0 |
84 | 89 | return err |
85 | 90 | } |
91 | ||
92 | // NOTE: This function is copied from stdlib because it is not available on darwin. | |
93 | func madvise(b []byte, advice int) (err error) { | |
94 | _, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), uintptr(advice)) | |
95 | if e1 != 0 { | |
96 | err = e1 | |
97 | } | |
98 | return | |
99 | } |