diff options
| author | Cliff Click <cliffc@acm.org> | 2026-08-10 11:11:18 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-11-16 16:23:52 -0800 |
| commit | 0b263f550d5e54d050f58f6b315b092944c9eff9 (patch) | |
| tree | aff564cf11711c41a6f94f17ffbc480e1b228da1 /src/main/java/com/seaofnodes/simple/util | |
| parent | 790268f4a1f6122376c34059f1633c1017436363 (diff) | |
| download | Simple-linear-chapter32.tar.gz Simple-linear-chapter32.zip | |
Chapter 32: Compiler Internal Speedupslinear-chapter32linear
Co-authored-by: Hsiang-Ying Fu <66062782+fuhsnn@users.noreply.github.com>
Co-authored-by: fuhsnn <66062782+fuhsnn@users.noreply.github.com>
Co-authored-by: Symious <14933944+symious@users.noreply.github.com>
Co-authored-by: Jun Kawasaki <jun784@gmail.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Diffstat (limited to 'src/main/java/com/seaofnodes/simple/util')
| -rw-r--r-- | src/main/java/com/seaofnodes/simple/util/BAOS.java | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/src/main/java/com/seaofnodes/simple/util/BAOS.java b/src/main/java/com/seaofnodes/simple/util/BAOS.java index 61ce7387..c47d386c 100644 --- a/src/main/java/com/seaofnodes/simple/util/BAOS.java +++ b/src/main/java/com/seaofnodes/simple/util/BAOS.java @@ -1,7 +1,8 @@ package com.seaofnodes.simple.util; -import java.util.HashMap; +//import java.util.HashMap; import java.util.Arrays; +import java.nio.charset.StandardCharsets; // "ByteArrayOutputStream", and Input Stream and not synchronized and has // matching pack/unpack strategies. @@ -96,12 +97,12 @@ public class BAOS { } // ----------------------------------------------------------------------- - // Write a packed integer to the BAOS. Track a histogram to make more - // intelligent choices in the future. - private static final HashMap<Long,Integer> PACKED_HISTOGRAM = new HashMap<>(); + // Optional profiling for choosing smaller packed representations. Leave + // disabled during normal compilation; re-enable when studying .simple size. + //private static final HashMap<Long,Integer> PACKED_HISTOGRAM = new HashMap<>(); private BAOS histo(long x) { - Integer cnt = PACKED_HISTOGRAM.get(x); - PACKED_HISTOGRAM.put(x,cnt==null ? 1 : cnt+1); + //Integer cnt = PACKED_HISTOGRAM.get(x); + //PACKED_HISTOGRAM.put(x,cnt==null ? 1 : cnt+1); return this; } @@ -125,15 +126,29 @@ public class BAOS { return x==0xFF ? read2() : x; } - // Write a packed i32 + // Signed zigzag varint: -64..63 use one byte, -8192..8191 two, + // and the full int range needs at most five. Seven payload bits per byte. public void packed4(int x) { - ((-127 <= x && x <= 127 ) ? write(x) : write(-128).write4(x)).histo(x); + histo(x); + int bits = (x<<1) ^ (x>>31); + while( (bits & ~127)!=0 ) { + write((bits & 127) | 128); + bits >>>= 7; + } + write(bits); } // Read a packed i32 public int packed4() { - int x = _buf[_len++]; // Signed byte read - return x== -128 ? read4() : x; + int bits=0; + for( int shift=0; shift<32; shift+=7 ) { + int b = read(); + if( shift==28 && (b & 0xF0)!=0 ) + throw new IllegalArgumentException("Packed int overflow"); + bits |= (b & 127)<<shift; + if( (b & 128)==0 ) return (bits>>>1) ^ -(bits & 1); + } + throw new IllegalArgumentException("Unterminated packed int"); } // Write a packed i64 @@ -148,14 +163,16 @@ public class BAOS { } public void packedS(String s) { - packed4(s.length()); - for( int i=0; i<s.length(); i++ ) - packed4(s.charAt(i)); + // Strings are bytes, not signed packed integers. Keep ASCII at one + // byte per character and agree on the representation of non-ASCII. + byte[] utf8 = s.getBytes(StandardCharsets.UTF_8); + packed4(utf8.length); + write(utf8); } public String packedS() { int len = packed4(); - String s = new String(_buf,_len,len).intern(); + String s = new String(_buf,_len,len,StandardCharsets.UTF_8).intern(); _len += len; return s; } |
