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

Remove empty keys #825

Merged
merged 8 commits into from
Jun 15, 2019
Merged

Remove empty keys #825

merged 8 commits into from
Jun 15, 2019

Conversation

shargon
Copy link
Member

@shargon shargon commented Jun 13, 2019

Close #824

@shargon shargon requested review from igormcoelho and erikzhang June 13, 2019 06:57
@codecov-io
Copy link

codecov-io commented Jun 13, 2019

Codecov Report

Merging #825 into master will decrease coverage by 0.01%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #825      +/-   ##
=========================================
- Coverage   38.41%   38.4%   -0.02%     
=========================================
  Files         176     176              
  Lines       12459   12463       +4     
=========================================
  Hits         4786    4786              
- Misses       7673    7677       +4
Impacted Files Coverage Δ
neo/SmartContract/InteropService.cs 20.89% <0%> (-0.19%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fe3ab04...815fcb4. Read the comment docs.

// If is empty, we try to remove it

engine.Snapshot.Storages.Delete(skey);
return true;
Copy link
Member

Choose a reason for hiding this comment

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

If I put an empty bytes with StorageFlags.Constant flag, what will happen?

Copy link
Member Author

Choose a reason for hiding this comment

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

The expected behaviour is: write nothing, read empty bytes

Copy link
Contributor

@igormcoelho igormcoelho Jun 13, 2019

Choose a reason for hiding this comment

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

What's an example use case for const? I think you're right... logically speaking, if someone wants to keep a constant value, that value could be zero. So we need to keep it allow it to be unchanged in the future. We cannot pre-reap const empty values @shargon, because it may be part of contract logic, and perhaps be re-written in the future because we accidentally pre-dropped its "constness".

Copy link
Contributor

@igormcoelho igormcoelho Jun 13, 2019

Choose a reason for hiding this comment

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

Only explicit Delete will destroy them, right @erikzhang ? I just noticed Delete cannot reap const values too. So these are contract-lifetime values, correct? Only Contract.Destroy or Migrate can reap them.

igormcoelho
igormcoelho previously approved these changes Jun 13, 2019
Copy link
Contributor

@igormcoelho igormcoelho left a comment

Choose a reason for hiding this comment

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

Looks good for me, a huge advance for NEP-5!

igormcoelho
igormcoelho previously approved these changes Jun 13, 2019
neo/SmartContract/InteropService.cs Outdated Show resolved Hide resolved
@igormcoelho
Copy link
Contributor

@shargon this will guarantee that value zero will always be converted to an empty array, thus, ripped out automatically 💪
neo-project/neo-vm#171

@vncoelho
Copy link
Member

Such a nice PR and quick, Shargon.
Congratulations to you and Igor for the insight and proposal.

@igormcoelho
Copy link
Contributor

@erikzhang I just noticed this will be useless for NEP5 Native Tokens, because each Key is being mapped to a TState object, not a BigInteger as I thought before...
Although this will still be fundamental for user-made NEP5, I wonder if we could directly put amount in a user key, instead of TState object? And perhaps, put TState somewhere else, or even remove it? Votes and Gas redistribution could still be made in connected contracts, this way is too attached to apply optimizations like the ones I was thinking. This will also be a challenge if we want to reuse Native NEP5 sketch for hand-made tokens, as in the inheritance proposal.

@igormcoelho
Copy link
Contributor

This PR could be very helpful: #831
Issue: #830

@shargon
Copy link
Member Author

shargon commented Jun 14, 2019

I think that i have an optimization

item.Value = value;
item.IsConstant = flags.HasFlag(StorageFlags.Constant);

if (engine.Snapshot.Storages.TryGet(skey)?.IsConstant == true) return false;
Copy link
Member Author

Choose a reason for hiding this comment

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

If we could access here to TrackableItem, we can save the GetAndChange

Copy link
Member

Choose a reason for hiding this comment

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

GetAndChange here doesn't access the internal storage. It read from the cache because we call TryGet before.

Copy link
Member Author

Choose a reason for hiding this comment

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

But if we can know if the item has trackable.State = TrackState.Added; we can avoid the two reads

Copy link
Member

Choose a reason for hiding this comment

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

If it is TrackState.Added, both the TryGet and GetAndChange calls read from the cache. These are all automatic.

@shargon shargon merged commit d411153 into neo-project:master Jun 15, 2019
@shargon shargon deleted the delete-on-empty branch June 15, 2019 08:50
@erikzhang erikzhang modified the milestone: NEO 3.0 Dec 6, 2019
Tommo-L pushed a commit to Tommo-L/neo that referenced this pull request Jun 22, 2020
* Close neo-project#824

* optimize

* remove redundancy

* simplified

* allow const empty array

* Update InteropService.cs

* Update InteropService.cs
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.

Garbage Collect empty storage keys (Neo 3)
5 participants