Skip to content

Add blit_to() to @ArrayView #2260

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

Conversation

Asterless
Copy link
Contributor

Summary

New Method Addition:

  • array/view.mbt: Implemented the blit_to method with logic for iterating over the source ArrayView and copying its elements to the destination array.

Testing Enhancements:

  • array/view_test.mbt: Added multiple test cases for the blit_to method, covering scenarios such as basic usage, copying at the start or end of the destination array, handling empty source arrays, and full overwrites of the destination array.

Verified

This commit was signed with the committer’s verified signature.
krzys-h Krzysztof Haładyn (krzys_h)
…tion array with tests
Copy link

peter-jerry-ye-code-review bot commented Jun 12, 2025

Complex nested conditional logic makes the copy length calculation error-prone and hard to verify

Category
Correctness
Code Snippet
let copy_len = if src_len < dst_len {
if src_len < len {
src_len
} else {
len
}
} else if dst_len < len {
dst_len
} else {
len
}
Recommendation
Use a simpler approach with built-in min function: let copy_len = min(len, min(src_len, dst_len))
Reasoning
The current nested conditionals are difficult to reason about and verify correctness. Using min() makes the intent clear: copy at most len elements, but no more than what's available in source or fits in destination.

Missing bounds validation could lead to out-of-bounds access

Category
Correctness
Code Snippet
let src_len = self.length() - src_offset
let dst_len = dst.length() - dst_offset
Recommendation
Add validation: if src_offset < 0 || src_offset > self.length() { panic("src_offset out of bounds") } if dst_offset < 0 || dst_offset > dst.length() { panic("dst_offset out of bounds") }
Reasoning
If offsets are negative or exceed array bounds, the subtraction could result in negative lengths or incorrect calculations, leading to potential runtime errors or memory safety issues.

Inconsistent parameter naming and documentation mismatch

Category
Maintainability
Code Snippet
pub fn[T] ArrayView::blit_to(
self : ArrayView[T],
dst : ArrayView[T],
len~ : Int,
src_offset~ : Int = 0,
dst_offset~ : Int = 0
) -> Unit
Recommendation
Update documentation to match actual signature: /// Copies up to len elements from this ArrayView to a destination ArrayView starting at specified offsets. and consider renaming dst to dst_view for clarity
Reasoning
The documentation says 'destination array' but the parameter is actually an ArrayView. Clear parameter names and accurate documentation improve code maintainability and prevent confusion.

@coveralls
Copy link
Collaborator

coveralls commented Jun 12, 2025

Pull Request Test Coverage Report for Build 432

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 90.113%

Totals Coverage Status
Change from base Build 421: 0.0%
Covered Lines: 3518
Relevant Lines: 3904

💛 - Coveralls

@peter-jerry-ye peter-jerry-ye requested a review from Yu-zh June 13, 2025 10:08
Asterless and others added 4 commits June 13, 2025 23:15
…tion array with tests
@peter-jerry-ye peter-jerry-ye force-pushed the Feat/@ArrayView-blit_to() branch from 2a4cc08 to 876fad5 Compare June 20, 2025 02:03
Asterless and others added 4 commits June 20, 2025 18:40
…ess/core into Feat/@ArrayView-blit_to()
Asterless and others added 4 commits June 23, 2025 17:41
Asterless and others added 2 commits July 2, 2025 01:06
… logic
@FlyCloudC
Copy link
Contributor

The bilt_to function is expected to be more efficient than a for loop copy. However, the current implementation creates two new arrays and performs four copies, introducing additional space and time overhead. A more efficient approach might be to implement it as a direct bilt_to from self.buf to dst.buf.

Asterless and others added 3 commits July 14, 2025 18:49
@Asterless
Copy link
Contributor Author

Asterless commented Jul 17, 2025

The bilt_to function is expected to be more efficient than a for loop copy. However, the current implementation creates two new arrays and performs four copies, introducing additional space and time overhead. A more efficient approach might be to implement it as a direct bilt_to from self.buf to dst.buf.

Ok, now I use UninitializedArray::unsafe_blit to achieve efficient blit_to method😊👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request] add ArrayView::bilt_to
4 participants