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

Short-Circuit Condition Optimization #144

Closed
jubnzv opened this issue Sep 25, 2024 · 1 comment · Fixed by #202
Closed

Short-Circuit Condition Optimization #144

jubnzv opened this issue Sep 25, 2024 · 1 comment · Fixed by #202
Assignees
Milestone

Comments

@jubnzv
Copy link
Member

jubnzv commented Sep 25, 2024

Summary
Suggest adjustments to comparison binary expressions to leverage short-circuit functionality.

Context
TVM supports short-circuit operations. For example, in the code: if (long_running_fun() && self.boolean_always_false), it would be more optimal to write it as: if (self.boolean_always_false && long_running_fun()). This pattern is present in several real-world contracts, making this detector useful for optimizing their code.

Short-circuit in TVM
For this function:

get fun test(a: Int): Int {
    if ((a > 4) && (a > 5) && (a > 6)) {
        return 44;
    }
    return 43;
}

Tact generates FunC code, which results in the following fift code, without providing any optimizations:

$Test$_fun_test PROCREF:<{
    DROP
    DUP
    4 GTINT
    IF:<{
      DUP
      5 GTINT
    }>ELSE<{
      FALSE
    }>
    IF:<{
      6 GTINT
    }>ELSE<{
      DROP
      FALSE
    }>
    IFJMP:<{
      44 PUSHINT
    }>
    43 PUSHINT
}>

The underlying execution of the IF opcodes works this way: it pops the stack value and executes the continuation only if it is true.

Therefore, TVM supports short-circuit operations, and the idea behind this detector is valid.

@jubnzv
Copy link
Member Author

jubnzv commented Sep 27, 2024

Ideally, we need types in the AST to simplify the implementation: #136 / #70

@jubnzv jubnzv added the blocked label Sep 27, 2024
@jubnzv jubnzv added this to the v0.5 milestone Sep 27, 2024
@jubnzv jubnzv modified the milestones: v0.5, v0.6 Oct 19, 2024
jubnzv added a commit that referenced this issue Nov 1, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Closes #144

Co-authored-by: Georgiy Komarov <jubnzv@gmail.com>
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