Skip to content

fix conflit for 2145 #2148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed

Conversation

peter-jerry-ye
Copy link
Collaborator

Try to solve conflicts for #2145

@Asterless See if this branch is what you want

Copy link

Potential memory leak in intersection implementation for Branch nodes

Category
Correctness
Code Snippet
Branch(sa1.intersection(sa2, fn(m1, m2) { m1.intersection(m2) }))
Recommendation
Add a size check and return Empty for empty results like in HashSet implementation:

let res = sa1.intersection(sa2, fn(m1, m2) { m1.intersection(m2) })
if res.size() == 0 { Empty } else { Branch(res) }```
**Reasoning**
The HashSet implementation includes this optimization to avoid creating unnecessary Branch nodes for empty intersections. The HashMap should follow the same pattern for consistency and memory efficiency.

</details>
<details>

<summary> Duplicated folding pattern in set operations </summary>

**Category**
Maintainability
**Code Snippet**
.fold(init=Empty, fn(m, kv) {
  match other.get(kv.0) {
    Some(v2) => m.add(kv.0, f(kv.0, kv.1, v2))
    None => m
  }
})
**Recommendation**
Extract common folding pattern into a helper function:
```moonbit
fn fold_matching[K,V,R](self: T[K,V], other: T[K,V], init: R, f: (R,K,V,V) -> R) -> R```
**Reasoning**
Similar folding patterns appear in union_with, intersection_with and difference implementations. Extracting to a helper would reduce code duplication and make the operations easier to maintain.

</details>
<details>

<summary> Unnecessary traversal in difference operation </summary>

**Category**
Performance
**Code Snippet**
difference[K : Eq + Hash](self : T[K, V], other : T[K, V]) -> T[K, V]
**Recommendation**
For Branch case, compute difference directly on matching subtrees:
```moonbit
Branch(sa1.difference(sa2, fn(m1, m2) { m1.difference(m2) }))```
**Reasoning**
The current implementation falls back to full iteration for Branch nodes instead of operating directly on matching subtrees, which is less efficient for large maps with similar structure.

</details>

@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 6859

Details

  • 40 of 81 (49.38%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.5%) to 90.763%

Changes Missing Coverage Covered Lines Changed/Added Lines %
immut/internal/sparse_array/sparse_array.mbt 5 6 83.33%
immut/hashset/HAMT.mbt 7 22 31.82%
immut/hashmap/HAMT.mbt 28 53 52.83%
Totals Coverage Status
Change from base Build 6851: -0.5%
Covered Lines: 6377
Relevant Lines: 7026

💛 - Coveralls

@Lampese Lampese closed this May 22, 2025
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