Skip to content

perf: Box::pin removed for future returned by self.client_server_exchange(context) - #1396

Merged
sgrif merged 3 commits into
pgdogdev:mainfrom
ygxio:perf/frontend-hot-path
Aug 21, 2026
Merged

perf: Box::pin removed for future returned by self.client_server_exchange(context)#1396
sgrif merged 3 commits into
pgdogdev:mainfrom
ygxio:perf/frontend-hot-path

Conversation

@ygxio

@ygxio ygxio commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

closes #1395

@codecov

codecov Bot commented Aug 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@ygxio
ygxio force-pushed the perf/frontend-hot-path branch from 248b4bc to 4096472 Compare August 20, 2026 06:38
@ygxio ygxio changed the title perf: drop per-query map lock and future boxing on the client hot path perf: Box::pin for future returned by self.client_server_exchange(context) Aug 20, 2026
@ygxio ygxio changed the title perf: Box::pin for future returned by self.client_server_exchange(context) perf: Box::pin replaced by pin! for future returned by self.client_server_exchange(context) Aug 20, 2026
@ygxio

ygxio commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

Can i get a review here @levkk

@jkaczman jkaczman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I looked through the issue linked, and it doesn't look like the stats you showed correlate to these exact changes (had some other things included); could you please clarify on the exact improvements here?

@ygxio

ygxio commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

yes as per keryl i removed the changes of DashMap and only kept the pinning changes

@jkaczman

Copy link
Copy Markdown
Contributor

yes as per keryl i removed the changes of DashMap and only kept the pinning changes

I saw you had this chart for DashMap + Box::pin,
image

Could you make one for this one solo?

@ygxio

ygxio commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

sure it was same for only pin changes, would you mind testing tps on ur system too?

@sgrif

sgrif commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

I'm not sure this is the right move. The whole point of this change was to avoid having it on the stack because the future size becomes too large otherwise. Removing this allocation is just going to end up moving the cost up the stack, not get rid of it

@ygxio

ygxio commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

@sgrif yes that is right but what i saw is extra rjmallocs, i donot have screenshot right now but i will get one, will post in this thread.

Comment on lines +63 to +70
// Stack-pinned, not `Box::pin`: this runs once per query, and boxing
// costs a malloc, a free and a memcpy of the whole exchange future
// every time. `Pin<&mut F>` is a `Future`, so `safe_timeout` takes it.
//
// The inner block matters: unlike the boxed future, which was moved
// into `safe_timeout` and dropped by the `.await`, `pin!` binds a local
// that would otherwise hold its borrow of `self` and `context` until
// the end of the function, past the arms below.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd prefer to see comments like this as part of the commit message rather than on the code itself. These often get out of sync with the code they're talking about and result in the code base being littered with inaccurate statements about why it's written a certain way. I'd only mention the borrow of self and context if the code fails to compile without the block

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure i will take care of it

@sgrif

sgrif commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

yes that is right but what i saw is extra rjmallocs

Right, but that's not inherently a problem. Replacing an allocation with a 10k stack variable is a tradeoff not an automatic win. The fact that you're seeing memcpy upon boxing implies something else is interfering with the optimizer. The compiler should be able to place the value in the box directly without having to copy it from the stack.

@ygxio

ygxio commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

yes that is right but what i saw is extra rjmallocs

Right, but that's not inherently a problem. Replacing an allocation with a 10k stack variable is a tradeoff not an automatic win. The fact that you're seeing memcpy upon boxing implies something else is interfering with the optimizer. The compiler should be able to place the value in the box directly without having to copy it from the stack.

will dig down more, need to know more on this issue

@sgrif

sgrif commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

To be clear, I don't think this change is 100% the wrong choice. I just don't think there's been sufficient investigation into what's going on. While this is a relatively hot path, one extra allocation per request should not be causing huge impacts on our performance.

@sgrif

sgrif commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

will dig down more, need to know more on this issue

Thank you!

@ygxio

ygxio commented Aug 21, 2026

Copy link
Copy Markdown
Contributor Author

starting from the root cause

image

execute occupies almost 5.2% of the samples

out of which have some mallocs and rjems

image image image image image image

@sgrif

sgrif commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Alright, I'm sufficiently convinced. If you can update this to pass CI I'll merge it. None of the pin stuff is necessary, though. Removing Box::pin is the only change required

@ygxio ygxio changed the title perf: Box::pin replaced by pin! for future returned by self.client_server_exchange(context) perf: Box::pin removed for future returned by self.client_server_exchange(context) Aug 21, 2026
@ygxio
ygxio force-pushed the perf/frontend-hot-path branch from 7e30898 to 0534116 Compare August 21, 2026 18:37
@sgrif

sgrif commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

This is perfect. Thanks for working on this!

@sgrif
sgrif merged commit a15d291 into pgdogdev:main Aug 21, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: Box::pin removed for future returned by self.client_server_exchange(context)

3 participants