Skip to content

Instantly share code, notes, and snippets.

View rednaxelafx's full-sized avatar

Kris Mok rednaxelafx

View GitHub Profile
@rednaxelafx
rednaxelafx / TrapDemo.java
Created October 22, 2016 09:12
Java implicit null check example. Run on Oracle JDK8u101
public class TrapDemo {
public static void demo(IntBox p) {
int dead = p.value;
if (p == null) return;
p.value = 42;
}
public static void main(String[] args) throws Exception {
demo(null);
}
@rednaxelafx
rednaxelafx / Demo$$Lambda$1.javap
Created October 12, 2016 08:20
Java 8 serializable lambda pitfall example, https://www.zhihu.com/question/51491241
$ javap -verbose -c -s -l -private Demo\$\$Lambda\$1
Classfile /private/tmp/Demo$$Lambda$1.class
Last modified Oct 12, 2016; size 754 bytes
MD5 checksum 81ce9dafbca34417b2dfa94f596a85a1
final class Demo$$Lambda$1 implements org.apache.spark.api.java.function.VoidFunction
minor version: 0
major version: 52
flags: ACC_FINAL, ACC_SUPER, ACC_SYNTHETIC
Constant pool:
#1 = Utf8 Demo$$Lambda$1
@rednaxelafx
rednaxelafx / Demo.j
Created June 8, 2016 02:11
HotSpot C1 doesn't compile this "monitorenter with Phi input", fails with "invalid parsing"
.class public Demo
.super java/lang/Object
.field public static dummy I
.method public static foo(Ljava/lang/Object;Ljava/lang/Object;Z)V
.limit stack 2
.limit locals 3
iload_2
@rednaxelafx
rednaxelafx / HashMapStackOverflow.java
Last active July 2, 2018 06:42
JDK8: StackOverflowError of HashMap due to self reference
import java.util.*;
public class HashMapStackOverflow {
public static void main(String[] args) throws Exception {
HashMap<String, Object> map = new HashMap<>();
map.put("self", map);
System.out.println(map.hashCode());
}
}
@rednaxelafx
rednaxelafx / command_prompt
Created July 16, 2015 21:39
Demo of Clang++'s implementation of C++ static local variable
$ clang++ -S -emit-llvm yy.cc
@rednaxelafx
rednaxelafx / test_libjit.cc
Last active August 16, 2022 06:36
Comparing different JIT libraries via contrived simple examples
#include <iostream>
// #include <jit/jit-dump.h>
#include <jit/jit-plus.h>
// Create an example function equivalent to:
// int foo(int x, int y, int z) {
// return x + y * z;
// }
jit_function create_example_foo(jit_context& context, int opt_level) {
jit_type_t signature = jit_function::signature_helper(
@rednaxelafx
rednaxelafx / TestVec.java
Created February 9, 2015 23:32
Simple demo of autovectorization in Oracle JDK8u25 on Linux/x64 with AVX
import java.util.*;
public class TestVec {
public static int[] doTest(int[] xs, int[] ys) {
if (xs.length != ys.length) return null;
int[] zs = new int[xs.length];
for (int i = 0; i < xs.length; i++) {
zs[i] = xs[i] * ys[i];
}
return zs;
@rednaxelafx
rednaxelafx / .hotspot_compiler
Created March 22, 2014 01:23
Demo of HotSpot C2 optimizing consecutive instanceof's. Run on JDK8 (b132). C2 missed an optimization for CmpP(LoadKlass(AddP(DecodeNKlass(LoadNKlass(...)))), ConP), should use CmpN(LoadNKlass(), ConNKlass), probably due to NPG changes.
exclude SubtypeTest main
dontinline SubtypeTest *
compileonly SubtypeTest driver
compileonly SubtypeTest getObject
@rednaxelafx
rednaxelafx / range_check.patch
Last active April 8, 2018 02:10
A quick-n-dirty patch to implement JDK-8003585: strength reduce or eliminate range checks for power-of-two sized arrays
diff -r 9d39e8a8ff61 src/share/vm/opto/subnode.cpp
--- a/src/share/vm/opto/subnode.cpp Fri Dec 27 07:51:07 2013 -0800
+++ b/src/share/vm/opto/subnode.cpp Wed Feb 12 14:59:00 2014 -0800
@@ -1206,6 +1206,40 @@
Node *ncmp = phase->transform( new (phase->C) CmpINode(cmp1->in(2),cmp2));
return new (phase->C) BoolNode( ncmp, _test.commute() );
}
+
+ // Change ((x & m) <= m) into (m >= 0)
+ // Integer expressions which perform bitwise and can be proven to
@rednaxelafx
rednaxelafx / cheney.cpp
Last active September 29, 2021 14:00
Pseudo-code that implements Cheney's algorithm for copying GC
// Pseudo-code that implements Cheney's algorithm
class Object {
// remains null for normal objects
// non-null for forwarded objects
Object* _forwardee;
public:
void forward_to(address new_addr);
Object* forwardee();