Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finagle-http: Optimize HttpMuxer.normalize
Problem `c.t.f.http.HttpMuxer.normalize` is slower than it could be. Solution Optimize it. Result Significantly faster and far less allocations. The common case when the path is already normalized is 96% faster and has 0 allocations. JIRA Issues: CSL-8520 Differential Revision: https://phabricator.twitter.biz/D343261
- Loading branch information
1 parent
4b0493c
commit 0ef03b8
Showing
3 changed files
with
108 additions
and
13 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
finagle-benchmark/src/main/scala/com/twitter/finagle/http/HttpMuxerBenchmark.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.twitter.finagle.http | ||
|
||
import com.twitter.finagle.benchmark.StdBenchAnnotations | ||
import org.openjdk.jmh.annotations.{Benchmark, Scope, State} | ||
|
||
@State(Scope.Benchmark) | ||
class HttpMuxerBenchmark extends StdBenchAnnotations { | ||
|
||
private[this] var i = 0 | ||
|
||
private[this] val alreadyNormalized = IndexedSeq( | ||
"/foo/bar", | ||
"/cool/cool/cool", | ||
"/" | ||
) | ||
|
||
private[this] val excessiveSlashes = IndexedSeq( | ||
"/foo///bar", | ||
"/cool//cool////cool", | ||
"////" | ||
) | ||
|
||
private[this] val missingLeadingSlash = IndexedSeq( | ||
"foo/bar", | ||
"cool/cool/cool" | ||
) | ||
|
||
@Benchmark | ||
def normalize_alreadyNormalized: String = { | ||
i += 1 | ||
if (i < 0) i = 0 | ||
val path = alreadyNormalized(i % alreadyNormalized.size) | ||
HttpMuxer.normalize(path) | ||
} | ||
|
||
@Benchmark | ||
def normalize_excessiveSlashes: String = { | ||
i += 1 | ||
if (i < 0) i = 0 | ||
val path = excessiveSlashes(i % excessiveSlashes.size) | ||
HttpMuxer.normalize(path) | ||
} | ||
|
||
@Benchmark | ||
def normalize_missingLeadingSlash: String = { | ||
i += 1 | ||
if (i < 0) i = 0 | ||
val path = missingLeadingSlash(i % missingLeadingSlash.size) | ||
HttpMuxer.normalize(path) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters