Skip to content

perf: use array as buffer #2030

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

illusory0x0
Copy link
Contributor

Copy link

Improved performance by avoiding double traversal

Category
Performance
Code Snippet
// Old implementation
self.rev().fold(init~, f)

// New implementation
let xs = self.to_array()
let mut acc = init
for x in xs.rev_iter() {
acc = f(acc, x)
}
Recommendation
The new implementation is better as it avoids creating an intermediate reversed list
Reasoning
The original implementation had to traverse the list twice - once for reversal and once for folding. The new implementation only needs one traversal after converting to an array, which is more efficient.

Function argument order inconsistency between implementations

Category
Correctness
Code Snippet
// immut/list/list.mbt
acc = f(x, acc)

// list/list.mbt
acc = f(acc, x)
Recommendation
The argument order should be consistent across both implementations. Based on the function signature f : (B, A) -> B, the mutable list implementation is correct while the immutable one needs to swap the arguments.
Reasoning
The function signature specifies f : (B, A) -> B, meaning the accumulator (type B) should be the first argument and the element (type A) should be the second. The immutable implementation has these swapped which could cause bugs.

Similar code duplicated across modules

Category
Maintainability
Code Snippet
// Both modules have nearly identical implementations:
let xs = self.to_array()
let mut acc = init
for x in xs.rev_iter() {
acc = f(...)
}
Recommendation
Consider extracting the common implementation into a helper function or trait that can be shared between both list types
Reasoning
Having duplicate implementations increases maintenance burden and the risk of inconsistencies. A shared implementation would ensure both list types behave identically and make future changes easier.

@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 6493

Details

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.002%) to 92.695%

Totals Coverage Status
Change from base Build 6492: -0.002%
Covered Lines: 6116
Relevant Lines: 6598

💛 - Coveralls

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.

2 participants