Skip to content
This repository has been archived by the owner on Apr 20, 2020. It is now read-only.

Commit

Permalink
Pool StringBuilders
Browse files Browse the repository at this point in the history
  • Loading branch information
slandelle committed Jan 4, 2017
1 parent a8e6a25 commit 0eb11ee
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/main/scala/io/gatling/jsonpath/Parser.scala
Expand Up @@ -22,8 +22,19 @@ import scala.util.parsing.combinator.RegexParsers
import io.gatling.jsonpath.AST._

object FastStringOps {

private val StringBuilderPool = new ThreadLocal[JStringBuilder] {
override def initialValue(): JStringBuilder = new JStringBuilder
}

private def pooledStringBuilder = {
val sb = StringBuilderPool.get()
sb.setLength(0)
sb
}

implicit class RichString(val text: String) extends AnyVal {
def fastReplaceAll(searchString: String, replacement: String): String = {
def fastReplaceAll(searchString: String, replacement: String): String =
if (searchString.isEmpty || replacement.isEmpty) {
text
} else {
Expand All @@ -32,7 +43,7 @@ object FastStringOps {
if (end == -1) {
text
} else {
val buf = new JStringBuilder(text.length)
val buf = pooledStringBuilder
while (end != -1) {
buf.append(text, start, end).append(replacement)
start = end + searchString.length
Expand All @@ -42,7 +53,6 @@ object FastStringOps {
buf.toString
}
}
}
}
}

Expand Down

0 comments on commit 0eb11ee

Please sign in to comment.