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

More efficient GST2 implementation #6

Open
fulldecent opened this issue Sep 16, 2018 · 4 comments
Open

More efficient GST2 implementation #6

fulldecent opened this issue Sep 16, 2018 · 4 comments

Comments

@fulldecent
Copy link

fulldecent commented Sep 16, 2018

Just wanted to say this is a cute project. I have been studying and wanted to share my notes.


Current contract call costs 22 gas.

https://github.com/projectchicago/gastoken/blob/master/contract/GST2_ETH.sol#L106-L114

Gas Set Inst
3 VERYLOW PUSH15 0xb3f879cb30fe243b4dfee438691c04
2 BASE CALLER
3 VERYLOW XOR
2 BASE PC
10 HIGH JUMPI
2 BASE CALLER
SELFDESTRUCT

Proposed update requires 19 gas.

Gas Set Inst
3 VERYLOW PUSH15 0xb3f879cb30fe243b4dfee438691cXX
2 BASE CALLER
3 VERYLOW XOR
8 MID JUMP
1 JUMPDEST JUMPDEST
2 BASE CALLER
SELFDESTRUCT

With this new technique, the comment on the homepage can be updated:

The address of our GST2 GasToken, 0x0000000000b3F879cb30FE243b4Dfee438691c04, is hardcoded. Note that this address is very short (only 15 non-zero bytes instead of 20). We generated our GasToken contract at such an address on purpose, so as to save some gas when creating contracts. The above child contract uses 22 bytes of code, and thus costs roughly 32000 + 22 * 200 = 36400 gas to create (the additional overhead of calling mint is only about 150 gas). If we could make use of address 0x0 for GasToken, we could save an extra 3000 gas or so for every created contract!

Don't worry about the 0x0 address. You want the 0xPC address. Then you can additionally save the PUSH15 and XOR operations.


This proposed update also affects the efficiency ratio and other parts of the web page.

@lorenzb
Copy link
Member

lorenzb commented Nov 26, 2018

@fulldecent
Thanks for letting us know! ❤️
If I understand you correctly, this technique would shave off 3 gas per minted contract?

@fulldecent
Copy link
Author

Yes, that is correct.

@k06a
Copy link

k06a commented May 10, 2019

@fulldecent can you clarify what XX means?

@fulldecent
Copy link
Author

XX = 0x04 ^ 0x13 = 0x17.

This is chosen so that

  1. 0xb3f879cb30fe243b4dfee438691c04 is the beneficiary (msg.caller)
  2. 0xb3f879cb30fe243b4dfee438691c04 ^ 0xb3f879cb30fe243b4dfee438691c17 = 0x13
  3. JUMP 0x13 lands on the JUMPDEST

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

No branches or pull requests

3 participants