=act improve ByteString#take(...) (#21438)

Currently, we use ByteStringBuilder to create a new ByteString instance,
which would not be quite efficient.

Instead of doing this, we can do as follows so that we can achieve better performance:
1. Seek the index of _last_ vector element we need to _take_
2. Find the number of characters left to take from the _last_ ByteString1 element.
3. Create ByteString based on the information we obtained from 1 and 2

Then we just need to create a new Vector[ByteString1] at most twice, which should be
better than the current implementation, i.e., _append_ a new element every time we check
bytestrings(Vector[ByteString1]) element, which ends up O(N) _append_ execution where _N_ is
the length of bytestrings.
This commit is contained in:
monkey-mas 2016-09-22 12:40:54 +09:00 committed by Konrad Malawski
parent 94d7237d17
commit f8f8828451
2 changed files with 37 additions and 13 deletions

View file

@ -308,6 +308,19 @@ class ByteStringSpec extends WordSpec with Matchers with Checkers {
ByteString1.fromString("ab").drop(2) should ===(ByteString(""))
ByteString1.fromString("ab").drop(3) should ===(ByteString(""))
}
"take" in {
ByteString1.empty.take(-1) should ===(ByteString(""))
ByteString1.empty.take(0) should ===(ByteString(""))
ByteString1.empty.take(1) should ===(ByteString(""))
ByteString1.fromString("a").take(1) should ===(ByteString("a"))
ByteString1.fromString("ab").take(-1) should ===(ByteString(""))
ByteString1.fromString("ab").take(0) should ===(ByteString(""))
ByteString1.fromString("ab").take(1) should ===(ByteString("a"))
ByteString1.fromString("ab").take(2) should ===(ByteString("ab"))
ByteString1.fromString("ab").take(3) should ===(ByteString("ab"))
ByteString1.fromString("0123456789").take(3).drop(1) should ===(ByteString("12"))
ByteString1.fromString("0123456789").take(10).take(8).drop(3).take(5) should ===(ByteString("34567"))
}
}
"ByteString1C" must {
"drop(0)" in {
@ -415,6 +428,9 @@ class ByteStringSpec extends WordSpec with Matchers with Checkers {
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("bc")).dropRight(3) should ===(ByteString(""))
}
"take" in {
ByteString.empty.take(-1) should ===(ByteString(""))
ByteString.empty.take(0) should ===(ByteString(""))
ByteString.empty.take(1) should ===(ByteString(""))
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("bc")).drop(1).take(0) should ===(ByteString(""))
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("bc")).drop(1).take(-1) should ===(ByteString(""))
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("bc")).drop(1).take(-2) should ===(ByteString(""))