Skip to content

feat: deep selector hack #3357

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

feat: deep selector hack #3357

wants to merge 9 commits into from

Conversation

pyAndr3w
Copy link
Contributor

@pyAndr3w pyAndr3w commented Jun 4, 2025

Closes #3336

@pyAndr3w
Copy link
Contributor Author

pyAndr3w commented Jun 4, 2025

Current Selector Structure

The current selector consists of:

  • maybe set c3
  • if-jmp to internal
  • if-jmp to external
  • maybe jmp to c3 (or throw 130)

This logic is implemented in the selector-hack get-method.


Proposed Optimization

I propose moving the selector into recv_internal (or recv_external, if exists) and making recv_internal (recv_external) the root cell of the contract.

This maneuver will make the bytecode layout of contracts slightly more compact and help avoid unnecessary "jumps" between code cells.


Proof of Concept Results

At this stage, proof of concept work for this optimization has been completed. The results are available and they are good.

Current Issues

There is a problem with the absence of the maybe set c3 condition. There is also a problem that tonapi will not be able to properly index contracts with recv_external due to the different location of the function dictionary.


Solution

The solution to the problem with the missing maybe set c3 condition is known.

We can obtain a dictionary with all functions at the moment when compilation of recv_internal (recv_external) begins. It will be necessary to manually exclude from the dictionary functions that use the inline flag or are not used in the contract.

For this purpose, Asm.fif has a separate hashmap with function usage flags. However, the recv_internal function is not yet compiled and we don't know which functions are used in it (function flags may change).

To solve this, embedding of fake_* functions has been implemented so that all flags are set at the Asm.fif level and we can safely obtain the necessary dictionary that includes only used functions.

@pyAndr3w
Copy link
Contributor Author

pyAndr3w commented Jun 4, 2025

👉👈 It is also necessary to fix the draft so that it passes absolutely all tests.

@Kaladin13
Copy link
Contributor

Can you please add benchmark table result output here in the conversation? I mean to compare the impact of a new hack

@pyAndr3w
Copy link
Contributor Author

pyAndr3w commented Jun 4, 2025

@Kaladin13

image image image image image image image

@pyAndr3w pyAndr3w marked this pull request as ready for review June 6, 2025 13:02
@pyAndr3w pyAndr3w requested a review from a team as a code owner June 6, 2025 13:02
@pyAndr3w
Copy link
Contributor Author

pyAndr3w commented Jun 6, 2025

image

One test is failing due to the lack of a restriction in the Tact compiler on the minimum method_id.
In the deep selector hack, getters with method_id < 65536 end up in the hashmap with non-inline procedures.

@pyAndr3w
Copy link
Contributor Author

pyAndr3w commented Jun 6, 2025

base jetton e2e tests passed

https://testnet.tonviewer.com/kQBEg_BB-yM5Hyik9O9xuVvVDGEqhKTDwbTVf3rYQrIk_HSN?section=method

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

Successfully merging this pull request may close these issues.

Optimize function selector by avoiding unnecessary inlining of internal/external receivers
2 participants