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

Reorder variables structs to optimize memory #1257

Closed
ValarDragon opened this issue Jun 14, 2018 · 3 comments
Closed

Reorder variables structs to optimize memory #1257

ValarDragon opened this issue Jun 14, 2018 · 3 comments

Comments

@ValarDragon
Copy link
Contributor

ValarDragon commented Jun 14, 2018

Read this first: https://medium.com/@felipedutratine/how-to-organize-the-go-struct-in-order-to-save-memory-c78afcf59ec2 (Its short)

We should reorder the variables in all of our structs, to save on memory used. I've tagged this is as prelaunch, since I'm not sure if this can be changed easily postlaunch, as it may affect state variables / data encoding. (I don't actually know though, anyone who knows if our codec cares about the order variables appear in, feel free to chime in. Would be glad to make this post-launch if wire.codec didn't care about the order variables appeared in) From what I can gather, it doesn't look like this will be done automatically for us before go 2.0 (judging from this issue: golang/go#10014), so we'll have to reorder all of our structs ourselves.

@ValarDragon
Copy link
Contributor Author

ValarDragon commented Jun 14, 2018

This doesn't need to be done prelaunch, since all things that are put into memory are marshalled. Still would be cool to have, though its a bit of a micro-optimization.

@ValarDragon
Copy link
Contributor Author

Running https://github.com/ValarDragon/maligned, we get that we should update the following structs:

$ gometalinter --disable-all --linter='maligned:maligned -v :PATH:LINE:MESSAGE' --enable='maligned' --vendor ./...
x/stake/client/rest/query.go:84::warning: 27: struct of size 600 could be 592. Reorder struct variables in the following order {PoolShares, Commission, CommissionMax, CommissionChangeToday, DelegatorShares, Description, CommissionChangeRate, PrevBondedShares, ProposerRewardPool, Owner, PubKey, BondHeight, BondIntraTxCounter, Revoked} (maligned)
store/iavlstore.go:194::warning: 19: struct of size 152 could be 144. Reorder struct variables in the following order {start, end, key, value, tree, quitCh, initCh, iterCh, mtx, invalid, ascending} (maligned)
server/init.go:58::warning: 17: struct of size 48 could be 40. Reorder struct variables in the following order {ChainID, GenTxsDir, GenTxs, Overwrite} (maligned)
x/stake/validator.go:21::warning: 16: struct of size 608 could be 600. Reorder struct variables in the following order {PoolShares, Commission, CommissionChangeToday, CommissionChangeRate, DelegatorShares, Description, CommissionMax, PrevBondedShares, Owner, ProposerRewardPool, PubKey, BondHeight, BondIntraTxCounter, Revoked} (maligned)

@ValarDragon
Copy link
Contributor Author

Decided to close this in favor of leaving structs in their logical ordering. (It doesn't save that many bytes either way, so its not that impactful)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants