- 
                Notifications
    You must be signed in to change notification settings 
- Fork 142
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] { | ||||||
| 
     | ||||||
| pub fn[T] copy(self : Array[T]) -> Array[T] { | |
| pub fn[T] Array::copy(self : Array[T]) -> Array[T] { | 
| 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
    
   
     | ||||||||||||||||||||
| /// Creates and returns a new array with a copy of all elements from the input | |
| /// array. | |
| /// | |
| /// Parameters: | |
| /// | |
| /// * `array` : The array to be copied. | |
| /// | |
| /// Copies and returns a new array containing all elements from `self`. | |
| /// | 
    
      
    
      Copilot
AI
    
    
    
      Sep 21, 2025 
    
  
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.
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] {| pub fn[T] copy(self : Array[T]) -> Array[T] { | |
| pub fn[T] Array::copy(self : Array[T]) -> Array[T] { | 
    
      
    
      Copilot
AI
    
    
    
      Sep 21, 2025 
    
  
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.
[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.
| let arr = Array::make_uninit(len) | |
| let arr = Array::make_uninit(len) | |
| // Safety invariants for unsafe_blit: | |
| // - len == self.length() | |
| // - arr is freshly allocated, so no overlap with self | |
| // - arr will be fully initialized by this blit | 
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).