Skip to content

Commit 030cad1

Browse files
feat: make copy faster
1 parent ece680d commit 030cad1

File tree

6 files changed

+56
-58
lines changed

6 files changed

+56
-58
lines changed

array/array_js.mbt

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515
///|
1616
priv type JSArray
1717

18-
///|
19-
fn[T] JSArray::ofAnyArray(array : Array[T]) -> JSArray = "%identity"
20-
21-
///|
22-
fn[T] JSArray::toAnyArray(self : JSArray) -> Array[T] = "%identity"
23-
2418
///|
2519
fn[T] JSArray::ofAnyFixedArray(array : FixedArray[T]) -> JSArray = "%identity"
2620

@@ -30,25 +24,3 @@ fn[T] JSArray::toAnyFixedArray(self : JSArray) -> FixedArray[T] = "%identity"
3024
///|
3125
extern "js" fn JSArray::copy(self : JSArray) -> JSArray =
3226
#|(arr) => arr.slice(0)
33-
34-
///|
35-
/// Creates and returns a new array with a copy of all elements from the input
36-
/// array.
37-
///
38-
/// Parameters:
39-
///
40-
/// * `array` : The array to be copied.
41-
///
42-
/// Returns a new array containing all elements from the original array.
43-
///
44-
/// Example:
45-
///
46-
/// ```moonbit
47-
/// let original = [1, 2, 3]
48-
/// let copied = original.copy()
49-
/// inspect(copied, content="[1, 2, 3]")
50-
/// inspect(physical_equal(original, copied), content="false")
51-
/// ```
52-
pub fn[T] copy(self : Array[T]) -> Array[T] {
53-
JSArray::ofAnyArray(self).copy().toAnyArray()
54-
}

array/array_nonjs.mbt

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,3 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
15-
///|
16-
/// Creates and returns a new array with a copy of all elements from the input
17-
/// array.
18-
///
19-
/// Parameters:
20-
///
21-
/// * `array` : The array to be copied.
22-
///
23-
/// Returns a new array containing all elements from the original array.
24-
///
25-
/// Example:
26-
///
27-
/// ```moonbit
28-
/// let original = [1, 2, 3]
29-
/// let copied = original.copy()
30-
/// inspect(copied, content="[1, 2, 3]")
31-
/// inspect(physical_equal(original, copied), content="false")
32-
/// ```
33-
pub fn[T] copy(self : Array[T]) -> Array[T] {
34-
let len = self.length()
35-
if len == 0 {
36-
[]
37-
} else {
38-
let arr = Array::make(len, self[0])
39-
Array::unsafe_blit(arr, 0, self, 0, len)
40-
arr
41-
}
42-
}

array/pkg.generated.mbti

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ impl[T : Eq] Eq for FixedArray[T]
4949
impl[T : Hash] Hash for FixedArray[T]
5050
impl[X : @quickcheck.Arbitrary] @quickcheck.Arbitrary for FixedArray[X]
5151

52-
fn[T] Array::copy(Self[T]) -> Self[T]
5352
fn[A, B] Array::filter_map(Self[A], (A) -> B? raise?) -> Self[B] raise?
5453
fn[T] Array::from_iter(Iter[T]) -> Self[T]
5554
fn[A : @string.ToStringView] Array::join(Self[A], StringView) -> String

builtin/arraycore_js.mbt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,29 @@ pub fn[A] Array::fill(
358358
}
359359
JSArray::ofAnyArray(self).fill(JSValue::ofAny(value), start, end)
360360
}
361+
362+
///|
363+
extern "js" fn JSArray::copy(self : JSArray) -> JSArray =
364+
#|(arr) => arr.slice(0)
365+
366+
///|
367+
/// Creates and returns a new array with a copy of all elements from the input
368+
/// array.
369+
///
370+
/// Parameters:
371+
///
372+
/// * `array` : The array to be copied.
373+
///
374+
/// Returns a new array containing all elements from the original array.
375+
///
376+
/// Example:
377+
///
378+
/// ```moonbit
379+
/// let original = [1, 2, 3]
380+
/// let copied = original.copy()
381+
/// inspect(copied, content="[1, 2, 3]")
382+
/// inspect(physical_equal(original, copied), content="false")
383+
/// ```
384+
pub fn[T] copy(self : Array[T]) -> Array[T] {
385+
JSArray::ofAnyArray(self).copy().toAnyArray()
386+
}

builtin/arraycore_nonjs.mbt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,32 @@ pub fn[A] Array::fill(
462462
}
463463
self.buf.unchecked_fill(start, value, length - start)
464464
}
465+
466+
///|
467+
/// Creates and returns a new array with a copy of all elements from the input
468+
/// array.
469+
///
470+
/// Parameters:
471+
///
472+
/// * `array` : The array to be copied.
473+
///
474+
/// Returns a new array containing all elements from the original array.
475+
///
476+
/// Example:
477+
///
478+
/// ```moonbit
479+
/// let original = [1, 2, 3]
480+
/// let copied = original.copy()
481+
/// inspect(copied, content="[1, 2, 3]")
482+
/// inspect(physical_equal(original, copied), content="false")
483+
/// ```
484+
pub fn[T] copy(self : Array[T]) -> Array[T] {
485+
let len = self.length()
486+
if len == 0 {
487+
[]
488+
} else {
489+
let arr = Array::make_uninit(len)
490+
Array::unsafe_blit(arr, 0, self, 0, len)
491+
arr
492+
}
493+
}

builtin/pkg.generated.mbti

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ fn[T] Array::chunk_by(Self[T], (T, T) -> Bool raise?) -> Self[Self[T]] raise?
7979
fn[T] Array::chunks(Self[T], Int) -> Self[Self[T]]
8080
fn[T] Array::clear(Self[T]) -> Unit
8181
fn[T : Eq] Array::contains(Self[T], T) -> Bool
82+
fn[T] Array::copy(Self[T]) -> Self[T]
8283
fn[T : Eq] Array::dedup(Self[T]) -> Unit
8384
fn[T] Array::drain(Self[T], Int, Int) -> Self[T]
8485
fn[T] Array::each(Self[T], (T) -> Unit raise?) -> Unit raise?

0 commit comments

Comments
 (0)