Files
releases/v1
Michael Chittenden a57aa7ee61 fix TermValueEqual performance regression (#8863)
### Why the changes in this PR are needed?

When I was updating my app from v1.1.0 to v1.2.0 I noticed an almost 50%
performance regression in the hot path. The line here is the deploy, and
of course you can see latency increase right afterwards:

<img width="2724" height="1050" alt="Screenshot 2026-07-06 at 8 52
11 PM"
src="https://github.com/user-attachments/assets/66a8ecfc-ff67-4000-b926-228a0845c3b4"
/>

I did a git bisect and was able to identify that [this
commit](https://github.com/open-policy-agent/opa/commit/3d7fc9f3ac98f4f4d756f5e36eced7dec82e116c)
was the culprit. The commit immediately before it kept essentially the
same baseline as before, and this one saw the increase in latency. This
seemed unusual given the change essentially didn't seem to be that
drastic of a change.

So I did a CPU profile on my running app and found that with this
change, there was increase in CPU cycles around `v1/ast.Compare` and
`v1/ast.sortOrder` and `v1/ast.(*object).Compare`. Doing some digging, I
see that the old implementation had used `(*Term).Equal()` (among
others), which has a pointer equality check:
https://github.com/open-policy-agent/opa/blob/main/v1/ast/term.go#L383

And the new (at the time) `TermValueEqual` does not:
https://github.com/open-policy-agent/opa/commit/3d7fc9f3ac98f4f4d756f5e36eced7dec82e116c#diff-bd1f965e4bfe6f4374ccff2a119e129c0b11538253a2a99dc8694d79a7daf51cR405

So before, the two term pointers being exactly the same could short
circuit, and after that commit they had to traverse both objects to
compare, even if both pointers are equal. With this change you can see
that latency returned to baseline from before:

<img width="2732" height="1056" alt="Screenshot 2026-07-06 at 8 52 31 PM
(1)"
src="https://github.com/user-attachments/assets/c207454f-e9a5-423f-a942-a3fc293174f8"
/>

### What are the changes in this PR?

Basic pointer equality check to reduce CPU cycles.

---------

Signed-off-by: Mike Chittenden <mchitten@gmail.com>
2026-07-07 13:52:48 +00:00
..
2025-10-10 17:51:02 +02:00
2026-03-06 22:07:35 +00:00
2026-05-13 13:11:27 +02:00
2024-12-12 15:27:34 +01:00