|
| 1 | +--- |
| 2 | +name: java-perf-reviewer |
| 3 | +description: Code review skill for high-throughput Java data processing, focusing on memory bounds (FFM API), zero-copy I/O, and hot-loop optimization. |
| 4 | +--- |
| 5 | + |
| 6 | +## Overview |
| 7 | +This skill provides specialized instructions for reviewing high-performance Java code, particularly data processing pipelines, columnar decoders, and hot-loop execution. When asked to review Java code for performance, apply these advanced heuristics rather than generic advice. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## Core Optimization Rules |
| 12 | + |
| 13 | +### 1. Hot-Loop Optimization |
| 14 | +- **Loop Unswitching:** Hoist polymorphic branches (e.g., `switch` on primitive types) *outside* tight loops to improve branch predictability and enable vectorization. |
| 15 | +- **Strength Reduction:** Replace repeated multiplications (`base + i * stride`) with incremental additions (`v += stride`). |
| 16 | + - Warn about IEEE‑754 drift when accumulating `float`/`double` over large iteration counts. |
| 17 | +- **Invariant Hoisting:** Move loop-invariant loads, bounds, and metadata outside the loop. |
| 18 | +- **Predictability:** Flag unpredictable, data-dependent branches inside hot loops. |
| 19 | +- **Bounds Check Elimination:** Encourage patterns that allow the JIT to remove array bounds checks (e.g., using indexed loops with prevalidated ranges). |
| 20 | +- **mark opportunities for Java Vector API** |
| 21 | +--- |
| 22 | + |
| 23 | +### 2. Memory Rules: Heap, Direct, and FFM |
| 24 | +- **2GB Limit:** Flag any `(int) (n * elemBytes)` or similar size computation that may overflow `Integer.MAX_VALUE` (~2.14 GB). |
| 25 | +- **Prefer FFM for Large Buffers:** Recommend migrating large `ByteBuffer` allocations to the Foreign Function & Memory API (`MemorySegment`). |
| 26 | + - Use `Arena.ofAuto().allocate(size, alignment)` for 64‑bit capacity and fast unaligned access via `ValueLayout`. |
| 27 | +- **Alignment & VarHandles:** |
| 28 | + - Warn when using `ValueLayout.JAVA_INT` or similar on potentially unaligned addresses. |
| 29 | + - Prefer `ValueLayout` constants over manual offset math. |
| 30 | + - Flag aliasing between overlapping `MemorySegment` slices. |
| 31 | +- **Mixed Heap/Off‑Heap Pipelines:** |
| 32 | + - Flag transitions between heap arrays, direct buffers, and `MemorySegment` unless explicitly justified. |
| 33 | + - Require clear ownership and lifetime rules when mixing arenas, slices, and native handles. |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +### 3. Zero-Copy I/O and Protobuf |
| 38 | +- **Eliminate Intermediate `byte[]`:** Flag any heap array created solely to bridge between `ByteBuffer` and parsing frameworks. |
| 39 | +- **Direct Protobuf Parsing:** |
| 40 | + - Recommend `CodedInputStream.newInstance(ByteBuffer)` for metadata parsing. |
| 41 | + - Note: Protobuf does not support `MemorySegment`; `ByteBuffer` is optimal for small metadata payloads. |
| 42 | + |
| 43 | +--- |
| 44 | + |
| 45 | +## Profiling-Driven Overrides |
| 46 | +When the user provides profiling data (JFR, async-profiler, perf, flamegraphs): |
| 47 | + |
| 48 | +- Prioritize empirical evidence over static heuristics. |
| 49 | +- If a rule contradicts profiling data, warn but defer to the measurements. |
| 50 | +- Encourage validating changes with before/after profiles. |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## Conflict Resolution Rules |
| 55 | +- **Correctness > Performance:** If an optimization risks semantic drift, warn instead of rewriting. |
| 56 | +- **FFM vs ByteBuffer:** Prefer FFM for large or long-lived buffers; prefer `ByteBuffer` for tiny metadata payloads or Protobuf interop. |
| 57 | +- **Precision vs Speed:** When strength reduction introduces floating-point drift, warn and let the user decide. |
| 58 | +- **Safety First:** If memory aliasing, lifetime, or alignment is unclear, flag it even if performance would improve. |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +## When to Apply |
| 63 | +Apply these guidelines automatically whenever the user asks to: |
| 64 | + |
| 65 | +- Review array, columnar, or sequence decoders. |
| 66 | +- Audit large dataset memory allocations (I/O, buffers). |
| 67 | +- Review code using FFM (`MemorySegment`), NIO (`ByteBuffer`), or JNI. |
| 68 | +- Optimize hot loops, vectorizable loops, or tight decode/encode paths. |
| 69 | +- Improve zero-copy I/O, Protobuf parsing, or off-heap memory usage. |
| 70 | + |
| 71 | + |
0 commit comments