Skip to content

perf: avoid unnecessary getThis in writeToOutputBuffer #3548

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

Merged
merged 1 commit into from
May 23, 2025

Conversation

mehah
Copy link
Contributor

@mehah mehah commented May 22, 2025

This PR optimizes the ProtocolGame::writeToOutputBuffer method by avoiding the unnecessary creation of shared_from_this() (getThis()) when already within the dispatcher context.

Previously, the method always called getThis() to capture self, even when already executing inside the dispatcher, which introduced significant overhead due to shared_ptr management. Now, the check g_dispatcher().context().isAsync() ensures that if we're already in the dispatcher context, the operation is performed immediately and directly, avoiding the cost of shared_from_this().

This change eliminates the need to create extra shared_ptr instances in common scenarios, reducing pressure on the reference counting system and improving overall performance, especially under high load.

Copy link

@dudantas dudantas changed the title perf: Evitar shared_from_this desnecessário em writeToOutputBuffer, reduzindo overhead e melhorando a performance perf: avoid unnecessary shared_from_this in writeToOutputBuffer May 22, 2025
@dudantas dudantas changed the title perf: avoid unnecessary shared_from_this in writeToOutputBuffer perf: avoid unnecessary getThis in writeToOutputBuffer May 23, 2025
@dudantas dudantas merged commit ff4d408 into main May 23, 2025
33 checks passed
@dudantas dudantas deleted the perf_writeToOutputBuffer branch May 23, 2025 02:54
feliphechaves pushed a commit to feliphechaves/opentibiabr that referenced this pull request May 27, 2025
)

This PR optimizes the ProtocolGame::writeToOutputBuffer method by
avoiding the unnecessary creation of shared_from_this() (getThis()) when
already within the dispatcher context.

Previously, the method always called getThis() to capture self, even
when already executing inside the dispatcher, which introduced
significant overhead due to shared_ptr management. Now, the check
g_dispatcher().context().isAsync() ensures that if we're already in the
dispatcher context, the operation is performed immediately and directly,
avoiding the cost of shared_from_this().

This change eliminates the need to create extra shared_ptr instances in
common scenarios, reducing pressure on the reference counting system and
improving overall performance, especially under high load.
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.

2 participants