Skip to content

Commit f42b58b

Browse files
committed
feat: implement bubble sort optimization and corresponding tests in Java
1 parent 41814cd commit f42b58b

7 files changed

Lines changed: 316 additions & 2 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
name: E2E - Java Void Optimization (No Git)
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- 'codeflash/languages/java/**'
7+
- 'codeflash/languages/base.py'
8+
- 'codeflash/languages/registry.py'
9+
- 'codeflash/optimization/**'
10+
- 'codeflash/verification/**'
11+
- 'code_to_optimize/java/**'
12+
- 'codeflash-java-runtime/**'
13+
- 'tests/scripts/end_to_end_test_java_void_optimization.py'
14+
- '.github/workflows/e2e-java-void-optimization.yaml'
15+
16+
workflow_dispatch:
17+
18+
concurrency:
19+
group: ${{ github.workflow }}-${{ github.ref_name }}
20+
cancel-in-progress: true
21+
22+
jobs:
23+
java-void-optimization-no-git:
24+
environment: ${{ (github.event_name == 'workflow_dispatch' || (contains(toJSON(github.event.pull_request.files.*.filename), '.github/workflows/') && github.event.pull_request.user.login != 'misrasaurabh1' && github.event.pull_request.user.login != 'KRRT7')) && 'external-trusted-contributors' || '' }}
25+
26+
runs-on: ubuntu-latest
27+
env:
28+
CODEFLASH_AIS_SERVER: prod
29+
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}
30+
CODEFLASH_API_KEY: ${{ secrets.CODEFLASH_API_KEY }}
31+
COLUMNS: 110
32+
MAX_RETRIES: 3
33+
RETRY_DELAY: 5
34+
EXPECTED_IMPROVEMENT_PCT: 70
35+
CODEFLASH_END_TO_END: 1
36+
steps:
37+
- name: Checkout
38+
uses: actions/checkout@v4
39+
with:
40+
ref: ${{ github.event.pull_request.head.ref }}
41+
repository: ${{ github.event.pull_request.head.repo.full_name }}
42+
fetch-depth: 0
43+
token: ${{ secrets.GITHUB_TOKEN }}
44+
45+
- name: Validate PR
46+
env:
47+
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
48+
PR_STATE: ${{ github.event.pull_request.state }}
49+
BASE_SHA: ${{ github.event.pull_request.base.sha }}
50+
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
51+
run: |
52+
if git diff --name-only "$BASE_SHA" "$HEAD_SHA" | grep -q "^.github/workflows/"; then
53+
echo "⚠️ Workflow changes detected."
54+
echo "PR Author: $PR_AUTHOR"
55+
if [[ "$PR_AUTHOR" == "misrasaurabh1" || "$PR_AUTHOR" == "KRRT7" ]]; then
56+
echo "✅ Authorized user ($PR_AUTHOR). Proceeding."
57+
elif [[ "$PR_STATE" == "open" ]]; then
58+
echo "✅ PR is open. Proceeding."
59+
else
60+
echo "⛔ Unauthorized user ($PR_AUTHOR) attempting to modify workflows. Exiting."
61+
exit 1
62+
fi
63+
else
64+
echo "✅ No workflow file changes detected. Proceeding."
65+
fi
66+
67+
- name: Set up JDK 11
68+
uses: actions/setup-java@v4
69+
with:
70+
java-version: '11'
71+
distribution: 'temurin'
72+
cache: maven
73+
74+
- name: Set up Python 3.11 for CLI
75+
uses: astral-sh/setup-uv@v6
76+
with:
77+
python-version: 3.11.6
78+
79+
- name: Install dependencies (CLI)
80+
run: uv sync
81+
82+
- name: Build codeflash-runtime JAR
83+
run: |
84+
cd codeflash-java-runtime
85+
mvn clean package -q -DskipTests
86+
mvn install -q -DskipTests
87+
88+
- name: Verify Java installation
89+
run: |
90+
java -version
91+
mvn --version
92+
93+
- name: Remove .git
94+
run: |
95+
if [ -d ".git" ]; then
96+
sudo rm -rf .git
97+
echo ".git directory removed."
98+
else
99+
echo ".git directory does not exist."
100+
exit 1
101+
fi
102+
103+
- name: Run Codeflash to optimize void function
104+
run: |
105+
uv run python tests/scripts/end_to_end_test_java_void_optimization.py
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.example;
2+
3+
public class InPlaceSorter {
4+
5+
public static void bubbleSortInPlace(int[] arr) {
6+
if (arr == null || arr.length <= 1) {
7+
return;
8+
}
9+
10+
int n = arr.length;
11+
for (int i = 0; i < n; i++) {
12+
for (int j = 0; j < n - 1; j++) {
13+
if (arr[j] > arr[j + 1]) {
14+
int temp = arr[j];
15+
arr[j] = arr[j + 1];
16+
arr[j + 1] = temp;
17+
}
18+
}
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.example;
2+
3+
public class InstanceSorter {
4+
5+
public void bubbleSortInPlace(int[] arr) {
6+
if (arr == null || arr.length <= 1) {
7+
return;
8+
}
9+
10+
int n = arr.length;
11+
for (int i = 0; i < n; i++) {
12+
for (int j = 0; j < n - 1; j++) {
13+
if (arr[j] > arr[j + 1]) {
14+
int temp = arr[j];
15+
arr[j] = arr[j + 1];
16+
arr[j + 1] = temp;
17+
}
18+
}
19+
}
20+
}
21+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.example;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
class InPlaceSorterTest {
7+
8+
@Test
9+
void testBubbleSortInPlace() {
10+
int[] arr = {5, 3, 1, 4, 2};
11+
InPlaceSorter.bubbleSortInPlace(arr);
12+
assertArrayEquals(new int[]{1, 2, 3, 4, 5}, arr);
13+
}
14+
15+
@Test
16+
void testBubbleSortInPlaceAlreadySorted() {
17+
int[] arr = {1, 2, 3, 4, 5};
18+
InPlaceSorter.bubbleSortInPlace(arr);
19+
assertArrayEquals(new int[]{1, 2, 3, 4, 5}, arr);
20+
}
21+
22+
@Test
23+
void testBubbleSortInPlaceReversed() {
24+
int[] arr = {5, 4, 3, 2, 1};
25+
InPlaceSorter.bubbleSortInPlace(arr);
26+
assertArrayEquals(new int[]{1, 2, 3, 4, 5}, arr);
27+
}
28+
29+
@Test
30+
void testBubbleSortInPlaceWithDuplicates() {
31+
int[] arr = {3, 2, 4, 1, 3, 2};
32+
InPlaceSorter.bubbleSortInPlace(arr);
33+
assertArrayEquals(new int[]{1, 2, 2, 3, 3, 4}, arr);
34+
}
35+
36+
@Test
37+
void testBubbleSortInPlaceWithNegatives() {
38+
int[] arr = {3, -2, 7, 0, -5};
39+
InPlaceSorter.bubbleSortInPlace(arr);
40+
assertArrayEquals(new int[]{-5, -2, 0, 3, 7}, arr);
41+
}
42+
43+
@Test
44+
void testBubbleSortInPlaceSingleElement() {
45+
int[] arr = {42};
46+
InPlaceSorter.bubbleSortInPlace(arr);
47+
assertArrayEquals(new int[]{42}, arr);
48+
}
49+
50+
@Test
51+
void testBubbleSortInPlaceEmpty() {
52+
int[] arr = {};
53+
InPlaceSorter.bubbleSortInPlace(arr);
54+
assertArrayEquals(new int[]{}, arr);
55+
}
56+
57+
@Test
58+
void testBubbleSortInPlaceNull() {
59+
InPlaceSorter.bubbleSortInPlace(null);
60+
}
61+
62+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.example;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
class InstanceSorterTest {
7+
8+
@Test
9+
void testBubbleSortInPlace() {
10+
InstanceSorter sorter = new InstanceSorter();
11+
int[] arr = {5, 3, 1, 4, 2};
12+
sorter.bubbleSortInPlace(arr);
13+
assertArrayEquals(new int[]{1, 2, 3, 4, 5}, arr);
14+
}
15+
16+
@Test
17+
void testBubbleSortInPlaceAlreadySorted() {
18+
InstanceSorter sorter = new InstanceSorter();
19+
int[] arr = {1, 2, 3, 4, 5};
20+
sorter.bubbleSortInPlace(arr);
21+
assertArrayEquals(new int[]{1, 2, 3, 4, 5}, arr);
22+
}
23+
24+
@Test
25+
void testBubbleSortInPlaceReversed() {
26+
InstanceSorter sorter = new InstanceSorter();
27+
int[] arr = {5, 4, 3, 2, 1};
28+
sorter.bubbleSortInPlace(arr);
29+
assertArrayEquals(new int[]{1, 2, 3, 4, 5}, arr);
30+
}
31+
32+
@Test
33+
void testBubbleSortInPlaceWithDuplicates() {
34+
InstanceSorter sorter = new InstanceSorter();
35+
int[] arr = {3, 2, 4, 1, 3, 2};
36+
sorter.bubbleSortInPlace(arr);
37+
assertArrayEquals(new int[]{1, 2, 2, 3, 3, 4}, arr);
38+
}
39+
40+
@Test
41+
void testBubbleSortInPlaceWithNegatives() {
42+
InstanceSorter sorter = new InstanceSorter();
43+
int[] arr = {3, -2, 7, 0, -5};
44+
sorter.bubbleSortInPlace(arr);
45+
assertArrayEquals(new int[]{-5, -2, 0, 3, 7}, arr);
46+
}
47+
48+
@Test
49+
void testBubbleSortInPlaceSingleElement() {
50+
InstanceSorter sorter = new InstanceSorter();
51+
int[] arr = {42};
52+
sorter.bubbleSortInPlace(arr);
53+
assertArrayEquals(new int[]{42}, arr);
54+
}
55+
56+
@Test
57+
void testBubbleSortInPlaceEmpty() {
58+
InstanceSorter sorter = new InstanceSorter();
59+
int[] arr = {};
60+
sorter.bubbleSortInPlace(arr);
61+
assertArrayEquals(new int[]{}, arr);
62+
}
63+
64+
@Test
65+
void testBubbleSortInPlaceNull() {
66+
InstanceSorter sorter = new InstanceSorter();
67+
sorter.bubbleSortInPlace(null);
68+
}
69+
}

codeflash/languages/java/instrumentation.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,20 +340,30 @@ def wrap_target_calls_with_treesitter(
340340
is_void = target_return_type == "void"
341341
var_name = f"_cf_result{iter_id}_{call_counter}"
342342
receiver = call.get("receiver", "this")
343+
arg_texts: list[str] = call.get("arg_texts", [])
343344
cast_type = _infer_array_cast_type(orig_line)
344345
if not cast_type and target_return_type and not is_void:
345346
cast_type = target_return_type
346347
var_with_cast = f"({cast_type}){var_name}" if cast_type else var_name
347348

348349
if is_void:
349350
bare_call_stmt = f"{call['full_call']};"
351+
# For void methods, serialize the post-call state to capture side effects.
352+
# For instance methods (receiver is a variable), serialize the receiver.
353+
# For static methods (receiver is a class name), serialize the arguments
354+
# since the class name itself is not a value and can't be cast to Object.
355+
is_static_call = receiver != "this" and receiver[:1].isupper()
356+
if is_static_call and arg_texts:
357+
serialize_target = f"new Object[]{{{', '.join(arg_texts)}}}"
358+
else:
359+
serialize_target = f"(Object) {receiver}"
350360
if precise_call_timing:
351-
serialize_stmt = f"_cf_serializedResult{iter_id}_{call_counter} = com.codeflash.Serializer.serialize((Object) {receiver});"
361+
serialize_stmt = f"_cf_serializedResult{iter_id}_{call_counter} = com.codeflash.Serializer.serialize({serialize_target});"
352362
start_stmt = f"_cf_start{iter_id}_{call_counter} = System.nanoTime();"
353363
end_stmt = f"_cf_end{iter_id}_{call_counter} = System.nanoTime();"
354364
else:
355365
serialize_stmt = (
356-
f"_cf_serializedResult{iter_id} = com.codeflash.Serializer.serialize((Object) {receiver});"
366+
f"_cf_serializedResult{iter_id} = com.codeflash.Serializer.serialize({serialize_target});"
357367
)
358368
start_stmt = f"_cf_start{iter_id} = System.nanoTime();"
359369
end_stmt = f"_cf_end{iter_id} = System.nanoTime();"
@@ -493,6 +503,13 @@ def _collect_calls(
493503
es_end = parent.end_byte - prefix_len
494504
object_node = node.child_by_field_name("object")
495505
receiver = analyzer.get_node_text(object_node, wrapper_bytes) if object_node else "this"
506+
# Extract argument texts for void method serialization
507+
args_node = node.child_by_field_name("arguments")
508+
arg_texts: list[str] = []
509+
if args_node:
510+
for child in args_node.children:
511+
if child.type not in ("(", ")", ","):
512+
arg_texts.append(analyzer.get_node_text(child, wrapper_bytes))
496513
out.append(
497514
{
498515
"start_byte": start,
@@ -504,6 +521,7 @@ def _collect_calls(
504521
"es_start_byte": es_start,
505522
"es_end_byte": es_end,
506523
"receiver": receiver,
524+
"arg_texts": arg_texts,
507525
}
508526
)
509527
for child in node.children:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os
2+
import pathlib
3+
4+
from end_to_end_test_utilities import TestConfig, run_codeflash_command, run_with_retries
5+
6+
7+
def run_test(expected_improvement_pct: int) -> bool:
8+
config = TestConfig(
9+
file_path="src/main/java/com/example/InPlaceSorter.java",
10+
function_name="bubbleSortInPlace",
11+
min_improvement_x=0.70,
12+
)
13+
cwd = (pathlib.Path(__file__).parent.parent.parent / "code_to_optimize" / "java").resolve()
14+
return run_codeflash_command(cwd, config, expected_improvement_pct)
15+
16+
17+
if __name__ == "__main__":
18+
exit(run_with_retries(run_test, int(os.getenv("EXPECTED_IMPROVEMENT_PCT", 70))))

0 commit comments

Comments
 (0)