Skip to content

Remove redundant exception check from VM #18730

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

Merged
merged 2 commits into from
Jun 2, 2025
Merged

Conversation

nielsdos
Copy link
Member

@nielsdos nielsdos commented Jun 1, 2025

Whether an exception happened is already checked a few lines up.

Whether an exception happened is already checked a few lines up.
@bwoebi
Copy link
Member

bwoebi commented Jun 1, 2025

The proper way to do it would be moving the ZEND_VM_NEXT_OPCODE_EX (which does not check EG(exception) itself in non-debug, just EX(opline) vs %r15) before the exception check. (As that avoids the branch completely on that path.)

@nielsdos
Copy link
Member Author

nielsdos commented Jun 1, 2025

I don't see how that's better

@bwoebi
Copy link
Member

bwoebi commented Jun 1, 2025

Seems less expensive to me to just load EX(opline) than checking if(EG(exception))? Or am I mistaken?

@nielsdos
Copy link
Member Author

nielsdos commented Jun 1, 2025

Seems less expensive to me to just load EX(opline) than checking if(EG(exception))? Or am I mistaken?

I agree with this statement.
However, if I move the if (EXPECTED(opline->extended_value == 0 && (opline+1)->opcode == ZEND_DO_FCALL)) { check upwards then I still need the EG(exception) check anyway for the stack push...

@bwoebi
Copy link
Member

bwoebi commented Jun 1, 2025

Yes sure, but only for the case where there are actually args for the object construction.

@nielsdos
Copy link
Member Author

nielsdos commented Jun 1, 2025

Yes sure, but only for the case where there are actually args for the object construction.

It clicked. Thanks for pointing it out

@dstogov
Copy link
Member

dstogov commented Jun 2, 2025

I don't see how the new code is better.
For the case with arguments and no exception it performs exactly the same checks just in different order.
Case with exception becomes even more expensive...
May be I miss something?

@nielsdos
Copy link
Member Author

nielsdos commented Jun 2, 2025

No, I had originally changed ZEND_VM_NEXT_OPCODE_EX so that check_exception is 0. And then undid that and swapped the checks. I suppose the first idea was better as it kept the exception code path the same cost and changed an EX(opline) access in just an online access.

@dstogov
Copy link
Member

dstogov commented Jun 2, 2025

I suppose the first idea was better

I think so.

@bwoebi
Copy link
Member

bwoebi commented Jun 2, 2025

@dstogov I'm confused. ZEND_VM_NEXT_OPCODE_EX returns. The case with exception and arguments is slightly more expensive, yes. The case without arguments should be less expensive. And the case with arguments and no exceptions should be unchanged.

Correct?

@dstogov
Copy link
Member

dstogov commented Jun 2, 2025

@dstogov I'm confused. ZEND_VM_NEXT_OPCODE_EX returns. The case with exception and arguments is slightly more expensive, yes. The case without arguments should be less expensive. And the case with arguments and no exceptions should be unchanged.

Correct?

No. The case without arguments becomes more expensive.

A single ZEND_VM_NEXT_OPCODE_EX(0...) would be better than single EG(exception) check, but in this case ZEND_VM_NEXT_OPCODE_EX() is under two additional checks and instead of a single check we perform two.

@bwoebi
Copy link
Member

bwoebi commented Jun 2, 2025

The case without arguments and an exception. The case without arguments without exception is cheaper, right?

And IIRC, we optimize for the happy path, not the exception?

@dstogov
Copy link
Member

dstogov commented Jun 2, 2025

The case without arguments and an exception. The case without arguments without exception is cheaper, right?

right.

And IIRC, we optimize for the happy path, not the exception?

right. I was wrong.

@nielsdos nielsdos merged commit 36891a6 into php:master Jun 2, 2025
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants