-
Notifications
You must be signed in to change notification settings - Fork 15
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
avoid altering amt when empty #22
Conversation
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'd expect all nodes to have this bitfield, empty or otherwise.
@@ -3,6 +3,7 @@ package amt | |||
import ( | |||
"context" | |||
"fmt" | |||
"github.com/stretchr/testify/require" |
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.
import order nit
I think I agree with @Stebalien , this value should always be set and never should be serialized as nil. |
amt.go
Outdated
@@ -495,11 +495,14 @@ func (n *Node) Flush(ctx context.Context, bs cbor.IpldStore, depth int) error { | |||
if len(n.expVals) == 0 { | |||
return nil | |||
} | |||
n.Bmap = []byte{0} | |||
n.Bmap = nil |
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.
always defaulting to having the bitfield set would just mean moving this line (before this change) to above the if
Fix for byte arrays: whyrusleeping/cbor-gen#29 |
Fix merged. |
* Saves an allocation. * Importantly, the bitfield is _always_ initialized. Breaking: This will refuse to decode AMTs with mis-sized bitfields.
Fix pushed. Could you take a look at this @whyrusleeping and @acruikshank? |
closes #18
Motivation
Actions like calling
ForEach
or adding and removing elements put an empty AMT into a state that causes it to be serialized differently even though it is still the empty array. This is because these actions causeexpVals
to be expanded in the node from a nil value to a slice with nil values. WhileexpVals
isn't itself serialized, The fact that it is non-nil causes the node'sBmap
value to be initialized from a nil value to[]byte{0}
inFlush
, and that does serialize differently.Proposed Changes
Node.Bmap
lazily inFlush
.This is probably the simplest solution that fixes the problem. It leaves open a bigger question about whether empty nodes should be structurally different when serialized from nodes containing one or more elements. I find it a little surprising, but not really wrong.