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

Remove excessive null? checks #1282

Open
anton-trunov opened this issue Jan 3, 2025 · 2 comments
Open

Remove excessive null? checks #1282

anton-trunov opened this issue Jan 3, 2025 · 2 comments
Labels
gas Gas consumption and fee-related things optimizations
Milestone

Comments

@anton-trunov
Copy link
Member

For instance, this snippet

fun foo(s: SomeStruct?): Bool {
    if (s == null) { return false }
    else { return s!!.b }
}

translates into the following FunC code:

int $global_foo(tuple $s) impure inline_ref {
    var ($s) = $s;
    if (null?($s)) {        ;; 1st null? check
        return false;
    } else {
        return $SomeStruct$_get_b($SomeStruct$_not_null($s));
    }
}

((int, int)) $SomeStruct$_not_null(tuple v) inline {
    throw_if(128, null?(v));     ;; 2nd null? check
    var (int vvv'i, int vvv'b) = __tact_tuple_destroy_2(v);
    return (vvv'i, vvv'b);
}

which just wastes gas.

@anton-trunov anton-trunov added gas Gas consumption and fee-related things optimizations labels Jan 3, 2025
@anton-trunov anton-trunov added this to the v1.6.0 milestone Jan 3, 2025
@Gusarich
Copy link
Member

Gusarich commented Jan 9, 2025

Do you mean we should generally remove this throw_if(128, null?(v)) for all !! expressions or only in cases when the value was checked for null already?

@anton-trunov
Copy link
Member Author

I would argue we should remove all those checks now and provide a static analysis to warn about unguarded uses of the !! operator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gas Gas consumption and fee-related things optimizations
Projects
None yet
Development

No branches or pull requests

2 participants