bbolt icon indicating copy to clipboard operation
bbolt copied to clipboard

boltdb write data by os.file.writeAt, why not use mmap() to write data into db ?

Open Lawliet-Chan opened this issue 7 years ago • 5 comments

I find boltdb only use mmap() to read data, but you do not use mmap() to write data . you just use os.file.writeAt() to write data into db. The code I find as follow: db.ops.writeAt = db.file.WriteAt

why not use mmap()? It can write data faster.

Lawliet-Chan avatar Nov 03 '18 10:11 Lawliet-Chan

Not exactly sure what you mean. But if you think there can be some performance gains, can you try to modify the code and do some benchmark to show the result first?

xiang90 avatar Nov 03 '18 22:11 xiang90

ok, I will manage to find time to modify some code and benchmark .

Thank you for your prompt reply.

Lawliet-Chan avatar Nov 04 '18 00:11 Lawliet-Chan

If you directly update the mmaped pages, how do you guarantee the OS kernel flush the dirty pages to disk as you want?

louchenyao avatar Apr 24 '19 16:04 louchenyao

If you directly update the mmaped pages, how do you guarantee the OS kernel flush the dirty pages to disk as you want?

@louchenyao, there is 'msync' function for that.

zenovich avatar Oct 13 '21 19:10 zenovich

It should be possible to simply copy the pages onto the existing memory mapping, I tried the following but it wasn't sufficient to get it working. Someone more familiar with the code might be able to figure out how to handle things like locking and truncation:

db.ops.writeAt = func(b []byte, off int64) (n int, err error) {
	copy(db.data[off:], b)
	return len(b), nil
}

as mentioned by @Lawliet-Chan this could allow significantly faster writes, particularly when NoSync is enabled as the pages are just written to RAM and left up to the kernel to decide how/when to flush them to disk.

note: the 'protection bits' used when creating the mapping will need to be changed to syscall.PROT_READ|syscall.PROT_WRITE

missinglink avatar Feb 10 '22 13:02 missinglink