Skip to content

Commit

Permalink
Merge pull request #670 from usev6/nqp_singleton
Browse files Browse the repository at this point in the history
[JVM] Use singleton object for VMnull
  • Loading branch information
jnthn authored Sep 25, 2020
2 parents 37963db + fc058f8 commit 41f143f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/vm/jvm/runtime/org/raku/nqp/runtime/Ops.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
import org.raku.nqp.sixmodel.reprs.VMHashInstance;
import org.raku.nqp.sixmodel.reprs.VMIterInstance;
import org.raku.nqp.sixmodel.reprs.VMNull;
import org.raku.nqp.sixmodel.reprs.VMNullInstance;
import org.raku.nqp.sixmodel.reprs.VMThreadInstance;

import sun.misc.Unsafe;
Expand Down Expand Up @@ -159,6 +160,8 @@ public static void disableWarning() {
}
}

private static SixModelObject theVMNull = null;

/* I/O opcodes */
public static String print(String v, ThreadContext tc) {
tc.gc.out.print(v);
Expand Down Expand Up @@ -2349,11 +2352,12 @@ public static long eqaddr(SixModelObject a, SixModelObject b) {
return a == b ? 1 : 0;
}
public static SixModelObject createNull(ThreadContext tc) {
SixModelObject vmnull = tc.gc.VMNull.st.REPR.allocate(tc, tc.gc.VMNull.st);
return vmnull;
if (theVMNull == null)
theVMNull = VMNullInstance.getInstance(tc);
return theVMNull;
}
public static long isnull(SixModelObject obj) {
return obj == null || (obj.st != null && obj.st.REPR instanceof VMNull) ? 1 : 0;
return obj == null || obj == theVMNull ? 1 : 0;
}
public static long isnull_s(String str) {
return str == null ? 1 : 0;
Expand Down
4 changes: 1 addition & 3 deletions src/vm/jvm/runtime/org/raku/nqp/sixmodel/reprs/VMNull.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public SixModelObject type_object_for(ThreadContext tc, SixModelObject HOW) {

@Override
public SixModelObject allocate(ThreadContext tc, STable st) {
VMNullInstance obj = new VMNullInstance();
obj.st = st;
return obj;
return VMNullInstance.getInstance(tc);
}

@Override
Expand Down
19 changes: 19 additions & 0 deletions src/vm/jvm/runtime/org/raku/nqp/sixmodel/reprs/VMNullInstance.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
package org.raku.nqp.sixmodel.reprs;

import org.raku.nqp.runtime.ThreadContext;
import org.raku.nqp.sixmodel.SixModelObject;

public class VMNullInstance extends SixModelObject {
// singleton object
private static volatile VMNullInstance theVMNull;

private VMNullInstance(ThreadContext tc) {
this.st = tc.gc.VMNull.st;
}

public synchronized static VMNullInstance getInstance(ThreadContext tc) {
// double check to make singleton thread safe
if (theVMNull == null) {
synchronized (VMNullInstance.class) {
if (theVMNull == null)
theVMNull = new VMNullInstance(tc);
}
}

return theVMNull;
}
}

0 comments on commit 41f143f

Please sign in to comment.