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

Speedup sum aggregator #120241

Merged
merged 1 commit into from
Jan 17, 2025

Conversation

original-brownbear
Copy link
Member

@original-brownbear original-brownbear commented Jan 15, 2025

Random profiling find :)

Using an object to track the two double fields is extremely inefficient here since the object can't be removed by escape analysis.
We have a couple more like this that could be fixed in a similar way. There is no state to retain in that summation object but since it's injected from the outside the compiler has no way to optimize away the two double stores at the end of the method call (and might not even be able to do away with the intermediary steps).
It's mildly unfortunate admittedly that there isn't really a neat Java way of drying this calculation up as far as I can tell because we need two double returns.

Using an object to track the two double fields is extremely inefficient
here since the object can't be removed by escape analysis.
@elasticsearchmachine elasticsearchmachine added Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v9.0.0 labels Jan 15, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

@nik9000
Copy link
Member

nik9000 commented Jan 17, 2025

It's mildly unfortunate admittedly that there isn't really a neat Java way of drying this calculation up as far as I can tell because we need two double returns.

In ESQL we tend to generate code for this sort of thing. Though we might still be using these Kahan objects out of habit. Server doesn't have any such precedent so I guess we're out of luck there.

@original-brownbear
Copy link
Member Author

@nik9000 you would probably get the same code out of the JIT if you instantiated that object inline and used its code. The problem here is that the object comes from an outside scope so escape analysis can't do away with it.
I just didn't like the idea of relying on escape analysis to get things right for us here. It also doesn't really simplify the code to hide the math.
I could clean this up in the other use cases (avg. etc.) and then see if I can find a drier solution.
The performance difference is quite massive here actually. I don't have hard numbers on it but I could see this method dominate some aggs benchmarks and it seems this change cuts the runtime CPU use for the leaf collecting ~ in half :O

Copy link
Member

@nik9000 nik9000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Give it a go if you've got time.

@original-brownbear original-brownbear added auto-backport Automatically create backport pull requests when merged v8.18.0 labels Jan 17, 2025
@original-brownbear
Copy link
Member Author

Thanks Nik!

@original-brownbear original-brownbear merged commit b8cb080 into elastic:main Jan 17, 2025
16 checks passed
@original-brownbear original-brownbear deleted the speedup-sum-agg branch January 17, 2025 13:32
}

var compensations = SumAggregator.this.compensations;
double delta = compensations.get(bucket);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we put this inside the if statement?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ouch you're right we have another optimization there, was so proud I did away with the redundant store and missed this sec, lets fix it right away.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#120383 :)

@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
8.x

original-brownbear added a commit to original-brownbear/elasticsearch that referenced this pull request Jan 17, 2025
Using an object to track the two double fields is extremely inefficient
here since the object can't be removed by escape analysis.
original-brownbear added a commit to original-brownbear/elasticsearch that referenced this pull request Jan 17, 2025
Trivial follow up to elastic#120241, no need for a redundant load though the not-load path is probably very cold anyway,
but still better to give the compiler all the inputs it can get.
elasticsearchmachine pushed a commit that referenced this pull request Jan 17, 2025
Using an object to track the two double fields is extremely inefficient
here since the object can't be removed by escape analysis.
original-brownbear added a commit that referenced this pull request Jan 17, 2025
Trivial follow up to #120241, no need for a redundant load though the not-load path is probably very cold anyway,
but still better to give the compiler all the inputs it can get.
original-brownbear added a commit to original-brownbear/elasticsearch that referenced this pull request Jan 17, 2025
Trivial follow up to elastic#120241, no need for a redundant load though the not-load path is probably very cold anyway,
but still better to give the compiler all the inputs it can get.
elasticsearchmachine pushed a commit that referenced this pull request Jan 17, 2025
Trivial follow up to #120241, no need for a redundant load though the not-load path is probably very cold anyway,
but still better to give the compiler all the inputs it can get.
@original-brownbear
Copy link
Member Author

Here we go for a bit of drier logic #120436 and speedup the average calculation as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Analytics/Aggregations Aggregations auto-backport Automatically create backport pull requests when merged >non-issue Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v8.18.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants