Skip to content

ArrayView::join and FixedArray::join #2125

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 9 commits into
base: main
Choose a base branch
from

Conversation

ethereal-dream
Copy link

Adding ArrayView::join and FixedArray::join functions makes it convenient to concatenate strings within an array into a single complete string.

…ient to concatenate strings within an array into a single complete string.
Copy link

peter-jerry-ye-code-review bot commented May 18, 2025

ArrayView::join creates unnecessary intermediate array

Category
Performance
Code Snippet
pub fn View::join(self : ArrayView[String], separator : String) -> String {
let array = self.to_array()
@string.concat(array, separator~)
}
Recommendation
Implement direct string concatenation similar to FixedArray::join to avoid intermediate array allocation
Reasoning
Creating a new array just to join strings is inefficient as it requires additional memory allocation and copying. The implementation could directly iterate over the view and build the result string, similar to how FixedArray::join does it.

Inconsistent parameter types between FixedArray::join and ArrayView::join

Category
Maintainability
Code Snippet
fn FixedArray::join(self : FixedArray[String], separator : @string.View)
fn View::join(self : ArrayView[String], separator : String)
Recommendation
Use consistent parameter types for separator across both implementations, preferably @string.View for better performance
Reasoning
Using different types for the same parameter in similar functions makes the API less predictable and harder to use. @string.View is likely more efficient as it avoids copying strings.

FixedArray::join could be simplified by extracting the string building logic

Category
Maintainability
Code Snippet
pub fn FixedArray::join(...) {
// ... complex string building logic ...
}
Recommendation
Extract the string building logic into a separate private function that can be shared between different join implementations
Reasoning
The string building logic is relatively complex and could be reused. Extracting it would make the code more maintainable and could enable sharing the implementation with ArrayView::join.

@coveralls
Copy link
Collaborator

coveralls commented May 18, 2025

Pull Request Test Coverage Report for Build 6808

Details

  • 0 of 10 (0.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.1%) to 92.865%

Changes Missing Coverage Covered Lines Changed/Added Lines %
array/view.mbt 0 1 0.0%
array/fixedarray.mbt 0 9 0.0%
Totals Coverage Status
Change from base Build 6807: -0.1%
Covered Lines: 6221
Relevant Lines: 6699

💛 - Coveralls

@peter-jerry-ye
Copy link
Collaborator

@Yu-zh asked whether we should be using View instead of String.

I think maybe we can use @string.View for separator, but keep T[String] for this signature.

@ethereal-dream
Copy link
Author

When using @string.View for separator, what should I do with string.write_string(separator) since there doesn't seem to be a write_view function?

@peter-jerry-ye peter-jerry-ye linked an issue May 22, 2025 that may be closed by this pull request
@peter-jerry-ye
Copy link
Collaborator

I think you can simply call to_string on the view. The support for view on the string builder would come later as its currently blocked by how the core package is organize, which I assume would still need a few weeks.

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.

[Feature Request] ArrayView::join and FixedArray::join
4 participants