Special-case executor expression steps for common combinations

Started by Daniel Gustafssonover 2 years ago16 messageshackers
Jump to latest
#1Daniel Gustafsson
daniel@yesql.se

The attached patch adds special-case expression steps for common sets of steps
in the executor to shave a few cycles off during execution, and make the JIT
generated code simpler.

* Adds EEOP_FUNCEXPR_STRICT_1 and EEOP_FUNCEXPR_STRICT_2 for function calls of
strict functions with 1 or 2 arguments (EEOP_FUNCEXPR_STRICT remains used for

2 arguments).

* Adds EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1 which is a special case for the
common case of one arg aggs.
* Replace EEOP_DONE with EEOP_DONE_RETURN and EEOP_DONE_NO_RETURN to be able to
skip extra setup for steps which are only interested in the side effects.

Stressing the EEOP_FUNCEXPR_STRICT_* steps specifically shows a 1.5%
improvement and pgbench over the branch shows a ~1% improvement in TPS (both
measured over 6 runs with outliers removed).

EEOP_FUNCEXPR_STRICT_* (10M iterations):
master : (7503.317, 7553.691, 7634.524)
patched : (7422.756, 7455.120, 7492.393)

pgbench:
master : (3653.83, 3792.97, 3863.70)
patched : (3743.04, 3830.02, 3869.80)

This patch was extracted from a larger body of work from Andres [0]/messages/by-id/20191023163849.sosqbfs5yenocez3@alap3.anarazel.de aiming at
providing the necessary executor infrastructure for making JIT expression
caching possible. This patch, and more which are to be submitted, is however
separate in the sense that it is not part of the infrastructure, it's an
improvements on its own.

Thoughts?

--
Daniel Gustafsson

[0]: /messages/by-id/20191023163849.sosqbfs5yenocez3@alap3.anarazel.de

Attachments:

