Cosmos-sdk: Reorder variables structs to optimize memory

Created on 14 Jun 2018  路  3Comments  路  Source: cosmos/cosmos-sdk

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: https://github.com/golang/go/issues/10014), so we'll have to reorder all of our structs ourselves.

good first issue help wanted performance

Most helpful comment

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.

All 3 comments

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.

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)

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)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ValarDragon picture ValarDragon  路  3Comments

jackzampolin picture jackzampolin  路  3Comments

ssssssu12 picture ssssssu12  路  3Comments

ValarDragon picture ValarDragon  路  3Comments

hendrikhofstadt picture hendrikhofstadt  路  3Comments