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

produce better OPcodes in strict IF or-conditions #12199

Closed
staabm opened this issue Sep 13, 2023 · 4 comments
Closed

produce better OPcodes in strict IF or-conditions #12199

staabm opened this issue Sep 13, 2023 · 4 comments

Comments

@staabm
Copy link
Contributor

staabm commented Sep 13, 2023

Description

could php-src be smarter about IF or-conditions which only contain strict-comparisions to make it produce the same opcodes as in a strict in-array call?

see https://3v4l.org/QQQMr/vld

indentified in phpstan/phpstan-src#2618 (comment)

@morrisonlevi
Copy link
Contributor

I'm not sure about the optimizer, but just wanted to point out that if we're going to do this, we may as well make it a lookup table instead of a linear search: https://3v4l.org/V5PeZ/vld

function f1(string $name): bool {
    if (in_array($name, ['__construct', '__destruct', '__unset', '__wakeup', '__clone'], true)) {
        return true;
    }

    return false;
}

function f2(string $name): bool {
    if ($name === '__construct'
        || $name === '__destruct'
        || $name === '__unset'
        || $name === '__wakeup'
        || $name === '__clone'
    ) {
      return true;
    }

    return false;
}

function f3(string $name): bool {
    return isset([
        '__construct' => true,
        '__destruct' => true,
        '__unset' => true,
        '__wakeup' => true,
        '__clone' => true,
    ][$name]);
}

Of these, f3 should perform the best. Disclaimer: I never ran this code ^_^

@iluuu1994
Copy link
Member

iluuu1994 commented Sep 18, 2023

match already has support for jump tables with strict equality, so I'm not sure we need another variant. https://3v4l.org/vbrLB/vld ifs are intended for complex expressions and/or varying subjects where I a jump table isn't possible. Switch also uses jump tables, but needs a fallback chain with loose comparison.

@zonuexe
Copy link

zonuexe commented Oct 26, 2023

I'm not sure about the optimizer, but just wanted to point out that if we're going to do this, we may as well make it a lookup table instead of a linear search:

When searching a constant array, already in PHP 7.2 the in_array() function is compiled into an instruction that looks up from a hash table rather than a linear search.

@staabm
Copy link
Contributor Author

staabm commented Mar 7, 2024

Sounds like we already have plenty alternatives.

Thanks for the input. Closing.

@staabm staabm closed this as not planned Won't fix, can't repro, duplicate, stale Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants