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

Websockets remove unnecessary memory allocations when trace logging level disabled. #4257

Conversation

berezovik-rs
Copy link
Contributor

@berezovik-rs berezovik-rs commented Aug 29, 2024

Websockets remove unnecessary memory allocations when trace logging level disabled.

Subsystem
Server websockets

Motivation
Unnecessary memory allocations if log level > TRACE

Solution
Add inline function with isTraceEnabled flag check

2024-08-29 13 07 24

@e5l e5l requested a review from bjhham September 5, 2024 07:18
Comment on lines 128 to 131
if (LOGGER.isTraceEnabled) {
LOGGER.trace(
"Starting default WebSocketSession($this) " +
"with negotiated extensions: ${negotiatedExtensions.joinToString()}"
)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of duplicating the check here, could you please introduce an inline function in ktor-utils/common/src/io/ktor/util/logging/Logger.kt?

/**
 * Check `isTraceEnabled` flag before logging to save some memory allocations.
 */
public inline fun Logger.trace(message: () -> String) {
    if (isTraceEnabled) trace(message())
}

Then we can start replacing our other trace calls with this to save on memory allocations.

@berezovik-rs berezovik-rs force-pushed the feat/websockets-remove-unnecessary-trace-logs-memory-allocations branch from 3fefee0 to 6ee0c82 Compare September 7, 2024 20:41
@berezovik-rs berezovik-rs changed the title Websockets remove unnecessary memory allocations, trace logs only if isTraceEnabled=true Websockets remove unnecessary memory allocations when trace logging level disabled. Sep 7, 2024
bjhham
bjhham approved these changes Sep 9, 2024
@bjhham
Copy link
Contributor

bjhham commented Sep 9, 2024

Could you please run:

./gradlew apiDump

And commit the resulting API file changes for ktor-utils prior to merging?

@berezovik-rs berezovik-rs force-pushed the feat/websockets-remove-unnecessary-trace-logs-memory-allocations branch from 6ee0c82 to f178842 Compare September 9, 2024 07:52
@berezovik-rs
Copy link
Contributor Author

Could you please run:

./gradlew apiDump

And commit the resulting API file changes for ktor-utils prior to merging?

Done

@bjhham bjhham merged commit 1efdf12 into ktorio:main Sep 11, 2024
12 of 15 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.

2 participants