Skip to content

Optimized Uri.withQuery - #637

Merged
raboof merged 3 commits into
apache:mainfrom
davidik1:optimize-uri-withquery
Dec 16, 2024
Merged

Optimized Uri.withQuery#637
raboof merged 3 commits into
apache:mainfrom
davidik1:optimize-uri-withquery

Conversation

@davidik1

Copy link
Copy Markdown

Uri.withQuery receives a Query object, converts it to a string, and then calls the Uri.copy method which creates a new Uri object with the given query (The Uri class holds the query as an un-parsed string).
The copy method ends up in queryString.map(new UriParser(_).parseRawQueryString()) which percent-encodes the query string.
As in this flow, the query string is converted from a Query object, it is always percent-encoded, so the additional encoding operation is unnecessary. We can skip it by replacing the call to copy with createUnsafe.

@pjfanning pjfanning added this to the 1.1.1 milestone Dec 12, 2024

@raboof raboof left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Logic-wise makes sense to me.

UriSpec.scala already has some tests such as:

uri.withQuery(Query("param1" -> "val\"ue1")) shouldEqual Uri("http://host/path?param1=val%22ue1#fragment")

This is a good start, but perhaps it would be good to add some more test cases with Query instances that are created with the other Query constructors to validate that these also produce the correct results. I'm pretty convinced that this implementation will indeed behave correctly, but good to codify.

*/
def withQuery(query: Query): Uri = copy(rawQueryString = if (query.isEmpty) None else Some(query.toString))
def withQuery(query: Query): Uri =
createUnsafe(scheme, authority, path, if (query.isEmpty) None else Some(query.toString), fragment)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider adding a copyUnsafe method and calling it here and also in all the other with... methods (this assumes that the query stored inside Uri is also already encoded).

@davidik1
davidik1 requested review from YoniKF and raboof December 15, 2024 09:53

@He-Pin He-Pin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm, thanks

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

fails to compile in scala 2.12 build

[error] /home/runner/work/pekko-http/pekko-http/http-core/src/test/scala/org/apache/pekko/http/scaladsl/model/UriSpec.scala:824:36: value addOne is not a member of scala.collection.mutable.Builder[(String, String),org.apache.pekko.http.scaladsl.model.Uri.Query]
[error]       val query = Query.newBuilder.addOne("param1" -> "val\"ue1").result()
[error]                                    ^
Error: value addOne is not a member of scala.collection.mutable.Builder[(String, String),org.apache.pekko.http.scaladsl.model.Uri.Query]

You need to avoid the addOne function.

@davidik1
davidik1 requested a review from pjfanning December 15, 2024 14:11

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the fast turnaround on the compile issue.

@raboof
raboof merged commit 4554337 into apache:main Dec 16, 2024
@pjfanning pjfanning modified the milestones: 1.1.1, 1.2.0 Jan 9, 2025
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.

5 participants