I have a contract that has a struct including address and unit, and mapping of a struct, like this.
struct User {
uint count;
address userAddress;
}
mapping (address => User) users;
There is a point at which I need to make a copy of this mapping and clear out the entries of the old one, like this pseudo code:
users_backup = users;
users = [];
Of course this code won't work, so I learned you can loop through it like this:
Assignment of mapping in Solidity
But that seems expensive, especially after reading:
Copying a mapping from Contract A to contract B
So How do I do this efficiently?
With a mapping of 100k users or 1 million or even 100 million entries would this looping method be feasible? Or would it be better to change my architecture?
uint[] userCounts;
address[] userAddresses;
I could use a series of arrays instead of maps. Then, would I be able to copy one array to a new variable and clear the old variable out in one or two lines with less gas? The Hard thing is, even the arrays will have to be variable arrays because I can't know beforehand how many users will use it.
What would you do to minimize gas consumption? I'm just not sure what the best pattern is, thanks!
users_backup
variable). but I'm new to solidity so that seemed more complex than just resetting all the variables in the same contract...There is a point at which I need to make a copy of this mapping and clear out the entries of the old one
- now, why would you ever wanna do something like that? I recommend that you review your design and find out how you got to this point to begin with. I can't think of any reason whatsoever, of having to replicate a mapping and then clear the old one.