Skip to content

fix: Java loop ID calculation and assertion transformer bug#1638

Merged
misrasaurabh1 merged 8 commits intoomni-javafrom
loop-id-calculation-fix-bug
Feb 23, 2026
Merged

fix: Java loop ID calculation and assertion transformer bug#1638
misrasaurabh1 merged 8 commits intoomni-javafrom
loop-id-calculation-fix-bug

Conversation

@misrasaurabh1
Copy link
Copy Markdown
Contributor

Implemented CUDA-style loop ID calculation for performance mode:

  • loopId = outerLoop * maxInnerIterations + innerIteration
  • Behavior mode uses simple loop index (no inner iterations)
  • Invocation ID simplified to call counter only
  • Default CODEFLASH_INNER_ITERATIONS set to 10

Fixed critical bug in JavaAssertTransformer:

  • Removed duplicate _special_re assignment that was missing parentheses
  • Combined patterns into single regex: ["'{}()]
  • This fixes _find_balanced_parens and enables assertion transformation

Updated test expectations to match new marker format and loop ID calculation.

All 41 Java instrumentation tests passing.

misrasaurabh1 and others added 2 commits February 22, 2026 21:04
Implemented CUDA-style loop ID calculation for performance mode:
- loopId = outerLoop * maxInnerIterations + innerIteration
- Behavior mode uses simple loop index (no inner iterations)
- Invocation ID simplified to call counter only
- Default CODEFLASH_INNER_ITERATIONS set to 10

Fixed critical bug in JavaAssertTransformer:
- Removed duplicate _special_re assignment that was missing parentheses
- Combined patterns into single regex: [\"'{}()]
- This fixes _find_balanced_parens and enables assertion transformation

Updated test expectations to match new marker format and loop ID calculation.

All 41 Java instrumentation tests passing.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
f'{indent}int _cf_loop{current_id} = Integer.parseInt(System.getenv("CODEFLASH_LOOP_INDEX"));',
f'{indent}int _cf_innerIterations{current_id} = Integer.parseInt(System.getenv().getOrDefault("CODEFLASH_INNER_ITERATIONS", "100"));',
f'{indent}int _cf_outerLoop{current_id} = Integer.parseInt(System.getenv("CODEFLASH_LOOP_INDEX"));',
f'{indent}int _cf_maxInnerIterations{current_id} = Integer.parseInt(System.getenv().getOrDefault("CODEFLASH_INNER_ITERATIONS", "10"));',
Copy link
Copy Markdown
Contributor

@claude claude Bot Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Fixed in latest commit

@@ -231,7 +236,7 @@ def _generate_sqlite_write_code(
f"{inner_indent} _cf_pstmt{iter_id}_{call_counter}.setString(3, _cf_test{iter_id});",
Copy link
Copy Markdown
Contributor

@claude claude Bot Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Fixed in latest commit

@claude
Copy link
Copy Markdown
Contributor

claude Bot commented Feb 23, 2026

PR Review Summary

Prek Checks

All checks pass — ruff check and ruff format both passed on changed files.

Mypy

⚠️ 2 issues fixed, many remaining in new Java files

  • Fixed: Variable type mismatch in comparator.py and formatter.py where shutil.which() return (str | None) was assigned to a Path-typed variable. Renamed to java_which to avoid mypy narrowing conflict.
  • Remaining: ~40+ mypy errors across new Java support files — primarily union-attr (Optional without None guards), missing generic type parameters, and operator errors on toml types. These require logic changes or are complex generic issues and were not modified per guidelines.

Code Review

Existing comments: Both previous inline comments are already marked as ✅ Fixed.

Re-review of latest changes — no critical bugs found. Specific observations:

  1. Composite loopId calculation (instrumentation.py): The loopId = outerLoop * maxInnerIterations + innerIteration formula is correct and consistent across both code paths (single-statement and multi-statement).

  2. loop_count += 1 for Java (function_optimizer.py:2758,3013): Correct — effective_loop_count() returns max(loop_indices) which is 0-indexed for Java, so +1 gives the display count.

  3. Redundant variable declarations: Both _cf_maxInnerIterations and _cf_innerIterations are set from the same env var CODEFLASH_INNER_ITERATIONS. This works correctly (one is the multiplier for composite IDs, the other is the loop bound), but could be simplified to a single variable in a follow-up.

  4. remove_asserts.py: Good fix — removed duplicate _special_re definition that was overwriting itself with a different pattern. Now a single correct regex including parens.

  5. init_java.py: Replaced hasattr(get_java_formatter_cmd, "_warning_shown") pattern with a module-level global formatter_warning_shown. Cleaner approach.

  6. parser.py: Replaced type: ignore[assignment] with a proper assert check for _cached_cum_bytes. Better type safety.

Test Coverage

Most changed files are new Java support files that don't exist on main, so no regression comparison is possible. Coverage on PR branch for key changed files:

File Stmts Miss Cover
languages/java/instrumentation.py 583 108 81%
languages/java/parser.py 338 5 99%
languages/java/remove_asserts.py 462 64 86%
languages/java/concurrency_analyzer.py 112 13 88%
languages/java/config.py 235 35 85%
languages/java/context.py 469 54 88%
languages/java/discovery.py 104 13 88%
languages/java/line_profiler.py 144 14 90%
languages/java/test_discovery.py 319 31 90%
languages/java/import_resolver.py 130 16 88%
languages/java/comparator.py 159 117 26%
languages/java/formatter.py 143 51 64%
languages/java/build_tools.py 423 177 58%
languages/java/replacement.py 330 132 60%
languages/java/support.py 178 67 62%
languages/java/test_runner.py 785 452 42%
cli_cmds/init_java.py 302 249 18%
optimization/function_optimizer.py 1324 1052 21%

⚠️ Low coverage on comparator.py (26%), test_runner.py (42%), and init_java.py (18%) — these are new files that would benefit from additional test coverage.

Test results: 3159 passed, 58 skipped, 21 failed (all failures are pre-existing — tracer tests and a comparator test requiring Java runtime JAR not available in CI).

Codeflash Optimization PRs

No optimization PRs are mergeable — all 13 open codeflash-ai[bot] PRs have failing CI checks (snyk limits, JS test failures, Windows test failures).


Last updated: 2026-02-23

@codeflash-ai
Copy link
Copy Markdown
Contributor

codeflash-ai Bot commented Feb 23, 2026

⚡️ Codeflash found optimizations for this PR

📄 54% (0.54x) speedup for _generate_sqlite_write_code in codeflash/languages/java/instrumentation.py

⏱️ Runtime : 9.66 milliseconds 6.28 milliseconds (best of 185 runs)

A dependent PR with the suggested changes has been created. Please review:

If you approve, it will be merged into this PR (branch loop-id-calculation-fix-bug).

Static Badge

misrasaurabh1 and others added 3 commits February 22, 2026 22:27
In Java performance mode, loop IDs are calculated as:
  loopId = outerLoop * maxInnerIterations + innerIteration

With 0-indexed inner iterations, this causes the max loop ID to end
with 9 (e.g., 209, 299) when the actual total iteration count should
be a round number (210, 300).

This change adds +1 to the loop count when displaying for Java only,
so that "299 loops" becomes "300 loops" to accurately reflect the
total number of iterations run.

Python loop counts remain unchanged as they use 1-based indexing.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@codeflash-ai
Copy link
Copy Markdown
Contributor

codeflash-ai Bot commented Feb 23, 2026

⚡️ Codeflash found optimizations for this PR

📄 13% (0.13x) speedup for get_java_formatter_cmd in codeflash/cli_cmds/init_java.py

⏱️ Runtime : 1.45 milliseconds 1.29 milliseconds (best of 154 runs)

A dependent PR with the suggested changes has been created. Please review:

If you approve, it will be merged into this PR (branch loop-id-calculation-fix-bug).

Static Badge

misrasaurabh1 and others added 3 commits February 22, 2026 23:42
Changed iteration_id in performance mode markers to properly encode
inner loop iterations for test case grouping:

- Single call: iteration_id = innerIteration (0, 1, 2...)
- Multiple calls: iteration_id = callId_innerIteration (1_0, 1_1, 2_0, 2_1...)

This allows test results to be properly grouped by InvocationId, where
each unique (call, inner_iteration) pair gets its own group for
calculating minimum runtimes across outer loops.

Fixed test expectations to match the new format.

All 43 Java performance tests passing.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@misrasaurabh1 misrasaurabh1 merged commit 82a8c41 into omni-java Feb 23, 2026
23 of 29 checks passed
@misrasaurabh1 misrasaurabh1 deleted the loop-id-calculation-fix-bug branch February 23, 2026 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant