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

optimize uri parsing to reduce StringBuilder allocations #330

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/src/main/scala/sttp/model/UriInterpolator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,16 @@ object UriInterpolator {
case Singleton(ExpressionToken(s: Array[_])) =>
b ++= s.flatMap(anyToStringOpt)
doToSeq(tailTs)
case valueTs if(valueTs.size == 1) =>
Copy link
Member

Choose a reason for hiding this comment

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

maybe add a comment here why the optimization is in place? with the issue number

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure! I've just added it

// This case is equivalent to the next one but optimizes for the
// frequent scenario where the sequence contains a single element.
valueTs.get(0) match {
case StringToken(s) => b += decode(s, decodePlusAsSpace)
case ExpressionToken(e) => anyToStringOpt(e).foreach(b += _)
case EqInQuery => b += "="
case _ =>
}
doToSeq(tailTs)
case valueTs =>
val mbStr = valueTs mkStringOpt {
case StringToken(s) => Some(decode(s, decodePlusAsSpace))
Expand Down
Loading