Skip to content

Instantly share code, notes, and snippets.

@toddlipcon
Created August 8, 2018 20:55
Show Gist options
  • Save toddlipcon/885d36cc5e0097c4628f454d3deb23a6 to your computer and use it in GitHub Desktop.
Save toddlipcon/885d36cc5e0097c4628f454d3deb23a6 to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2014, Oracle America, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Oracle nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.sample;
import java.nio.ByteBuffer;
import org.openjdk.jmh.annotations.Benchmark;
public class MyBenchmark {
private static short getShortFromArray(final byte[] b, final int offset) {
return (short) ((b[offset + 1] & 0xFF) | (b[offset] << 8));
}
private static int getIntFromArray(final byte[] b, final int offset) {
return (b[offset + 0] & 0xFF) << 24 |
(b[offset + 1] & 0xFF) << 16 |
(b[offset + 2] & 0xFF) << 8 |
(b[offset + 3] & 0xFF) << 0;
}
/**
* Original implementation as used by FlatBuffers
*/
private static class FileDesc {
private final ByteBuffer bb;
int bbPos;
FileDesc(ByteBuffer bb) {
bbPos = bb.getShort(bb.position()) + bb.position();
this.bb = bb;
}
public int getVal() {
return bb.getInt(bbPos);
}
}
/**
* Direct implementation using a byte[]
*/
private static class FileDesc2 {
private final byte[] bb;
int bbPos;
FileDesc2(byte[] bb) {
bbPos = getShortFromArray(bb, 0);
this.bb = bb;
}
public int getVal() {
return getIntFromArray(bb, bbPos);
}
}
/**
* Implementation using a DataAccessor to wrap either a ByteBuffer
* or a ByteArray.
*
* Unfortunately, it seems like escape analysis is not able to
* do scalar replacement of this class :(
*/
private static class FileDesc3 {
private final DataAccessor bb;
int bbPos;
FileDesc3(DataAccessor bb) {
bbPos = bb.getShort(bb.position()) + bb.position();
this.bb = bb;
}
public int getVal() {
return bb.getInt(bbPos);
}
}
private interface DataAccessor {
int position();
int getInt(int pos);
short getShort(int pos);
}
private static class ByteArrayAccessor implements DataAccessor {
private byte[] b;
public ByteArrayAccessor(byte[] b) {
this.b = b;
}
public ByteArrayAccessor() {}
public int position() { return 0; }
public short getShort(int pos) {
return getShortFromArray(b, pos);
}
public int getInt(int pos) {
return getIntFromArray(b, pos);
}
}
static final byte[] foo = new byte[]{0, 4,3,4,5,6,7,8,9};
@Benchmark
public int testWithByteBuffer() {
return new FileDesc(ByteBuffer.wrap(foo)).getVal();
}
@Benchmark
public int testWithBytes() {
return new FileDesc2(foo).getVal();
}
@Benchmark
public int testWithBytesWrappedInAccessor() {
return new FileDesc3(new ByteArrayAccessor(foo)).getVal();
}
private static ThreadLocal<ByteArrayAccessor> tlAccessor =
ThreadLocal.withInitial(() -> new ByteArrayAccessor());
@Benchmark
public int testWithBytesWrappedInThreadLocalAccessor() {
ByteArrayAccessor acc = tlAccessor.get();
acc.b = foo;
return new FileDesc3(acc).getVal();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment