This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Preserve cache on reverting the snapshot #2488
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,6 +108,20 @@ impl AccountEntry { | |
state: AccountState::Clean, | ||
} | ||
} | ||
|
||
// Replace data with another entry but preserve storage cache | ||
fn merge_snapshot(&mut self, other: AccountEntry) { | ||
self.state = other.state; | ||
match other.account { | ||
None => self.account = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. more readable to keep |
||
Some(acc) => match self.account { | ||
Some(ref mut ours) => { | ||
ours.merge_with(acc); | ||
}, | ||
None => {}, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
/// Representation of the entire state of all accounts in the system. | ||
|
@@ -210,8 +224,12 @@ impl State { | |
let last = self.snapshots.borrow_mut().pop(); | ||
if let Some(mut snapshot) = last { | ||
if let Some(ref mut prev) = self.snapshots.borrow_mut().last_mut() { | ||
for (k, v) in snapshot.drain() { | ||
prev.entry(k).or_insert(v); | ||
if prev.is_empty() { | ||
**prev = snapshot; | ||
} else { | ||
for (k, v) in snapshot.drain() { | ||
prev.entry(k).or_insert(v); | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -223,7 +241,16 @@ impl State { | |
for (k, v) in snapshot.drain() { | ||
match v { | ||
Some(v) => { | ||
self.cache.borrow_mut().insert(k, v); | ||
match self.cache.borrow_mut().entry(k) { | ||
Entry::Occupied(mut e) => { | ||
// Merge snapshotted changes back into the main account | ||
// storage preserving the cache. | ||
e.get_mut().merge_snapshot(v); | ||
}, | ||
Entry::Vacant(e) => { | ||
e.insert(v); | ||
} | ||
} | ||
}, | ||
None => { | ||
match self.cache.borrow_mut().entry(k) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
what if the new storage is dirty but the
other
account is clean?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.
Its the other way round.
other
contains "new" changes that replaceself
. Everything inself
is discarded except for the storage cacheThere 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.
maybe rename
other
tonewer
, oroverride
.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.
also it's not really
merge
- it's more likeoverlay
orreplace
.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.
Aren't the items in the cache dirtier than those in the snapshots? In
revert_snapshot
it merges the snapshotted account into the cached, overwriting the cached account with the (cleaner) snapshotted account while preserving the dirty storage (whichrevert_snapshot
should be throwing away?)The name is a bit confusing too, it seems like it would be used in
clear_snapshot
but is in fact only used when reverting. (edit: looks like Gav got to this point first)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.
@rphmeier That's correct. Dirty storage is discarded, but the clean storage cache is preserved
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.
Renamed into
replace_with
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.
just to clarify, the purpose is basically to completely replace the cached account with the snapshotted block, while also preserving any new cached storage values since the snapshotted version was put away?
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.
yes