Skip to content

Instantly share code, notes, and snippets.

@nitsanw
nitsanw / Step1.java
Created October 31, 2012 17:41
Using Unsafe to break encapsulation - step 1
public static final Unsafe unsafe;
static
{
try
{
// This is a bit of voodoo to force the unsafe object into
// visibility and acquire it.
// This is not playing nice, but as an established back door it is
// not likely to be taken away.
Field field = Unsafe.class.getDeclaredField("theUnsafe");
@nitsanw
nitsanw / Step2.java
Created October 31, 2012 17:44
Using Unsafe to break encapsulation - step 2
private static final long valueFieldOffset;
private static final long countFieldOffset;
private static final long offsetFieldOffset;
static
{
try
{
valueFieldOffset = UnsafeAccess.unsafe.objectFieldOffset(String.class.getDeclaredField("value"));
countFieldOffset = UnsafeAccess.unsafe.objectFieldOffset(String.class.getDeclaredField("count"));
@nitsanw
nitsanw / Step3.java
Created October 31, 2012 17:47
Using Unsafe to break encapsulation - step 3
public final static String buildUnsafe(char[] chars){
String mutable = new String();// an empty string to hack
UnsafeAccess.unsafe.putObject(mutable,valueFieldOffset,chars);
UnsafeAccess.unsafe.putInt(mutable,countFieldOffset,chars.length);
return mutable;
}
public final static String buildUnsafe(char[] chars, int offset, int length){
String mutable = new String();// an empty string to hack
UnsafeAccess.unsafe.putObject(mutable,valueFieldOffset,chars);
UnsafeAccess.unsafe.putInt(mutable,countFieldOffset,length);
@nitsanw
nitsanw / gist:4254221
Created December 10, 2012 23:11
Ping pong experiment
...
// On the ping thread
for (long l = 0; l < iterations; l++) {
pingValue.set(l);
while (pongValue.get() != l)
;
}
...
// On the pong thread
@nitsanw
nitsanw / gist:4276162
Created December 13, 2012 12:49
VolatileLong
private volatile long value;
private static final long valueOffset;
static {
try {
valueOffset = UnsafeAccess.unsafe.objectFieldOffset(VolatileLong.class.getDeclaredField("value"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@nitsanw
nitsanw / gist:4276304
Created December 13, 2012 13:10
Catchup
final class Producer implements Runnable {
public void run() {
latch.countDown();
for (long l = 0; l < ITERATIONS; l++) {
values[(int) l] = l;
producerIndex.set(l);
}
}
}
@nitsanw
nitsanw / gist:4341270
Created December 19, 2012 22:45
Ping utility
// Server loop
...
accepted = serverSocket.accept();
accepted.socket().setTcpNoDelay(true);
accepted.configureBlocking(false);
serverSocket.close();
while (!Thread.interrupted()) {
buffy.clear();
do {
if (accepted.read(buffy) == -1)
@nitsanw
nitsanw / gist:4373453
Last active July 28, 2021 17:59
Aligning a byte buffer
private static final long addressOffset;
static {
try {
addressOffset = UnsafeAccess.unsafe.objectFieldOffset(Buffer.class.getDeclaredField("address"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static long getAddress(ByteBuffer buffy) {
@nitsanw
nitsanw / gist:4380323
Last active December 10, 2015 04:18
Unaligned Memory Access Cost Experiment
package alignment;
import java.nio.ByteBuffer;
import util.UnsafeAccess;
import static util.UnsafeDirectByteBuffer.*;
import com.google.caliper.Param;
import com.google.caliper.SimpleBenchmark;
@nitsanw
nitsanw / gist:4587721
Last active December 11, 2015 10:38
Build your project using Ivy
<project xmlns:ivy="antlib:org.apache.ivy.ant" name="TheBestestProjectEver" default="build">
<property name="ivy.install.version" value="2.0.0-beta1" />
<property name="ivy.jar.dir" value="${basedir}/ivy" />
<property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar" />
<property name="allocation.jar.file" value="${basedir}/lib/allocation.jar" />
<condition property="skip.download.ivy">
<and>
<available file="${ivy.jar.file}"/>
</and>
</condition>