-
Notifications
You must be signed in to change notification settings - Fork 141
feat: make copy faster #2721
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
base: main
Are you sure you want to change the base?
feat: make copy faster #2721
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -358,3 +358,29 @@ pub fn[A] Array::fill( | |||||
} | ||||||
JSArray::ofAnyArray(self).fill(JSValue::ofAny(value), start, end) | ||||||
} | ||||||
|
||||||
///| | ||||||
extern "js" fn JSArray::copy(self : JSArray) -> JSArray = | ||||||
#|(arr) => arr.slice(0) | ||||||
|
||||||
///| | ||||||
/// Creates and returns a new array with a copy of all elements from the input | ||||||
/// array. | ||||||
/// | ||||||
/// Parameters: | ||||||
/// | ||||||
/// * `array` : The array to be copied. | ||||||
/// | ||||||
/// Returns a new array containing all elements from the original array. | ||||||
/// | ||||||
/// Example: | ||||||
/// | ||||||
/// ```moonbit | ||||||
/// let original = [1, 2, 3] | ||||||
/// let copied = original.copy() | ||||||
/// inspect(copied, content="[1, 2, 3]") | ||||||
/// inspect(physical_equal(original, copied), content="false") | ||||||
/// ``` | ||||||
pub fn[T] copy(self : Array[T]) -> Array[T] { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issue as the non-JS implementation: the method should be namespaced as Array::copy to match the API surface and the style used elsewhere in this file. Update the signature to: 384 |pub fn[T] Array::copy(self : Array[T]) -> Array[T] {
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
JSArray::ofAnyArray(self).copy().toAnyArray() | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -462,3 +462,32 @@ pub fn[A] Array::fill( | |||||||||||||||||||
} | ||||||||||||||||||||
self.buf.unchecked_fill(start, value, length - start) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
///| | ||||||||||||||||||||
/// Creates and returns a new array with a copy of all elements from the input | ||||||||||||||||||||
/// array. | ||||||||||||||||||||
/// | ||||||||||||||||||||
/// Parameters: | ||||||||||||||||||||
/// | ||||||||||||||||||||
/// * `array` : The array to be copied. | ||||||||||||||||||||
/// | ||||||||||||||||||||
Comment on lines
+467
to
+473
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The documentation refers to a parameter named array, but the function uses a receiver self and has no such parameter. Please update the docs to reflect the receiver-based API (for example, remove the Parameters section or replace it with “Copies and returns a new Array from self.”).
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||
/// Returns a new array containing all elements from the original array. | ||||||||||||||||||||
/// | ||||||||||||||||||||
/// Example: | ||||||||||||||||||||
/// | ||||||||||||||||||||
/// ```moonbit | ||||||||||||||||||||
/// let original = [1, 2, 3] | ||||||||||||||||||||
/// let copied = original.copy() | ||||||||||||||||||||
/// inspect(copied, content="[1, 2, 3]") | ||||||||||||||||||||
/// inspect(physical_equal(original, copied), content="false") | ||||||||||||||||||||
/// ``` | ||||||||||||||||||||
pub fn[T] copy(self : Array[T]) -> Array[T] { | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method is declared without the Array:: qualifier, which is inconsistent with other methods in this file (for example, Array::fill) and with the new signature exposed in builtin/pkg.generated.mbti. Define it as a method on Array to match the declared API: change the signature to: 484 |pub fn[T] Array::copy(self : Array[T]) -> Array[T] {
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||
let len = self.length() | ||||||||||||||||||||
if len == 0 { | ||||||||||||||||||||
[] | ||||||||||||||||||||
} else { | ||||||||||||||||||||
let arr = Array::make_uninit(len) | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Since make_uninit is used, a brief comment would help future readers understand the safety invariants for unsafe_blit (that len == self.length(), no overlap, and arr becomes fully initialized). Suggest adding a short comment above this call clarifying these guarantees.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||
Array::unsafe_blit(arr, 0, self, 0, len) | ||||||||||||||||||||
arr | ||||||||||||||||||||
} | ||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation mentions a parameter array, but the function is a method on Array taking self; there is no such parameter. Please align the docs with the receiver-based API (e.g., remove the Parameters section or refer to self).
Copilot uses AI. Check for mistakes.