v1-0001-Add-fast-path-expression-steps-for-common-combina.patchapplication/octet-stream; name=v1-0001-Add-fast-path-expression-steps-for-common-combina.patch; x-unix-mode=0644Download+192-38
#2Heikki Linnakangas
heikki.linnakangas@enterprisedb.com
In reply to: Daniel Gustafsson (#1)
Re: Special-case executor expression steps for common combinations

On 12/10/2023 12:48, Daniel Gustafsson wrote:

The attached patch adds special-case expression steps for common sets of steps
in the executor to shave a few cycles off during execution, and make the JIT
generated code simpler.

* Adds EEOP_FUNCEXPR_STRICT_1 and EEOP_FUNCEXPR_STRICT_2 for function calls of
strict functions with 1 or 2 arguments (EEOP_FUNCEXPR_STRICT remains used for

2 arguments).

* Adds EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1 which is a special case for the
common case of one arg aggs.

Are these relevant when JITting? I'm a little sad if the JIT compiler
cannot unroll these on its own. Is there something we could do to hint
it, so that it could treat the number of arguments as a constant?

I understand that this can give a small boost in interpreter mode, so
maybe we should do it in any case. But I'd like to know if we're missing
a trick with the JITter, before we mask it with this.

* Replace EEOP_DONE with EEOP_DONE_RETURN and EEOP_DONE_NO_RETURN to be able to
skip extra setup for steps which are only interested in the side effects.

I'm a little surprised if this makes a measurable performance
difference, but sure, why not. It seems nice to be more explicit when
you don't expect a return value.

--
Heikki Linnakangas
Neon (https://neon.tech)

#3David Rowley
dgrowleyml@gmail.com
In reply to: Daniel Gustafsson (#1)
Re: Special-case executor expression steps for common combinations

On Thu, 12 Oct 2023 at 22:54, Daniel Gustafsson <daniel@yesql.se> wrote:

EEOP_FUNCEXPR_STRICT_* (10M iterations):
master : (7503.317, 7553.691, 7634.524)
patched : (7422.756, 7455.120, 7492.393)

pgbench:
master : (3653.83, 3792.97, 3863.70)
patched : (3743.04, 3830.02, 3869.80)

Thoughts?

Did any of these tests compile the expression with JIT?

If not, how does the performance compare for a query that JITs the expression?

David

#4Andres Freund
andres@anarazel.de
In reply to: Heikki Linnakangas (#2)
Re: Special-case executor expression steps for common combinations

Hi,

On 2023-10-12 13:24:27 +0300, Heikki Linnakangas wrote:

On 12/10/2023 12:48, Daniel Gustafsson wrote:

The attached patch adds special-case expression steps for common sets of steps
in the executor to shave a few cycles off during execution, and make the JIT
generated code simpler.

* Adds EEOP_FUNCEXPR_STRICT_1 and EEOP_FUNCEXPR_STRICT_2 for function calls of
strict functions with 1 or 2 arguments (EEOP_FUNCEXPR_STRICT remains used for

2 arguments).

* Adds EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1 which is a special case for the
common case of one arg aggs.

Are these relevant when JITting? I'm a little sad if the JIT compiler cannot
unroll these on its own. Is there something we could do to hint it, so that
it could treat the number of arguments as a constant?

I think it's mainly important for interpreted execution.

skip extra setup for steps which are only interested in the side effects.

I'm a little surprised if this makes a measurable performance difference,
but sure, why not. It seems nice to be more explicit when you don't expect a
return value.

IIRC this is more interesting for JIT than the above, because it allows LLVM
to know that the return value isn't needed and thus doesn't need to be
computed.

Greetings,

Andres Freund

#5Daniel Gustafsson
daniel@yesql.se
In reply to: Andres Freund (#4)
Re: Special-case executor expression steps for common combinations

On 12 Oct 2023, at 19:52, Andres Freund <andres@anarazel.de> wrote:
On 2023-10-12 13:24:27 +0300, Heikki Linnakangas wrote:

On 12/10/2023 12:48, Daniel Gustafsson wrote:

The attached patch adds special-case expression steps for common sets of steps
in the executor to shave a few cycles off during execution, and make the JIT
generated code simpler.

* Adds EEOP_FUNCEXPR_STRICT_1 and EEOP_FUNCEXPR_STRICT_2 for function calls of
strict functions with 1 or 2 arguments (EEOP_FUNCEXPR_STRICT remains used for

2 arguments).

* Adds EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1 which is a special case for the
common case of one arg aggs.

Are these relevant when JITting? I'm a little sad if the JIT compiler cannot
unroll these on its own. Is there something we could do to hint it, so that
it could treat the number of arguments as a constant?

I think it's mainly important for interpreted execution.

Agreed.

skip extra setup for steps which are only interested in the side effects.

I'm a little surprised if this makes a measurable performance difference,
but sure, why not. It seems nice to be more explicit when you don't expect a
return value.

Right, performance benefits aside it does improve readability IMHO.

IIRC this is more interesting for JIT than the above, because it allows LLVM
to know that the return value isn't needed and thus doesn't need to be
computed.

Correct, this is important to the JIT code which no longer has to perform two
Loads and one Store in order to get nothing, but can instead fastpath to
building a zero returnvalue.

--
Daniel Gustafsson

#6Andreas Karlsson
andreas.karlsson@percona.com
In reply to: Daniel Gustafsson (#1)
Re: Special-case executor expression steps for common combinations

On 10/12/23 11:48 AM, Daniel Gustafsson wrote:

Thoughts?

I have looked at the patch and it still applies, builds and passes the
test cases and I personally think these optimizations are pretty much
no-brainers that we should do and it is a pity nobody has had the time
to review this patch.

1) The no-return case should help with the JIT, making jitted code faster.

2) The specialized strict steps helps with many common queries in the
interpreted mode.

The code itself looks really good (great work!) but I have two comments
on it.

1) I think the patch should be split into two. The two different
optimizations are not related at all other than that they create
specialized versions of expressions steps. Having them as separate makes
the commit history easier to read for future developers.

2) We could generate functions which return void rather than NULL and
therefore not have to do a return at all but I am not sure that small
optimization and extra clarity would be worth the hassle. The current
approach with adding Assert() is ok with me. Daniel, what do you think?

Andreas

#7Andreas Karlsson
andreas.karlsson@percona.com
In reply to: Andreas Karlsson (#6)
Re: Special-case executor expression steps for common combinations

On 6/20/24 5:22 PM, Andreas Karlsson wrote:

On 10/12/23 11:48 AM, Daniel Gustafsson wrote:

Thoughts?

I have looked at the patch and it still applies, builds and passes the
test cases and I personally think these optimizations are pretty much
no-brainers that we should do and it is a pity nobody has had the time
to review this patch.

Forgot to write that I am planning to also try to do so benchmarks to
see if I can reproduce the speedups. :)

Andreas

#8Daniel Gustafsson
daniel@yesql.se
In reply to: Andreas Karlsson (#6)
Re: Special-case executor expression steps for common combinations

On 20 Jun 2024, at 17:22, Andreas Karlsson <andreas@proxel.se> wrote:

On 10/12/23 11:48 AM, Daniel Gustafsson wrote:

Thoughts?

I have looked at the patch and it still applies, builds and passes the test cases and I personally think these optimizations are pretty much no-brainers that we should do and it is a pity nobody has had the time to review this patch.

1) The no-return case should help with the JIT, making jitted code faster.

2) The specialized strict steps helps with many common queries in the interpreted mode.

The code itself looks really good (great work!) but I have two comments on it.

Thanks for review!

1) I think the patch should be split into two. The two different optimizations are not related at all other than that they create specialized versions of expressions steps. Having them as separate makes the commit history easier to read for future developers.

That's a good point, the attached v2 splits it into two separate commits.

2) We could generate functions which return void rather than NULL and therefore not have to do a return at all but I am not sure that small optimization and extra clarity would be worth the hassle. The current approach with adding Assert() is ok with me. Daniel, what do you think?

I'm not sure that would move the needle enough to warrant the extra complexity.
It could be worth pursuing, but it can be done separately from this.

--
Daniel Gustafsson

Attachments:

v2-0002-Add-special-case-fast-paths-for-strict-functions.patchapplication/octet-stream; name=v2-0002-Add-special-case-fast-paths-for-strict-functions.patch; x-unix-mode=0644Download+96-6
v2-0001-Replace-EEOP_DONE-with-special-steps-for-return-n.patchapplication/octet-stream; name=v2-0001-Replace-EEOP_DONE-with-special-steps-for-return-n.patch; x-unix-mode=0644Download+96-33
#9Andreas Karlsson
andreas.karlsson@percona.com
In reply to: Daniel Gustafsson (#8)
Re: Special-case executor expression steps for common combinations

On 7/4/24 6:26 PM, Daniel Gustafsson wrote:

2) We could generate functions which return void rather than NULL and therefore not have to do a return at all but I am not sure that small optimization and extra clarity would be worth the hassle. The current approach with adding Assert() is ok with me. Daniel, what do you think?

I'm not sure that would move the needle enough to warrant the extra complexity.
It could be worth pursuing, but it can be done separately from this.

Agreed.

I looked some more at the patch and have a suggestion for code style.
Attaching the diff. Do with them what you wish, for me they make to code
easier to read.

Andreas

Attachments:

0001-Suggested-style-changes.patchtext/x-patch; charset=UTF-8; name=0001-Suggested-style-changes.patchDownload+3-7
#10Andreas Karlsson
andreas.karlsson@percona.com
In reply to: Andreas Karlsson (#9)
Re: Special-case executor expression steps for common combinations

I have bench marked the two patches now and failed to measure any
speedup or slowdown from the first patch (removing return) but I think
it is a good idea anyway.

For the second patch (optimize strict) I managed to measure a ~1% speed
up for the following query "SELECT sum(x + y + 1) FROM t;" over one
million rows.

I would say both patches are ready for committer modulo my proposed
style fixes.

Andreas

#11Daniel Gustafsson
daniel@yesql.se
In reply to: Andreas Karlsson (#10)
Re: Special-case executor expression steps for common combinations

On 22 Jul 2024, at 23:25, Andreas Karlsson <andreas@proxel.se> wrote:

I have bench marked the two patches now and failed to measure any speedup or slowdown from the first patch (removing return) but I think it is a good idea anyway.

For the second patch (optimize strict) I managed to measure a ~1% speed up for the following query "SELECT sum(x + y + 1) FROM t;" over one million rows.

That's expected, this is mostly about refactoring the code to simplifying the
JITed code (and making tiny strides towards JIT expression caching).

I would say both patches are ready for committer modulo my proposed style fixes.

I am a bit wary about removing the out_error label and goto since it may open
up for reports from static analyzers about control reaching the end of a
non-void function without a return. The other change has been incorporated.

The attached v3 is a rebase to handle executor changes done since v2, with the
above mentioned fix as well. If there are no objections I think we should
apply this version.

--
Daniel Gustafsson

Attachments:

v3-0001-Replace-EEOP_DONE-with-special-steps-for-return-n.patchapplication/octet-stream; name=v3-0001-Replace-EEOP_DONE-with-special-steps-for-return-n.patch; x-unix-mode=0644Download+97-34
v3-0002-Add-special-case-fast-paths-for-strict-functions.patchapplication/octet-stream; name=v3-0002-Add-special-case-fast-paths-for-strict-functions.patch; x-unix-mode=0644Download+96-6
#12Andreas Karlsson
andreas.karlsson@percona.com
In reply to: Daniel Gustafsson (#11)
Re: Special-case executor expression steps for common combinations

On 9/10/24 10:54 AM, Daniel Gustafsson wrote:

On 22 Jul 2024, at 23:25, Andreas Karlsson <andreas@proxel.se> wrote:

I have bench marked the two patches now and failed to measure any speedup or slowdown from the first patch (removing return) but I think it is a good idea anyway.

For the second patch (optimize strict) I managed to measure a ~1% speed up for the following query "SELECT sum(x + y + 1) FROM t;" over one million rows.

That's expected, this is mostly about refactoring the code to simplifying the
JITed code (and making tiny strides towards JIT expression caching).

Yup! Expected and nice tiny speedup.

I would say both patches are ready for committer modulo my proposed style fixes.

I am a bit wary about removing the out_error label and goto since it may open
up for reports from static analyzers about control reaching the end of a
non-void function without a return. The other change has been incorporated.

The attached v3 is a rebase to handle executor changes done since v2, with the
above mentioned fix as well. If there are no objections I think we should
apply this version.

Sounds good to me and in my opinion this is ready to be committed.

Andreas

#13Daniel Gustafsson
daniel@yesql.se
In reply to: Andreas Karlsson (#12)
Re: Special-case executor expression steps for common combinations

On 13 Sep 2024, at 15:01, Andreas Karlsson <andreas@proxel.se> wrote:

On 9/10/24 10:54 AM, Daniel Gustafsson wrote:

On 22 Jul 2024, at 23:25, Andreas Karlsson <andreas@proxel.se> wrote:

I have bench marked the two patches now and failed to measure any speedup or slowdown from the first patch (removing return) but I think it is a good idea anyway.

For the second patch (optimize strict) I managed to measure a ~1% speed up for the following query "SELECT sum(x + y + 1) FROM t;" over one million rows.

That's expected, this is mostly about refactoring the code to simplifying the
JITed code (and making tiny strides towards JIT expression caching).

Yup! Expected and nice tiny speedup.

I would say both patches are ready for committer modulo my proposed style fixes.

I am a bit wary about removing the out_error label and goto since it may open
up for reports from static analyzers about control reaching the end of a
non-void function without a return. The other change has been incorporated.
The attached v3 is a rebase to handle executor changes done since v2, with the
above mentioned fix as well. If there are no objections I think we should
apply this version.

Sounds good to me and in my opinion this is ready to be committed.

This fell off the ever-growing TODO again. Re-reading it I still think it's a
good idea, it applied almost cleanly still and still gives a slight performance
improvement along with the more interesting refactoring which will make caching
of expressions easier down the line.

Attached is a rebased v4, unless there are objections I'll go ahead with this
version.

--
Daniel Gustafsson

Attachments:

v4-0002-Add-special-case-fast-paths-for-strict-functions.patchapplication/octet-stream; name=v4-0002-Add-special-case-fast-paths-for-strict-functions.patch; x-unix-mode=0644Download+97-7
v4-0001-Replace-EEOP_DONE-with-special-steps-for-return-n.patchapplication/octet-stream; name=v4-0001-Replace-EEOP_DONE-with-special-steps-for-return-n.patch; x-unix-mode=0644Download+97-34
#14Andreas Karlsson
andreas.karlsson@percona.com
In reply to: Daniel Gustafsson (#13)
Re: Special-case executor expression steps for common combinations

On 2/5/25 11:00 AM, Daniel Gustafsson wrote:

This fell off the ever-growing TODO again. Re-reading it I still think it's a
good idea, it applied almost cleanly still and still gives a slight performance
improvement along with the more interesting refactoring which will make caching
of expressions easier down the line.

Attached is a rebased v4, unless there are objections I'll go ahead with this
version.

+1 Still seems like a nice change to me too.

Andreas

#15Daniel Gustafsson
daniel@yesql.se
In reply to: Andreas Karlsson (#14)
Re: Special-case executor expression steps for common combinations

On 8 Mar 2025, at 17:15, Andreas Karlsson <andreas@proxel.se> wrote:

On 2/5/25 11:00 AM, Daniel Gustafsson wrote:

This fell off the ever-growing TODO again. Re-reading it I still think it's a
good idea, it applied almost cleanly still and still gives a slight performance
improvement along with the more interesting refactoring which will make caching
of expressions easier down the line.
Attached is a rebased v4, unless there are objections I'll go ahead with this
version.

+1 Still seems like a nice change to me too.

Thanks, I actually had it staged to go in early next week.

--
Daniel Gustafsson

#16Daniel Gustafsson
daniel@yesql.se
In reply to: Daniel Gustafsson (#15)
Re: Special-case executor expression steps for common combinations

On 9 Mar 2025, at 23:35, Daniel Gustafsson <daniel@yesql.se> wrote:

On 8 Mar 2025, at 17:15, Andreas Karlsson <andreas@proxel.se> wrote:

+1 Still seems like a nice change to me too.

Thanks, I actually had it staged to go in early next week.

..which was done yesterday, thanks for review!

--
Daniel Gustafsson