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

Improve showShort to avoid parsing Uri #3590

Merged
merged 8 commits into from
Mar 12, 2024
Merged

Improve showShort to avoid parsing Uri #3590

merged 8 commits into from
Mar 12, 2024

Conversation

kciesielski
Copy link
Member

@kciesielski kciesielski commented Mar 11, 2024

Fixes #3550, #3547, #3272
The ServerRequest.showShort method is used in DefaultServerLog and possibly in user logs as well. It is quite expensive due to the overhead of sttp.model.Uri.unsafeParse. This cost can be reduced by overriding showShort, which prints the uri from the request as directly as possible.

P.S. It's ok to just get the full .uri() String in Netty/Vert.X, because this string contains only the path + query params.

@@ -10,6 +10,8 @@ import scala.collection.immutable.Seq
trait ServerRequest extends RequestMetadata {
def protocol: String
def connectionInfo: ConnectionInfo
/** Override in backend-specific implementation if you want a more efficient implementation and avoid uri parsing overhead */
def uriStr: String = uri.copy(scheme = None, authority = None, fragmentSegment = None).toString
Copy link
Member

Choose a reason for hiding this comment

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

or maybe impls should simple override showShort? Since we are doing some customisations here (removing the scheme etc.), which are not necessarily general

Copy link
Member

Choose a reason for hiding this comment

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

alternatively, we should rename uriStr to showUriShort

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, since uriStr will be used only rather only for showShort it seems like a good idea to skip it and just override that method 👍

@kciesielski kciesielski changed the title Introduce uriStr for better performance in logging Improve showShort to avoid parsing Uri Mar 11, 2024
@kciesielski kciesielski marked this pull request as ready for review March 11, 2024 15:49
@kciesielski kciesielski requested a review from adamw March 11, 2024 16:34
@@ -24,6 +24,7 @@ case class NettyServerRequest(req: HttpRequest, attributes: AttributeMap = Attri
QueryParams.fromMultiMap(multiMap)
}
override lazy val method: Method = Method.unsafeApply(req.method().name())
override lazy val showShort: String = s"$method ${req.uri()}"
Copy link
Member

Choose a reason for hiding this comment

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

we don't have to strip anything, or will this include the scheme & fragment here?

also, maybe worth adding some docs to showShort or ServerRequest that impls should override for performance?

Copy link
Member Author

Choose a reason for hiding this comment

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

I ran some tests on our Netty servers (and Vert.X, which is based on Netty), and req.uri returned only path + query params, without fragment, user info, etc.
Good idea to add a comment about overriding showShort, I'll do it.

@kciesielski kciesielski merged commit 87cb412 into master Mar 12, 2024
28 checks passed
@kciesielski kciesielski deleted the perf-uri-str branch March 12, 2024 07:12
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.

Optimize VertxServerRequest.uri
2 participants