From bdd64ac9ba7f6802ec27561374e8c9aea0577d2c Mon Sep 17 00:00:00 2001 From: brian Date: Tue, 6 Jul 2004 10:42:49 +0000 Subject: [PATCH] jsstring caching darcs-hash:20040706104249-24bed-bbff3a3b5232fc7709897a7f856f997512350722.gz --- src/org/ibex/js/JS.java | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/org/ibex/js/JS.java b/src/org/ibex/js/JS.java index d8d0f33..04d7edd 100644 --- a/src/org/ibex/js/JS.java +++ b/src/org/ibex/js/JS.java @@ -209,7 +209,15 @@ public abstract class JS { public static final JS B(boolean b) { return b ? T : F; } public static final JS B(int i) { return i==0 ? F : T; } - public static final JS S(String s) { return new JSString(s); } + private static final int CACHE_SIZE = 65536 / 4; // must be a power of two + private static final JSString[] stringCache = new JSString[CACHE_SIZE]; + public static final JS S(String s) { + if(s == null) return null; + int slot = s.hashCode()&(CACHE_SIZE-1); + JSString ret = stringCache[slot]; + if(ret == null || !ret.s.equals(s)) stringCache[slot] = ret = new JSString(s); + return ret; + } public static final JS N(double d) { return new JSNumber.D(d); } public static final JS N(long l) { return new JSNumber.L(l); } @@ -220,20 +228,20 @@ public abstract class JS { return N(n.doubleValue()); } - private static final JSNumber.I[] smallIntCache = new JSNumber.I[65535 / 4]; - private static final JSNumber.I[] largeIntCache = new JSNumber.I[65535 / 4]; + private static final JSNumber.I[] smallIntCache = new JSNumber.I[CACHE_SIZE]; + private static final JSNumber.I[] largeIntCache = new JSNumber.I[CACHE_SIZE]; public static final JS N(int i) { JSNumber.I ret = null; int idx = i + smallIntCache.length / 2; - if (idx < smallIntCache.length && idx > 0) { + if (idx < CACHE_SIZE && idx > 0) { ret = smallIntCache[idx]; if (ret != null) return ret; } - else ret = largeIntCache[Math.abs(idx % largeIntCache.length)]; + else ret = largeIntCache[Math.abs(idx % CACHE_SIZE)]; if (ret == null || ret.i != i) { ret = new JSNumber.I(i); if (idx < smallIntCache.length && idx > 0) smallIntCache[idx] = ret; - else largeIntCache[Math.abs(idx % largeIntCache.length)] = ret; + else largeIntCache[Math.abs(idx % CACHE_SIZE)] = ret; } return ret; } -- 1.7.10.4