use of sun.misc.Unsafe in project hazelcast by hazelcast.
the class UnsafeUtil method findUnsafe.
private static Unsafe findUnsafe() {
try {
return Unsafe.getUnsafe();
} catch (SecurityException se) {
return AccessController.doPrivileged(new PrivilegedAction<Unsafe>() {
@Override
public Unsafe run() {
try {
Class<Unsafe> type = Unsafe.class;
try {
Field field = type.getDeclaredField("theUnsafe");
field.setAccessible(true);
return type.cast(field.get(type));
} catch (Exception e) {
for (Field field : type.getDeclaredFields()) {
if (type.isAssignableFrom(field.getType())) {
field.setAccessible(true);
return type.cast(field.get(type));
}
}
}
} catch (Throwable t) {
throw rethrow(t);
}
throw new RuntimeException("Unsafe unavailable");
}
});
}
}
use of sun.misc.Unsafe in project flink by apache.
the class ListViaRangeSpeedMiniBenchmark method main.
public static void main(String[] args) throws Exception {
final File rocksDir = new File("/tmp/rdb");
FileUtils.deleteDirectory(rocksDir);
final Options options = new Options().setCompactionStyle(CompactionStyle.LEVEL).setLevelCompactionDynamicLevelBytes(true).setIncreaseParallelism(4).setUseFsync(false).setMaxOpenFiles(-1).setDisableDataSync(true).setCreateIfMissing(true).setMergeOperator(new StringAppendOperator());
final WriteOptions write_options = new WriteOptions().setSync(false).setDisableWAL(true);
final RocksDB rocksDB = RocksDB.open(options, rocksDir.getAbsolutePath());
final String key = "key";
final String value = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ7890654321";
final byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
final byte[] valueBytes = value.getBytes(StandardCharsets.UTF_8);
final byte[] keyTemplate = Arrays.copyOf(keyBytes, keyBytes.length + 4);
final Unsafe unsafe = MemoryUtils.UNSAFE;
final long offset = unsafe.arrayBaseOffset(byte[].class) + keyTemplate.length - 4;
final int num = 50000;
System.out.println("begin insert");
final long beginInsert = System.nanoTime();
for (int i = 0; i < num; i++) {
unsafe.putInt(keyTemplate, offset, i);
rocksDB.put(write_options, keyTemplate, valueBytes);
}
final long endInsert = System.nanoTime();
System.out.println("end insert - duration: " + ((endInsert - beginInsert) / 1_000_000) + " ms");
final byte[] resultHolder = new byte[num * valueBytes.length];
final long beginGet = System.nanoTime();
final RocksIterator iterator = rocksDB.newIterator();
int pos = 0;
// seek to start
unsafe.putInt(keyTemplate, offset, 0);
iterator.seek(keyTemplate);
// mark end
unsafe.putInt(keyTemplate, offset, -1);
// iterate
while (iterator.isValid()) {
byte[] currKey = iterator.key();
if (samePrefix(keyBytes, currKey)) {
byte[] currValue = iterator.value();
System.arraycopy(currValue, 0, resultHolder, pos, currValue.length);
pos += currValue.length;
iterator.next();
} else {
break;
}
}
final long endGet = System.nanoTime();
System.out.println("end get - duration: " + ((endGet - beginGet) / 1_000_000) + " ms");
}
use of sun.misc.Unsafe in project caffeine by ben-manes.
the class UnsafeAccess method load.
static Unsafe load(String openJdk, String android) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Field field;
try {
// try OpenJDK field name
field = Unsafe.class.getDeclaredField(openJdk);
} catch (NoSuchFieldException e) {
try {
// try Android field name
field = Unsafe.class.getDeclaredField(android);
} catch (NoSuchFieldException e2) {
// try to create a new instance
Constructor<Unsafe> unsafeConstructor = Unsafe.class.getDeclaredConstructor();
unsafeConstructor.setAccessible(true);
return unsafeConstructor.newInstance();
}
}
field.setAccessible(true);
return (Unsafe) field.get(null);
}
use of sun.misc.Unsafe in project groovy-core by groovy.
the class FastStringUtils method loadUnsafe.
/**
* @return Unsafe
*/
private static Unsafe loadUnsafe() {
try {
Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
unsafeField.setAccessible(true);
return (Unsafe) unsafeField.get(null);
} catch (Exception e) {
return null;
}
}
use of sun.misc.Unsafe in project es6draft by anba.
the class UnsafeHolder method initializeUnsafe.
private static Unsafe initializeUnsafe() {
try {
return Unsafe.getUnsafe();
} catch (SecurityException e) {
try {
return AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
return (Unsafe) f.get(null);
});
} catch (PrivilegedActionException e2) {
throw new ExceptionInInitializerError(e2.getException());
}
}
}
Aggregations