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

Garbage Collect empty storage keys (Neo 3) #824

Closed
igormcoelho opened this issue Jun 12, 2019 · 6 comments · Fixed by #825
Closed

Garbage Collect empty storage keys (Neo 3) #824

igormcoelho opened this issue Jun 12, 2019 · 6 comments · Fixed by #825
Labels
Discussion Initial issue state - proposed but not yet accepted
Milestone

Comments

@igormcoelho
Copy link
Contributor

igormcoelho commented Jun 12, 2019

I think a very nice policy for Neo is to automatically garbage-collect empty keys.

Example:

Storage.Put("x", 10);
Storage.Put("x", Storage.Get("x") - 10);

This is used a lot on NEP-5 tokens, and usually we have to put an extra if, because if key is not empty (or zero-valued), we call Storage.Delete. It could be done automatically, which is more simple for users, and more efficient for the network (no empty keys recorded, and they take space).

Note that, one slight change may be necessary (proposed on neo-vm project): we must ensure that BigInteger format is always expressed in its most compressed format... meaning that BigInteger(0) will now be expressed as an empty array (not "0x00"). This is easier to understand and more space efficient (and automatically help us with this garbage collection).

@igormcoelho
Copy link
Contributor Author

See: neo-project/neo-vm#170

@igormcoelho
Copy link
Contributor Author

igormcoelho commented Jun 12, 2019

This could also be useful for: #814, to prevent unnecessary Storage.Delete operations on NEP-5.

@igormcoelho
Copy link
Contributor Author

Implementation is simple: every time we do a Put (or PutEx), if value is an empty array, we call Storage.Delete automatically.

@shargon
Copy link
Member

shargon commented Jun 13, 2019

Then we can't do this neo-project/neo-vm#170 :)

shargon added a commit to shargon/neo that referenced this issue Jun 13, 2019
@erikzhang erikzhang added the Discussion Initial issue state - proposed but not yet accepted label Jun 13, 2019
@erikzhang
Copy link
Member

engine.CurrentContext.EvaluationStack.Push(item?.Value ?? new byte[0]);

For Storage.Get, if the key does not exist, it returns an empty bytes.

So, for Storage.Put, if the value is an empty bytes, we can just remove the entry.

This seems quite reasonable.

@igormcoelho
Copy link
Contributor Author

Then we can't do this neo-project/neo-vm#170 :)

In fact, it's the opposite @shargon , we need that! I'll show you on the tests there... it's all connected ;)

@erikzhang erikzhang added this to the NEO 3.0 milestone Jun 14, 2019
shargon added a commit that referenced this issue Jun 15, 2019
* Close #824

* optimize

* remove redundancy

* simplified

* allow const empty array

* Update InteropService.cs

* Update InteropService.cs
Thacryba pushed a commit to simplitech/neo that referenced this issue Feb 17, 2020
Tommo-L pushed a commit to Tommo-L/neo that referenced this issue 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
Discussion Initial issue state - proposed but not yet accepted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants