Re: Special-case executor expression steps for common combinations

2024-09-13 Thread Andreas Karlsson

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

On 22 Jul 2024, at 23:25, Andreas Karlsson  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





Re: Special-case executor expression steps for common combinations

2024-09-10 Thread Daniel Gustafsson
> On 22 Jul 2024, at 23:25, Andreas Karlsson  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



v3-0001-Replace-EEOP_DONE-with-special-steps-for-return-n.patch
Description: Binary data


v3-0002-Add-special-case-fast-paths-for-strict-functions.patch
Description: Binary data


Re: Special-case executor expression steps for common combinations

2024-07-22 Thread Andreas Karlsson
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





Re: Special-case executor expression steps for common combinations

2024-07-22 Thread Andreas Karlsson

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
From 9ea9a07ce6e4faf728ccc4a7b161a70a214601c8 Mon Sep 17 00:00:00 2001
From: Andreas Karlsson 
Date: Thu, 18 Jul 2024 21:54:29 +0200
Subject: [PATCH] Suggested style changes

---
 src/backend/executor/execExprInterp.c | 9 +++--
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/src/backend/executor/execExprInterp.c b/src/backend/executor/execExprInterp.c
index 90f37db62e..845d077b6f 100644
--- a/src/backend/executor/execExprInterp.c
+++ b/src/backend/executor/execExprInterp.c
@@ -546,7 +546,7 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
 		EEO_CASE(EEOP_DONE_NO_RETURN)
 		{
 			Assert(isnull == NULL);
-			return 0;
+			return (Datum) 0;
 		}
 
 		EEO_CASE(EEOP_INNER_FETCHSOME)
@@ -1966,13 +1966,10 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
 		{
 			/* unreachable */
 			Assert(false);
-			goto out_error;
+			pg_unreachable();
+			return (Datum) 0;
 		}
 	}
-
-out_error:
-	pg_unreachable();
-	return (Datum) 0;
 }
 
 /*
-- 
2.43.0



Re: Special-case executor expression steps for common combinations

2024-07-04 Thread Daniel Gustafsson
> On 20 Jun 2024, at 17:22, 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.
> 
> 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



v2-0002-Add-special-case-fast-paths-for-strict-functions.patch
Description: Binary data


v2-0001-Replace-EEOP_DONE-with-special-steps-for-return-n.patch
Description: Binary data


Re: Special-case executor expression steps for common combinations

2024-06-20 Thread Andreas Karlsson

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




Re: Special-case executor expression steps for common combinations

2024-06-20 Thread Andreas Karlsson

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




Re: Special-case executor expression steps for common combinations

2023-10-19 Thread Daniel Gustafsson
> On 12 Oct 2023, at 19:52, Andres Freund  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





Re: Special-case executor expression steps for common combinations

2023-10-12 Thread Andres Freund
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




Re: Special-case executor expression steps for common combinations

2023-10-12 Thread David Rowley
On Thu, 12 Oct 2023 at 22:54, Daniel Gustafsson  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




Re: Special-case executor expression steps for common combinations

2023-10-12 Thread Heikki Linnakangas

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)