Skip to content

SI-7710 fix memory performance of RegexParsers in jdk7u6+ #17

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

Merged
merged 1 commit into from
Jun 25, 2014
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ trait RegexParsers extends Parsers {
*/
protected def handleWhiteSpace(source: java.lang.CharSequence, offset: Int): Int =
if (skipWhitespace)
(whiteSpace findPrefixMatchOf (source.subSequence(offset, source.length))) match {
(whiteSpace findPrefixMatchOf (new SubSequence(source, offset))) match {
case Some(matched) => offset + matched.end
case None => offset
}
Expand Down Expand Up @@ -107,7 +107,7 @@ trait RegexParsers extends Parsers {
val source = in.source
val offset = in.offset
val start = handleWhiteSpace(source, offset)
(r findPrefixMatchOf (source.subSequence(start, source.length))) match {
(r findPrefixMatchOf (new SubSequence(source, start))) match {
case Some(matched) =>
Success(source.subSequence(start, start + matched.end).toString,
in.drop(start + matched.end - offset))
Expand Down
32 changes: 32 additions & 0 deletions src/main/scala/scala/util/parsing/combinator/SubSequence.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2006-2013, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */


package scala
package util.parsing.combinator

// A shallow wrapper over another CharSequence (usually a String)
//
// See SI-7710: in jdk7u6 String.subSequence stopped sharing the char array of the original
// string and began copying it.
// RegexParsers calls subSequence twice per input character: that's a lot of array copying!
private[combinator] class SubSequence(s: CharSequence, start: Int, val length: Int) extends CharSequence {
def this(s: CharSequence, start: Int) = this(s, start, s.length - start)

def charAt(i: Int) =
if (i >= 0 && i < length) s.charAt(start + i) else throw new IndexOutOfBoundsException(s"index: $i, length: $length")

def subSequence(_start: Int, _end: Int) = {
if (_start < 0 || _end < 0 || _end > length || _start > _end)
throw new IndexOutOfBoundsException(s"start: ${_start}, end: ${_end}, length: $length")

new SubSequence(s, start + _start, _end - _start)
}

override def toString = s.subSequence(start, start + length).toString
}