Skip to content
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

Fix memory leak when calling get_writebatch and avoid unnecessary clones #786

Merged

Conversation

jkurian
Copy link
Contributor

@jkurian jkurian commented May 29, 2023

Memory leak

It seems wi is not deallocated properly which causes a memory leak when calling get_writebatch.

rocksdb_transaction_get_writebatch_wi allocates memory with malloc, which never gets deallocated.

There is rocksdb_writebatch_wi_destroy but that tries to delete the memory allocated with malloc which is UB and tries to clear rep. Using rocksdb_free seems to be the right choice.

Extra allocations

There also seems to be unnecessary cloning of the serialized writebatch. The code clones the data once by calling to_owned on the slice but the data is also cloned here:
https://github.com/facebook/rocksdb/blob/main/db/c.cc#L1905

To improve that, I think we can just pass the ptr directly to rocksdb_writebatch_create_from

@jkurian jkurian changed the title Draft: Free memory on writebatch index and avoid unnecessary clones Draft: Free memory on writebatch_wi and avoid unnecessary clones May 29, 2023
@jkurian jkurian changed the title Draft: Free memory on writebatch_wi and avoid unnecessary clones Free memory on writebatch_wi and avoid unnecessary clones May 29, 2023
@jkurian jkurian changed the title Free memory on writebatch_wi and avoid unnecessary clones FIx memory leak when calling get_writebatch and avoid unnecessary clones May 29, 2023
@jkurian jkurian changed the title FIx memory leak when calling get_writebatch and avoid unnecessary clones Fix memory leak when calling get_writebatch and avoid unnecessary clones May 29, 2023
@jkurian jkurian force-pushed the task/fix-writebatch-wi-memory-leak branch from e88f8f1 to 14fecf0 Compare May 29, 2023 19:13
Copy link
Member

@stanislav-tkach stanislav-tkach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pull request! Would you mind creating an issue about rocksdb_writebatch_wi_destroy in the rocksdb repository?

@aleksuss aleksuss merged commit 8cf8996 into rust-rocksdb:master Jun 5, 2023
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants