From c21026c5377429b11e285d251439407074fe3cd1 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Mon, 16 Nov 2026 09:15:19 -0800 Subject: One more qucik speedup item --- chapter32/README.ja.md | 3 ++ chapter32/README.md | 54 ++++++++++++++++++++++ .../simple/node/cpus/x86_64_v2/x86_64_v2.java | 7 +-- .../main/java/com/seaofnodes/simple/util/BAOS.java | 12 ++--- 4 files changed, 67 insertions(+), 9 deletions(-) diff --git a/chapter32/README.ja.md b/chapter32/README.ja.md index 05c091ec..30d3c26c 100644 --- a/chapter32/README.ja.md +++ b/chapter32/README.ja.md @@ -18,6 +18,9 @@ - IFG の live-out マップと作業領域を関数ごとに持ち、スピルによる再試行の間だけ再利用します。 過去のコンパイルの基本ブロックを保持し続けることがなくなります。 スピル候補の評価に使う関数の大きさも、彩色の試行ごとに一度だけ計算します。 +- ロードを命令に組み込めるか調べる走査では、訪問済みノードを ID の BitSet で管理します。 + パック値のヒストグラムはコメントアウトし、通常のコンパイルでは収集しません。 + ELF の `.simple` セクションのサイズと内訳も測定し、今後の形式改善の基準にします。 測定ではワークリストからの peephole 呼び出しが約446万回から370万回へ、17.2%減少しました。 詳細な測定条件、実装の説明、残る調査対象は[英語版](README.md)を参照してください。 diff --git a/chapter32/README.md b/chapter32/README.md index b3fb09ea..35387ec2 100644 --- a/chapter32/README.md +++ b/chapter32/README.md @@ -314,6 +314,60 @@ the native allocator checks. Benchmark sources, saved baseline classes, raw timings, and follow-up JFR recordings are in `build/ifg-profile/`; the suite log is `build/test-profile/ifg-full.log`. +## Reduce allocation in load folding and packed writes + +The x86 [load-fold legality check](src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java) +walks the other operand's inputs to ensure that combining it with a load cannot +move the read across a conflicting write. Its visited set now uses node IDs in +a local BitSet. The traversal, including control dependencies, is unchanged; +the check avoids growing arrays of identity-map keys and values. + +[BAOS](src/main/java/com/seaofnodes/simple/util/BAOS.java)'s packed-value +histogram was exploratory instrumentation for choosing smaller encodings. +Its map and updates are commented out, ready to restore for another packing +study. Normal compilation no longer boxes histogram keys and counts. Packed +bytes and their readers are unchanged. + +Against `6643e8fd`, an interleaved comparison measured sys compiler-thread +allocation falling from 119.6 to 69.4 MiB and median compile time from 1,017 to +942 ms. Breakout fell from 54.5 to 43.6 MiB and 652 to 617 ms. The smaller timing +differences were mixed: Clock measured 315 to 318 ms, graphical tic-tac-toe +292 to 291 ms, and HelloWorld 65 to 64 ms. All five allocate less. These use the +same three-warmup, six-measurement method and JVM settings as the IFG comparison. +All 26 resulting objects have byte-identical `.text` and `.simple` sections. +The Windows suite at log8 and the Linux suite at the default log10 each pass +all 805 test executions, including native scheduling and import coverage. + +## Measure serialized IR size before changing its packing + +The ELF `.simple` section stores reloadable ideal IR, declarations, and import +metadata. It can be much larger than the native code. For the same x86-64/win64 +objects, including all 22 sys modules: + +| Workload | Whole ELF bytes | `.simple` bytes | Share of ELF | +| --- | ---: | ---: | ---: | +| sys library, all modules | 597,399 | 469,881 | 78.7% | +| Clock | 64,317 | 43,854 | 68.2% | +| Breakout | 118,163 | 80,950 | 68.5% | +| Graphical tic-tac-toe | 64,605 | 44,353 | 68.7% | +| HelloWorld | 16,355 | 14,635 | 89.5% | + +Temporary byte-offset counters around [Serialize](src/main/java/com/seaofnodes/simple/codegen/Serialize.java)'s +existing write stages divide sys's payload into 67,844 bytes of strings and +unit metadata, 73,564 of identity mappings, 180,456 of types, 2,447 of function +summaries, and 145,482 of node records and input references, plus 88 header +bytes. HelloWorld's strings, mappings, and types alone take 14,161 of its 14,635 +bytes; its graph records and inputs take only 447 bytes. + +That suggests investigating metadata scope before choosing a different integer +encoding. The current writer emits the full compilation's alias, function, and +return-PC mappings into each object, along with their names. Restricting them +to identities needed by that object's declarations, summaries, and graph may +save space, but requires coordinated remapping in the writer and reader. +These measurements make no packing or format changes. Profiling helpers, +saved baseline classes, per-object sizes, byte breakdowns, and timings are in +`build/packing-profile/`. + ## Build and test From `chapter32`, using the existing offline dependency caches: diff --git a/chapter32/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java b/chapter32/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java index d4714953..9e0d864a 100644 --- a/chapter32/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java +++ b/chapter32/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java @@ -5,7 +5,7 @@ import com.seaofnodes.simple.node.*; import com.seaofnodes.simple.type.*; import com.seaofnodes.simple.util.Utils; import com.seaofnodes.simple.util.Ary; -import java.util.IdentityHashMap; +import java.util.BitSet; public class x86_64_v2 extends Machine { public x86_64_v2( CodeGen code ) {} @@ -461,12 +461,13 @@ public class x86_64_v2 extends Machine { if( load.nOuts()!=1 ) return false; Ary writers=load.antiDeps(new Ary<>(Node.class)); if( writers.isEmpty() ) return true; - var seen=new IdentityHashMap(); + var seen=new BitSet(); Ary work=new Ary<>(Node.class); work.push(value); while( !work.isEmpty() ) { Node n=work.pop(); - if( seen.put(n,Boolean.TRUE)!=null ) continue; + if( seen.get(n._nid) ) continue; + seen.set(n._nid); if( writers.find(n)!=-1 ) return false; for( Node def : n._inputs ) if( def!=null ) work.push(def); } diff --git a/chapter32/src/main/java/com/seaofnodes/simple/util/BAOS.java b/chapter32/src/main/java/com/seaofnodes/simple/util/BAOS.java index 61ce7387..a4d1f183 100644 --- a/chapter32/src/main/java/com/seaofnodes/simple/util/BAOS.java +++ b/chapter32/src/main/java/com/seaofnodes/simple/util/BAOS.java @@ -1,6 +1,6 @@ package com.seaofnodes.simple.util; -import java.util.HashMap; +//import java.util.HashMap; import java.util.Arrays; // "ByteArrayOutputStream", and Input Stream and not synchronized and has @@ -96,12 +96,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 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 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; } -- cgit v1.2.3