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

O(1) Clone of BTree. #16

Merged
merged 1 commit into from
Dec 13, 2016
Merged

O(1) Clone of BTree. #16

merged 1 commit into from
Dec 13, 2016

Conversation

gconnell
Copy link
Collaborator

This change allows a BTree user to quickly clone the tree, using copy-on-write
logic to modify shared state. Both trees continue to reference the original
tree's nodes for reading, thus the clone is O(1).
Writes to either the original or new tree after the clone copy nodes instead
of modifying them. These operations remain O(logn), but slow down slightly due
to the additional mallocs/copies necessitated by the copy-on-write logic.

Uses a copyOnWriteContext pointer at the tree and node level to determine
which nodes are able to be modified and which are read-only and must be
copied before modification.

To simplify things, freelists have been made safe for concurrent access.
With copy-on-write, keeping track of which freelists are being used by
which btrees is very difficult. Consider the case:

t1 := New()
t2 := t1.Clone()

What freelist should t1 and t2 have? Can you write to both t1 and t2 at
once? With this, the answers are "they're sharing one", and "yes".

@gconnell gconnell mentioned this pull request Dec 12, 2016
This change allows a BTree user to quickly clone the tree, using copy-on-write
logic to modify shared state.  Both trees continue to reference the original
tree's nodes for reading, thus the clone is O(1).
Writes to either the original or new tree after the clone copy nodes instead
of modifying them.  These operations remain O(logn), but slow down slightly due
to the additional mallocs/copies necessitated by the copy-on-write logic.

Uses a copyOnWriteContext pointer at the tree and node level to determine
which nodes are able to be modified and which are read-only and must be
copied before modification.

To simplify things, freelists have been made safe for concurrent access.
With copy-on-write, keeping track of which freelists are being used by
which btrees is very difficult.  Consider the case:

```go
t1 := New()
t2 := t1.Clone()
```

What freelist should t1 and t2 have?  Can you write to both t1 and t2 at
once?  With this, the answers are "they're sharing one", and "yes".
@gconnell gconnell merged commit 0c3044b into google:master Dec 13, 2016
@gconnell gconnell deleted the cow branch December 13, 2016 16:32
@tidwall
Copy link
Contributor

tidwall commented Jan 9, 2017

@gconnell Just gotta say that this feature is an excellent addition!

nvanbenschoten added a commit to nvanbenschoten/cockroach that referenced this pull request Nov 6, 2018
nvanbenschoten added a commit to nvanbenschoten/cockroach that referenced this pull request Nov 13, 2018
nvanbenschoten added a commit to nvanbenschoten/cockroach that referenced this pull request Nov 13, 2018
nvanbenschoten added a commit to nvanbenschoten/cockroach that referenced this pull request Nov 15, 2018
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.

2 participants