Skip to content

fix(#4095): synchronized replaced with lock in Heaps.java#4924

Merged
yegor256 merged 1 commit intoobjectionary:masterfrom
eshabakhov:#4905
Mar 11, 2026
Merged

fix(#4095): synchronized replaced with lock in Heaps.java#4924
yegor256 merged 1 commit intoobjectionary:masterfrom
eshabakhov:#4905

Conversation

@eshabakhov
Copy link

@eshabakhov eshabakhov commented Mar 11, 2026

Summary

This PR solves #4905

Changes

  • synchronized statements replaced with lock in Heaps.java

Test

Run the following commands to verify:

# Compile and run tests
mvn clean install -pl eo-runtime -am

Summary by CodeRabbit

  • Chores
    • Optimized internal synchronization mechanism for memory management operations to improve efficiency and reliability.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 11, 2026

📝 Walkthrough

Walkthrough

The Heaps class synchronization mechanism was refactored to use ReentrantLock instead of synchronized blocks for guarding the blocks map. All memory operations—malloc, size, resize, read, write, and free—now use explicit lock/unlock patterns with try/finally blocks while maintaining the same public API.

Changes

Cohort / File(s) Summary
Synchronization Refactoring
eo-runtime/src/main/java/EOorg/EOeolang/Heaps.java
Replaced synchronized blocks with ReentrantLock for thread-safe access to the blocks map across all memory operations (malloc, size, resize, read, write, free). Added private Lock field initialization and explicit lock/unlock calls in try/finally patterns.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A lock of a different kind now guards the heap so tight,
ReentrantLock replaces synchronized in earnest flight,
Each try and finally, a careful paw-step dance,
Memory operations shielded—no thread takes a chance! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title accurately describes the main change: replacing synchronized blocks with ReentrantLock in Heaps.java, matching the primary modification in the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Contributor

🚀 Performance Analysis

All benchmarks are within the acceptable range. No critical degradation detected (threshold is 100%). Please refer to the detailed report for more information.

Click to see the detailed report
Test Base Score PR Score Change % Change Unit Mode
benchmarks.XmirBench.xmirToEO 206.505 216.043 9.538 4.62% ms/op Average Time

⚠️ Performance loss: benchmarks.XmirBench.xmirToEO is slower by 9.538 ms/op (4.62%)

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
eo-runtime/src/main/java/EOorg/EOeolang/Heaps.java (1)

34-47: Consider replacing ConcurrentHashMap with HashMap.

Since all access to blocks is now protected by the ReentrantLock, the internal synchronization of ConcurrentHashMap is redundant. A plain HashMap would provide the same thread-safety guarantees with less overhead.

♻️ Proposed simplification
-import java.util.concurrent.ConcurrentHashMap;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
-    `@SuppressWarnings`("PMD.LooseCoupling")
-    private final ConcurrentHashMap<Integer, byte[]> blocks;
+    private final Map<Integer, byte[]> blocks;
-        this.blocks = new ConcurrentHashMap<>(0);
+        this.blocks = new HashMap<>();
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@eo-runtime/src/main/java/EOorg/EOeolang/Heaps.java` around lines 34 - 47, The
field blocks in class Heaps is using ConcurrentHashMap despite all access being
guarded by the ReentrantLock; change the declaration and initialization of
blocks from ConcurrentHashMap<Integer, byte[]> to plain HashMap<Integer, byte[]>
(update the type on the field and the constructor initialization in Heaps()),
remove any unnecessary concurrency imports (ConcurrentHashMap) and keep the
existing lock usage in methods that access blocks to preserve thread-safety;
ensure any `@SuppressWarnings` remains appropriate or remove it if no longer
needed.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@eo-runtime/src/main/java/EOorg/EOeolang/Heaps.java`:
- Around line 34-47: The field blocks in class Heaps is using ConcurrentHashMap
despite all access being guarded by the ReentrantLock; change the declaration
and initialization of blocks from ConcurrentHashMap<Integer, byte[]> to plain
HashMap<Integer, byte[]> (update the type on the field and the constructor
initialization in Heaps()), remove any unnecessary concurrency imports
(ConcurrentHashMap) and keep the existing lock usage in methods that access
blocks to preserve thread-safety; ensure any `@SuppressWarnings` remains
appropriate or remove it if no longer needed.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e0482e23-1f47-43f1-9611-50ed82e62460

📥 Commits

Reviewing files that changed from the base of the PR and between 34fddf4 and 7998ab7.

📒 Files selected for processing (1)
  • eo-runtime/src/main/java/EOorg/EOeolang/Heaps.java

@eshabakhov eshabakhov changed the title #4095: synchronized replaced with lock in Heaps.java fix(#4095): synchronized replaced with lock in Heaps.java Mar 11, 2026
@eshabakhov
Copy link
Author

@yegor256 could you check this PR?

@yegor256 yegor256 merged commit 4b059d1 into objectionary:master Mar 11, 2026
26 checks passed
@yegor256
Copy link
Member

@eshabakhov thanks!

@yegor256
Copy link
Member

@rultor release, tag is 0.59.6

@yegor256
Copy link
Member

@rultor status

@rultor
Copy link
Contributor

rultor commented Mar 23, 2026

@rultor release, tag is 0.59.6

@yegor256 OK, I will release it now. Please check the progress here.

@rultor
Copy link
Contributor

rultor commented Mar 23, 2026

@rultor status

@yegor256 This is what's going on here:

  • request 4106437271 is in processing, command is release
  • request has 3 parameter(s):
  • head: git@github.com:objectionary/eo.git
  • head_branch: master
  • tag: 0.59.6
  • build is not running

You can get more information about Rultor commands here.

@rultor
Copy link
Contributor

rultor commented Mar 23, 2026

@rultor release, tag is 0.59.6

@yegor256 Done! FYI, the full log is here (took me 13min).

@0crat
Copy link

0crat commented Mar 24, 2026

@eshabakhov Thanks for the contribution! You've earned +16 points following our bonus policy - you received the base 16 points for having reviews, plus additional points for your code contribution size, with appropriate adjustments applied. Please, keep them coming! Your running score is +16; don't forget to check your Zerocracy account too.

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.

4 participants