-
Notifications
You must be signed in to change notification settings - Fork 99
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
add share/unshare for class objects #180
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,17 @@ public class HashMap<K,V> ( | |
var table : [var KVs<K,V>] = [var]; | ||
var _count : Nat = 0; | ||
|
||
/// Get purely-functional representation | ||
public func share() : ([KVs<K,V>], Nat) { | ||
(A.freeze table, _count) | ||
}; | ||
|
||
/// Put purely-functional representation into class. Need to make sure the tree is constructed with the same Eq and Hash functions | ||
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. I vote that we either write those checking functions and use them, or remove this until we have them? Is this a performance thing? 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. The main use case is for upgrade. We cannot make HashMap stable, so the upgrade code would look like:
The db can be very large when upgrade, it's probably too expensive to check for the invariants.Also for the upgrade case, the invariants always hold. 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. 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. I'm curious how BigMap handles upgrade? 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. It doesn't handle it at the moment, but similar issues will arise (how to enforce invariants for the serialized representation without rechecking them). Also, I think this question will arise for each developer, each time they implement such functions, for any canister with data invariants (so, almost all of them). Perhaps we should add some notes in the design doc for 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. https://github.com/dfinity/motoko-base/blob/master/doc/design.md#classes Currently reads as follows:
If we follow this guide, it seems like we should be using a vector of key-value pairs for things like BigMap, and for other map-like structures, and shouldn't even include the hash values if they were computed for internal use only. 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. Indeed, if at all possible, let's make such functions safe. Otherwise you break encapsulation. I doubt that the copying with freeze and thaw are significantly cheaper than a safe alternative. Also, for polymorphic data structures, these functions need to be parameterised over the share/unshare functions for each type parameter, otherwise you cannot compose them properly. |
||
public func unsafeUnshare(t : [KVs<K,V>], count : Nat) { | ||
table := A.thaw(t); | ||
_count := count; | ||
}; | ||
|
||
/// Returns the number of entries in this HashMap. | ||
public func size() : Nat = _count; | ||
|
||
|
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.
Is there an
else
missing here? Otherwise this will trap when empty.