From b5786e4f67c166a11533660647196c15e04508cd Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Fri, 4 Dec 2026 14:30:06 -0800 Subject: Add interned TypeSet with duals, reset, and reusable scratch storage Retain the local/external default design and explicit rebasing API. Add uniform-default overloads for migrating existing finite/cofinite compiler sets without changing import semantics. Tests cover the lattice, discovery, rebasing, reset, and allocation-free warmed intern hits. Validation: make -k -j8 tests passes (834 executions); existing compiler sites still use XInt at this checkpoint. --- .../main/java/com/seaofnodes/simple/type/Type.java | 1 + .../java/com/seaofnodes/simple/type/TypeSet.java | 322 +++++++++++++++++++++ .../java/com/seaofnodes/simple/TypeSetTest.java | 186 ++++++++++++ 3 files changed, 509 insertions(+) create mode 100644 chapter33/src/main/java/com/seaofnodes/simple/type/TypeSet.java create mode 100644 chapter33/src/test/java/com/seaofnodes/simple/TypeSetTest.java diff --git a/chapter33/src/main/java/com/seaofnodes/simple/type/Type.java b/chapter33/src/main/java/com/seaofnodes/simple/type/Type.java index 221de04c..0b8d1a34 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/type/Type.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/type/Type.java @@ -257,6 +257,7 @@ public class Type /*implements Cloneable*/ { VISIT.clear(); INTERN.putAll(INTERN0); } + TypeSet.reset(); } // ---------------------------------------------------------- diff --git a/chapter33/src/main/java/com/seaofnodes/simple/type/TypeSet.java b/chapter33/src/main/java/com/seaofnodes/simple/type/TypeSet.java new file mode 100644 index 00000000..d49c1e25 --- /dev/null +++ b/chapter33/src/main/java/com/seaofnodes/simple/type/TypeSet.java @@ -0,0 +1,322 @@ +package com.seaofnodes.simple.type; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import com.seaofnodes.simple.util.Ary; +import com.seaofnodes.simple.util.BAOS; +import com.seaofnodes.simple.util.SB; + +/** + * Interned co-finite set with independent local and external defaults and + * finite XOR exceptions. Meet is union; dual complements both defaults, + * and shares the bits. This is a lattice component, not a new runtime Type. + * + * Used for FIDX, alias and RPC collections; callers keep those namespaces + * separate. Each default describes membership of undiscovered identities + * with that ownership. Uniform defaults need no ownership mask. + */ +public final class TypeSet { + private static final HashMap INTERN = new HashMap<>(); + private static final HashMap INTERN0 = new HashMap<>(); + private static final Ary FREE = new Ary<>(TypeSet.class); + private static final Ary> FREE_BITS = new Ary(Ary.class); + + public static final TypeSet EMPTY = intern(0,allocBits(0)); + public static final TypeSet FULL = EMPTY.dual(); + public static final TypeSet LOCAL = intern(1,allocBits(0)); + public static final TypeSet EXTERNAL = LOCAL.dual(); + public static final TypeSet SET1 = of(1); + public static final TypeSet SET3 = of(1,2); + public static final int[] NO_EXTERNAL = new int[0]; + + // Mutable only while probing or on the free list. + private byte _defaults;// Bit 0: local; bit 1: external. + private int[] _bits; // Finite exceptions, canonical zero padding. + private int _len; // Probe's significant words; interned arrays have this length. + private int _hash; + private TypeSet _dual; + + private TypeSet() { } + private static TypeSet malloc(int defaults, int[] bits, int len) { + TypeSet set = FREE.pop(); + if( set==null ) set = new TypeSet(); + set._defaults = (byte)defaults; + set._bits = bits; + set._len = len; + int hash = 1; + for( int i=0; i bitsFreeList(int len) { + Ary free = FREE_BITS.atX(len); + if( free==null ) FREE_BITS.setX(len,free = new Ary<>(int[].class)); + return free; + } + // All free arrays are zeroed. Retained arrays are never on this list. + private static int[] allocBits(int len) { + int[] bits = bitsFreeList(len).pop(); + return bits==null ? new int[len] : bits; + } + private static void freeBits(int[] bits) { + Arrays.fill(bits,0); + bitsFreeList(bits.length).push(bits); + } + + public static TypeSet make(boolean localDefault, boolean externalDefault) { + return localDefault ? externalDefault ? FULL : LOCAL : externalDefault ? EXTERNAL : EMPTY; + } + + /** The IDs are exceptions to the defaults, not necessarily members. */ + public static TypeSet make(boolean localDefault, boolean externalDefault, int... exceptions) { + int max = -1; + for( int id : exceptions ) { check(id); max = Math.max(max,id); } + int[] bits = allocBits((max+32)>>>5); + for( int id : exceptions ) bits[id>>>5] |= 1<0 && bits[len-1]==0 ) len--; + // Probe with a logical length before compacting: a hit needs neither + // a newly allocated candidate nor an array of the trimmed size. + TypeSet set = malloc(defaults,bits,len); + TypeSet old = INTERN.get(set); + if( old!=null ) { + freeBits(bits); + free(set); + return old; + } + if( len!=bits.length ) { + int[] compact = allocBits(len); + System.arraycopy(bits,0,compact,0,len); + freeBits(bits); + set._bits = bits = compact; + } + TypeSet dual = malloc(defaults^3,bits,len); + set._dual = dual; + dual._dual = set; + INTERN.put(set,set); + INTERN.put(dual,dual); + return set; + } + + /** Called by Type.reset after its basic types have initialized. Capture the + * initial table once, then discard per-compilation sets on subsequent resets. + * Like Type, callers must not carry compilation-specific sets across resets. */ + public static void reset() { + if( INTERN0.isEmpty() ) INTERN0.putAll(INTERN); + else { + for( Iterator it = INTERN.keySet().iterator(); it.hasNext(); ) { + TypeSet set = it.next(); + if( INTERN0.containsKey(set) ) continue; + it.remove(); + // A pair shares one array. Release it only after both keys + // leave INTERN; the first recycled partner has null bits. + if( set._dual._bits==null ) freeBits(set._bits); + free(set); + } + } + } + + public boolean localDefault() { return (_defaults & 1)!=0; } + public boolean externalDefault() { return (_defaults & 2)!=0; } + public boolean isFinite() { return _defaults==0; } + public boolean isEmpty() { return this==EMPTY; } + + public static TypeSet of(int... members) { return make(false,false,members); } + public static TypeSet of(int member) { return EMPTY.with(member,false); } + public boolean isConstant() { return isFinite() && bitCount()==1; } + public int bitCount() { + assert isFinite(); + int count = 0; + for( int bits : _bits ) count += Integer.bitCount(bits); + return count; + } + public int onlyBit() { + assert isConstant(); + return next(-1); + } + /** Finite membership enumeration, independent of ownership. */ + public int next(int after) { + assert isFinite(); + return nextKnown(after,_bits.length<<5,NO_EXTERNAL); + } + + public void packed(BAOS out) { + out.packed1(_defaults); + out.packed4(_bits.length); + for( int bits : _bits ) out.packed4(bits); + } + public static TypeSet unpack(BAOS in) { + int defaults = in.packed1(), len = in.packed4(); + if( defaults>3 || len<0 || len>in.buf().length-in.size() ) + throw new IllegalArgumentException("Invalid packed TypeSet"); + int[] bits = allocBits(len); + for( int i=0; i=0 ) { + sb.p(low); + if( i-1>low ) sb.p(i-1==low+1 ? ',' : '-').p(i-1); + sb.p(','); + low=-1; + } + } + sb.unchar(); + if( !isFinite() ) sb.p(localDefault()==externalDefault() ? ",..." : externalDefault() ? ",external..." : ",local..."); + return sb.p("]"); + } + + // Default membership for a word given its external-identity mask. + private int baseline(int external) { + return (localDefault() ? ~external : 0) | (externalDefault() ? external : 0); + } + // Word or zero, pre-scaled word index + private static int word(int[] bits, int i) { return i>>5) & (1<>>5) & (1<>>5)+1)); + System.arraycopy(_bits,0,bits,0,_bits.length); + bits[id>>>5] ^= 1<=limit-1 ) return -1; + int id = after+1, word = id>>>5; + int words = (limit+31)>>>5; + for( ; word>>5); + for( int id : ids ) { + if( (bits[id>>>5] & (1<>>5] |= 1<>>5] |= 1<