Optimize control flow with block-level dispatcher sharing#161
Merged
Conversation
Implements block-level dispatcher sharing to eliminate redundant control flow dispatch code when multiple calls occur in the same block with the same visible loops. Key improvements: - Multiple calls in same block share ONE dispatcher (not one per call) - Call sites reduced from ~150 bytes to ~20 bytes each - Block dispatcher emitted once per unique loop state (~150 bytes) - 36-76% bytecode savings for blocks with 2+ calls Results: - Test with 4 sequential calls: 2232 → 2139 lines (4.2% reduction) - CHECKCAST operations: 23 → 17 (26% reduction) - All 2006 unit tests pass Implementation: - JavaClassInfo: Added blockDispatcherLabels map and getLoopStateSignature() - EmitSubroutine: Simplified call sites, added emitBlockDispatcher() helper - Dispatcher stays within loop scope (no frame computation issues) - Automatic signature-based reuse via identity hash codes Trade-offs: - Single call: 23 bytes overhead (acceptable) - Multiple calls: massive savings (61% for 4 calls, 76% for 10 calls) - Net win for typical Perl code patterns Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
fglock
added a commit
that referenced
this pull request
Feb 4, 2026
With block-level dispatcher sharing (PR #161), non-local control flow now works correctly. The skip() function can use 'last SKIP' directly without workarounds. Changes: - Test/More.pm: Replaced skip_internal() with proper skip() that uses last SKIP - TestMoreHelper.java: Removed skip() call rewriting logic - test.pl.patch: Removed skip_internal() workaround from Perl 5 tests Testing: - All 2012 unit tests pass (100%) - Perl 5 tests work correctly with native skip() implementation - Non-local last SKIP exits SKIP block immediately from subroutine This cleanup removes ~100 lines of workaround code that is no longer needed. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements block-level dispatcher sharing to eliminate redundant control flow dispatch code when multiple calls occur in the same block with the same visible loops.
The Problem
Previously, each call site emitted a complete control flow dispatcher (~150 bytes):
Total: 600 bytes of redundant code for 4 calls.
The Solution
Block-level shared dispatchers - all calls with the same visible loop state share ONE dispatcher:
blockDispatcherLabelsmapResults
Bytecode Savings
Real-World Measurements
Implementation Details
Modified Files
JavaClassInfo.java
blockDispatcherLabelsmap to track dispatcher reusegetLoopStateSignature()method for unique loop state identificationEmitSubroutine.java
emitBlockDispatcher()helper methodDocumentation
BLOCK_DISPATCHER_OPTIMIZATION.md- detailed optimization guideCONTROL_FLOW_IMPLEMENTATION.md- comprehensive implementation guideHow It Works
blockDispatcherLabelsTrade-offs
Advantages:
Disadvantages:
Net Result: Overall WIN for typical Perl code patterns.
Testing
All 2006 unit tests pass, including:
Why This Works Better Than Alternatives
vs. Per-Call Dispatchers (Previous)
vs. Method-Level Centralization (Attempted)
Block-level is the sweet spot: sharing within proper scope boundaries.
🤖 Generated with Claude Code