-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Use the Linked List to achieve a FIFO access. #1660
Comments
I thought for a while, and I realize a key point, I want to remove elements by their order,
then I can record their sequence without an array, so I should write like:
So all modified code should be:
|
OK, please just give me some advice, and I will make a standard code |
Ok, I will think my requirements seriously, and then share it. |
The biggest problem here is the lack of generics in solidity. This means that if we implement a list for uint256, we will have to implement another one for addresses, and another one for your custom struct. |
I don't think Solidity will be adding support for generics soon, and would like to at least get an initial implementation for common types, like |
We should implement for I thought this issue was about linked lists? I don't understand how a circular buffer would work in the EVM where it's not possible (and not reasonable?) to "lock" until a slot in the buffer is available. |
I think linked lists would be a useful structure to have in the EVM but the motivation in this issue is not the right one. It's not necessary to use space efficiently in the EVM because there is effectively an infinit amount of it. A FIFO queue can be implemented by a dynamic array in storage. Rearranging elements is not necessary because you can assume that there is infinit room to grow the array. |
Heh, I got carried away with the circular buffer idea. An array-based FIFO is definitely possible and cheaper in the EVM: we'd just need a read index to read elements (which are |
I am not sure, in my opinion, if I do not recycle space, I think it will be a little weird, because when we delete the first element in an array, array[0] still exists, but its value becomes undefined, so maybe when I try to update an element in the array, I should add a extra requirement |
Yeah, maybe we can try it in this way. |
You do get the recycling of space, since you zero-out the slot. This is roughly what I (and I think @frangio) mean (untested!):
|
I become a little confused, you said
Just like your code, I call |
On the EVM, all storage slots exist (they can be read from and written to), and are initialized with 0. Nodes simply keep track of which ones have been written with a non-zero value, and delete slots from that list when you write zero to them. It is because of this that changing a value from zero to non-zero is more expensive than from non-zero to non-zero, and why changing it back to zero refunds gas. |
Oh, I see, thanks for your explanation. |
@Skyge I think this issue is larger than simply coding a FIFO queue, since e.g. even if we did it for |
OK |
Hi, @nventuro I can not find the post in the forum, so do we delete it? |
@Skyge It had been deleted but I think it was an accident. I have restored it now. Apologies. |
🧐 Motivation
A FIFO access with Linked List in the Solidity.
📝 Details
In my problem, there exists such a situation: I want to store an array structure, such as
Because I want to
push
the newest, andpop
the oldest,so there may be an array to store their sequence, such as
In my project, recording order is essential.
but when we push in some elements and pop out some elements, we will find that
the first half of the array is empty. so just like in an Array List, in order to use space efficiently,
we would move the second half of the array to the empty position,
such like this:
ok, now you will understand what I want to achieve, a FIFO access,
and according to @nventuro 's advice,
it could be a circular buffer.
(thanks for @nventuro at here!),
but we all know that, every operation in the EVM will cost some gas,
so it should be better to achieve by Linked List,
because it can remove element efficiently,
and this is what I want to find out, because I really would run the function of
delete()
frequently.So now the problem comes out, how to achieve a Linked List in the Solidity?
And what I need likes this:
The text was updated successfully, but these errors were encountered: