Skip to content
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

Introduce Chunk.ByteBuffer #1032

Merged
merged 1 commit into from
Dec 27, 2017

Conversation

rossabaker
Copy link
Member

Optimization for streams sourced from and sunk to ByteBuffers. This avoids copying bytes on direct buffers, and the overhead of wrapping and unwrapping array-backed buffers.

Moves .toByteBuffer operation to Chunk with Byte evidence.

Brings back some properties for Chunk, and tests the new implementation.

@rossabaker
Copy link
Member Author

Benchmark results:

[info] ByteBufferChunkBenchmark.byteBufferToArrayDirect             16  thrpt   15   39555429.510 ±  1836441.329  ops/s
[info] ByteBufferChunkBenchmark.byteBufferToArrayDirect            256  thrpt   15   20822644.569 ±   878054.247  ops/s
[info] ByteBufferChunkBenchmark.byteBufferToArrayDirect           4096  thrpt   15    2125880.384 ±    45082.611  ops/s
[info] ByteBufferChunkBenchmark.byteBufferToArrayDirect          65536  thrpt   15     116307.258 ±     4548.623  ops/s
[info] ByteBufferChunkBenchmark.byteBufferToArrayIndirect           16  thrpt   15   39834349.743 ±  1522890.804  ops/s
[info] ByteBufferChunkBenchmark.byteBufferToArrayIndirect          256  thrpt   15   24413239.513 ±   698907.398  ops/s
[info] ByteBufferChunkBenchmark.byteBufferToArrayIndirect         4096  thrpt   15    2147766.329 ±    49367.281  ops/s
[info] ByteBufferChunkBenchmark.byteBufferToArrayIndirect        65536  thrpt   15     116634.652 ±     3822.902  ops/s
[info] ByteBufferChunkBenchmark.byteBufferToByteBufferDirect        16  thrpt   15   96388608.780 ±  3377881.737  ops/s
[info] ByteBufferChunkBenchmark.byteBufferToByteBufferDirect       256  thrpt   15   95285259.935 ±  3271901.056  ops/s
[info] ByteBufferChunkBenchmark.byteBufferToByteBufferDirect      4096  thrpt   15   95825487.141 ±  3450158.834  ops/s
[info] ByteBufferChunkBenchmark.byteBufferToByteBufferDirect     65536  thrpt   15   96296780.742 ±  3677006.417  ops/s
[info] ByteBufferChunkBenchmark.byteBufferToByteBufferIndirect      16  thrpt   15   95686295.134 ±  3944239.315  ops/s
[info] ByteBufferChunkBenchmark.byteBufferToByteBufferIndirect     256  thrpt   15   95990518.934 ±  3560116.694  ops/s
[info] ByteBufferChunkBenchmark.byteBufferToByteBufferIndirect    4096  thrpt   15   97207226.670 ±  3141449.250  ops/s
[info] ByteBufferChunkBenchmark.byteBufferToByteBufferIndirect   65536  thrpt   15   92200737.924 ±  7827950.566  ops/s
[info] ByteBufferChunkBenchmark.bytesToArrayDirect                  16  thrpt   15   22825652.038 ±   771640.686  ops/s
[info] ByteBufferChunkBenchmark.bytesToArrayDirect                 256  thrpt   15   13227440.969 ±   255189.720  ops/s
[info] ByteBufferChunkBenchmark.bytesToArrayDirect                4096  thrpt   15    1010456.593 ±    30501.736  ops/s
[info] ByteBufferChunkBenchmark.bytesToArrayDirect               65536  thrpt   15      58017.448 ±     1793.885  ops/s
[info] ByteBufferChunkBenchmark.bytesToArrayIndirect                16  thrpt   15   45887579.351 ±  1349560.349  ops/s
[info] ByteBufferChunkBenchmark.bytesToArrayIndirect               256  thrpt   15   26496985.198 ±   677484.268  ops/s
[info] ByteBufferChunkBenchmark.bytesToArrayIndirect              4096  thrpt   15    2156347.360 ±    55485.379  ops/s
[info] ByteBufferChunkBenchmark.bytesToArrayIndirect             65536  thrpt   15      94756.123 ±    30073.334  ops/s
[info] ByteBufferChunkBenchmark.bytesToByteBufferDirect             16  thrpt   15   25181312.443 ±  3362596.240  ops/s
[info] ByteBufferChunkBenchmark.bytesToByteBufferDirect            256  thrpt   15   18497443.083 ±  2906172.291  ops/s
[info] ByteBufferChunkBenchmark.bytesToByteBufferDirect           4096  thrpt   15    1555702.723 ±   513057.694  ops/s
[info] ByteBufferChunkBenchmark.bytesToByteBufferDirect          65536  thrpt   15      83188.649 ±    31867.940  ops/s
[info] ByteBufferChunkBenchmark.bytesToByteBufferIndirect           16  thrpt   15   61358102.286 ± 21776616.164  ops/s
[info] ByteBufferChunkBenchmark.bytesToByteBufferIndirect          256  thrpt   15   80975320.552 ± 31098491.086  ops/s
[info] ByteBufferChunkBenchmark.bytesToByteBufferIndirect         4096  thrpt   15   54238556.198 ± 23289971.763  ops/s
[info] ByteBufferChunkBenchmark.bytesToByteBufferIndirect        65536  thrpt   15  103662792.632 ±  5520931.058  ops/s

JByteBuffer.wrap(c.values, c.offset, c.length)
case c: Chunk.ByteBuffer =>
val b = c.buf.asReadOnlyBuffer
if (c.offset == 0 && b.position == 0 && c.size == b.limit) b
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know that this actually saves a lot. This trick is inspired by scodec.bits.ByteVector.

// }

// "map.long => long" in forAll { c: Chunk[Long] => (c map (1 +)).toArray shouldBe (c.toArray map (1 +)) }
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Commented because #1033.

second.position(n + offset)
(ByteBuffer(first), ByteBuffer(second))
}
override def map[O2](f: Byte => O2): Chunk[O2] = {
Copy link
Member Author

Choose a reason for hiding this comment

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

I haven't benchmarked this against other plausible implementations. I think this will be faster than bulk-getting the array and mapping that, but I haven't proven it.

@pchlupacek
Copy link
Contributor

pchlupacek commented Dec 27, 2017

@ChristopherDavenport, @rossabaker looks great to me. thanks for this. lets take @mpilquist eyes on this before its merged ok ?

@mpilquist mpilquist merged commit 8064c76 into typelevel:series/0.10 Dec 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants