Skip to content

Instantly share code, notes, and snippets.

class SpscBoundedQueue<E> extends RingBuffer<E>{
long head = 0;
long tailIsClearUntil = 0;
long tail = 0;
//... constructor and so on
boolean offer(E e) { // e must not be null, ignoring for brevity
if (tail >= tailIsClearUntil) {
if (null != lvElement(tail + LOOK_AHEAD)){
return false;
}
@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:6700797
Last active December 12, 2018 07:40
Lamport queue
public final class P1C1QueueOriginal1<E> {
private final E[] buffer;
private volatile long tail = 0;
private volatile long head = 0;
public P1C1QueueOriginal1(final int capacity) {
buffer = (E[]) new Object[capacity];
}
public boolean offer(final E e) {
public class WhenWillItExit {
public static void main(String[] argc) throws InterruptedException {
Thread t = new Thread(() -> {
long l = 0;
for (int i = 0; i < Integer.MAX_VALUE; i++) {
for (int j = 0; j < Integer.MAX_VALUE; j++) {
if ((j & 1) == 1)
l++;
}
}
struct mpscq_node_t {
mpscq_node_t* volatile next;
void* state;
};
struct mpscq_t {
mpscq_node_t* volatile head;
mpscq_node_t* tail;
};
Index: jctools-core/src/main/java/org/jctools/maps/NonBlockingHashMap.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- jctools-core/src/main/java/org/jctools/maps/NonBlockingHashMap.java (revision a2191c1e0243c8cd4690990d6b250b26a3ca8bfa)
+++ jctools-core/src/main/java/org/jctools/maps/NonBlockingHashMap.java (revision )
@@ -650,134 +650,130 @@
int reprobe_cnt=0;
Object K=null, V=null;
0.18% 0.00% │││ │ 0x00007f485a84780d: lock addl $0x0,(%rsp) ;*invokevirtual putObjectRelease {reexecute=0 rethrow=0 return_oop=0}
│││ │ ; - sun.misc.Unsafe::putOrderedObject@7 (line 1042)
│││ │ ; - org.jctools.util.UnsafeRefArrayAccess::soElement@6 (line 77)
│││ │ ; - org.jctools.queues.SpscArrayQueue::offer@64 (line 187)
│││ │ ; - org.jctools.jmh.throughput.QueueThroughputBackoffNone::offer@8 (line 92)
│││ │ ; - org.jctools.jmh.throughput.generated.QueueThroughputBackoffNone_tpt_jmhTest::offer_thrpt_jmhStub@17 (line 299)
22.58% 31.59% │││ │ 0x00007f485a847812: inc %rbp ;*ladd {reexecute=0 rethrow=0 return_oop=0}
│││ │
// CPP, OpenJDK C2 compiler style... this is from:
// http://hg.openjdk.java.net/jdk9/jdk9/hotspot/file/b756e7a2ec33/src/share/vm/opto/graphKit.cpp#l4285
void GraphKit::g1_write_barrier_post(Node* oop_store,
Node* obj,
Node* adr,
uint alias_idx,
Node* val,
BasicType bt,
bool use_precise) {
/// ... Loads of other crap here...
# c2, level 4, org.jctools.jmh.throughput.generated.QueueThroughputBackoffNone_tpt_jmhTest::offer_thrpt_jmhStub, version 643 (89 bytes)
0,46%| 0xb254: mov %r11,%rcx ;*getfield buffer {reexecute=0 rethrow=0 return_oop=0}
| ; - org.jctools.queues.SpscArrayQueue::offer@14 (line 176)
| ; - org.jctools.jmh.throughput.QueueThroughputBackoffNone::offer@8 (line 92)
| ; - org.jctools.jmh.throughput.generated.QueueThroughputBackoffNone_tpt_jmhTest::offer_thrpt_jmhStub@17 (line 299)
| 0xb257: lea 0x10(%r11,%r10,4),%r13 ;*invokevirtual getObjectVolatile {reexecute=0 rethrow=0 return_oop=0}
| ; - sun.misc.Unsafe::getObjectVolatile@5 (line 923)
| ; - org.jctools.util.UnsafeRefArrayAccess::lvElement@5 (line 103)
| ; - org.jctools.queues.SpscArrayQueue::offerSlowPath
@nitsanw
nitsanw / gist:db28ed42a0a4b8c05d95
Last active August 16, 2017 19:33
NOOP.ASS part I
Decoding compiled method 0x00007fe5a106bb90:
Code:
[Entry Point]
[Constants]
# {method} 'noop_avgt_jmhLoop' '(Lorg/openjdk/jmh/runner/InfraControl;Lorg/openjdk/jmh/results/RawResults;Lpsy/lob/saw/generated/BaselineBenchmarks_noop$BaselineBenchmarks_1_jmh;Lpsy/lob/saw/generate
d/BaselineBenchmarks_noop$Blackhole_1_jmh;)V' in 'psy/lob/saw/generated/BaselineBenchmarks_noop'
# this: rsi:rsi = 'psy/lob/saw/generated/BaselineBenchmarks_noop'
# parm0: rdx:rdx = 'org/openjdk/jmh/runner/InfraControl'
# parm1: rcx:rcx = 'org/openjdk/jmh/results/RawResults'
# parm2: r8:r8 = 'psy/lob/saw/generated/BaselineBenchmarks_noop$BaselineBenchmarks_1_jmh'