-
Notifications
You must be signed in to change notification settings - Fork 968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
optimize put by reuse memory of kvData before #379
base: master
Are you sure you want to change the base?
optimize put by reuse memory of kvData before #379
Conversation
the next node of the previous node is current node, so we can use node to replace p.nodeData[m];
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good advise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought some more about this. I think it violates the API. There are some methods in the memdb, e.g. Find
, and Get
, which returns the value
slice from the kvData
buffer.
They are documented with The caller should not modify the contents of the returned slice
, implying that the returned value has not been copied, but is a direct reference to the backing data.
With this PR, a data-race is introduced, since the readlock is only held during the Get/Find, but not afterwards.
- Routine 1: Get(aaa) -> returns 'bbb'
- Routine 2: Put(aaa, cc) -> overwrites 'bbb' with
cc
.
And now, when Routine 1 looks at the data, it sees bbc
. Hence why the kvData needs to remain append-only, and cannot be modified in-place.
About that scenario ^ it would be good with a testcase for that.
It should hold true for both disk-db and memdb |
It might be that this actually already works, since leveldb uses sequence numbers overlaid on memdb. So this change does make memdb api unsafe, but it won't matter when the memdb is used through a regular db -- it won't Which still is a bit dangerous, because it's a very subtle bug which will be hard to find. However, due to this usage of sequence numbers, I think the optimization is probably moot aswell. |
very great thoughts |
optimize the put