Skip to content

Conversation

@erezrokah
Copy link
Contributor

@erezrokah erezrokah commented Oct 15, 2025

Summary

Fixes #1685. A better way would be to be able to disable these events altogether from being returned to the client.

Also, if this PR is accepted we might want to do the same for other listeners, though I haven't seen a big issue with allocations in other places (see issue for memory profiles).

For the benchmark in the issue this is over a 3x improvement

Checklist

  • Unit and integration tests covering the common scenarios were added
  • A human-readable description of the changes was provided to include in CHANGELOG

@erezrokah
Copy link
Contributor Author

Taking a look at the test failures 👀

@erezrokah
Copy link
Contributor Author

I have some tests failing for me on main so hopefully #1687 fixes the issues?

@erezrokah erezrokah force-pushed the fix/profile_events_memory branch from b264936 to ce6ffb5 Compare October 15, 2025 14:35
@erezrokah
Copy link
Contributor Author

Anything missing from the PR or from the issue? Is there another approach I should take? 3x improvement in memory allocations seems quite good. I can add more benchmarks/screenshots if needed

@mshustov mshustov requested review from Copilot and kavirajk October 25, 2025 10:04
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR optimizes memory allocation by skipping the scanning and processing of profile events when no profile event listener is registered. The change addresses a performance issue where profile events were being unnecessarily parsed even when the client wasn't interested in them, resulting in significant memory allocations.

Key changes:

  • Modified profile event handling to conditionally scan events only when a listener is set
  • Refactored onProcess initialization to conditionally set the profileEvents handler

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
tests/issues/1685_test.go Adds benchmark test to verify the performance improvement for the profile events optimization
context.go Refactors onProcess() to conditionally assign profileEvents handler only when listener is present
conn_profile_events.go Adds scanEvents parameter to skip event parsing when no listener is registered
conn_process.go Updates profile events handling to pass scan flag and conditionally invoke handler

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +192 to +194
if scanEvents {
on.profileEvents(events)
}
Copy link

Copilot AI Oct 25, 2025

Choose a reason for hiding this comment

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

The scanEvents check on line 192 is redundant. When scanEvents is false, on.profileEvents is nil (as set in context.go), so calling it would panic. However, since profileEvents() already returns nil when scanEvents is false (line 28 of conn_profile_events.go), this condition will never execute with a nil handler. Remove this check and call on.profileEvents(events) unconditionally, relying on the nil check in context.go.

Suggested change
if scanEvents {
on.profileEvents(events)
}
on.profileEvents(events)

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

@erezrokah erezrokah Oct 27, 2025

Choose a reason for hiding this comment

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

this condition will never execute with a nil handler

This is incorrect, if scanEvents is false on.profileEvents is nil and the call will panic (double checked by removing the condition and re-running the test)

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.

bug(performance-memory): Unnecessary memory allocations from reading profile events

1 participant