Skip to content

Override .slice in ArrayOps to use arraycopy.#5250

Merged
adriaanm merged 2 commits intoscala:2.12.xfrom
dimatkach:fast-array-slice-3
Jul 28, 2016
Merged

Override .slice in ArrayOps to use arraycopy.#5250
adriaanm merged 2 commits intoscala:2.12.xfrom
dimatkach:fast-array-slice-3

Conversation

@dimatkach
Copy link

This makes it ~10x faster when copying large chunks arround.
My benchmark:

def bm(duration: Long)(f: => Unit): Int = {
   val end = System.currentTimeMillis + duration
   var count = 0
   while(System.currentTimeMillis < end) {
     f
     count += 1
   }
  count
}

def measure(seconds: Int)(f: => Unit) = (1 to seconds).map { _ => bm(1000)(f) }.sum / seconds

val array = scala.util.Random.alphanumeric.take(1000).toArray

measure(20) { array.slice(100, 500) }
// ~5 million
measure(20) { scala.collection.WrappedArray(array).slice(100, 500) }
// ~300K

@scala-jenkins scala-jenkins added this to the 2.12.0-RC1 milestone Jun 28, 2016
This makes it ~10x faster when copying large chunks arround.
My benchmark:

def bm(duration: Long)(f: => Unit): Int = {
val end = System.currentTimeMillis + duration
var count = 0
while(System.currentTimeMillis < end) {
f
count += 1
}
count
}

def measure(seconds: Int)(f: => Unit) = (1 to seconds).map { _ => bm(1000)(f) }.sum / seconds

val array = scala.util.Random.alphanumeric.take(1000).toArray

measure(20) { array.slice(100, 500) }
// ~5 million
measure(20) { scala.collection.WrappedArray(array).slice(100, 500) }
// ~300K
@dimatkach dimatkach force-pushed the fast-array-slice-3 branch from 25209d6 to add83b8 Compare June 28, 2016 14:06
@dimatkach
Copy link
Author

@Ichoran @axel22 would you guys care to take a look at this?

override def slice(from: Int, until: Int): Array[T] = {
val lo = math.max(from, 0)
val hi = math.min(math.max(until, 0), repr.length)
val size = math.max(hi-lo, 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

We usually delimit operators like - with whitespaces.

@axel22
Copy link
Contributor

axel22 commented Jul 11, 2016

Thanks for your contribution! LGTM on my side.

Please verify if there is a test for slice on arrays, and add it if there isn't.

@lrytz
Copy link
Member

lrytz commented Jul 20, 2016

@dimatkach for test coverage, there is at least https://github.com/scala/scala/blob/2.12.x/test/files/run/slices.scala. If you'd like to add to it, that test could be converted to a JUnit test (see https://github.com/scala/scala/tree/2.12.x/test/junit/scala/collection).

@dimatkach
Copy link
Author

@lrytz I am not sure I understand - why/where do I need to "add" it, if it's already there? :)
(not trying to be difficult, I am just new, and don't know the right way).
I know that it does get run by the CI, because I got several failures on this change, and had to fix them before it passed.

@lrytz
Copy link
Member

lrytz commented Jul 20, 2016

Oh, i was just pointing out the existing test, but you already know about it then. My question is if you think it is comprehensive enough. If you know of more corner cases that should be tested, you could add them to the test, and if you work on the test you could consider converting it to JUnit. JUnit tests run more efficiently.

Also, it would be nice to address Alex's code style remarks.

For setting up your local dev env, see https://github.com/scala/scala#get-ready-to-contribute.

@dimatkach
Copy link
Author

@lrytz @axel22 I have fixed the style issues you pointed out. Re. testing, slices.scala seems to be thorough enough, I could not think of any other cases that would be useful.

@axel22
Copy link
Contributor

axel22 commented Jul 27, 2016

LGTM

@adriaanm adriaanm added the welcome hello new contributor! label Jul 28, 2016
@adriaanm adriaanm merged commit 80ee988 into scala:2.12.x Jul 28, 2016
@adriaanm adriaanm added the 2.12 label Oct 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

welcome hello new contributor!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants