Skip to content

Commit

Permalink
Avoid copying Arrays
Browse files Browse the repository at this point in the history
One last request to avoid copying Arrays when the underlying storage is
already an Array.
  • Loading branch information
hubertp committed Aug 22, 2022
1 parent 0fd1217 commit 934e642
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
10 changes: 9 additions & 1 deletion distribution/lib/Standard/Base/0.0.0-dev/src/Data/Vector.enso
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,15 @@ type Vector
type Vector storage

to_array self =
self.storage.to_array
arr = self.storage.to_array
case arr of
Array ->
arr
_ ->
len = self.storage.length
a = Array.new len
Array.copy arr 0 a 0 len
a

## Returns the number of elements stored in this vector.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,4 @@ type Proxy_Polyglot_Array

to_array : Array Any
to_array self =
len = self.length
a = Array.new len
0.up_to len . each ix-> a.set_at ix (self.at ix)
a
self.arr
5 changes: 5 additions & 0 deletions test/Tests/src/Semantic/Js_Interop_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ foreign js make_object = """
foreign js make_array = """
return [{ x: 10}, {x: 20}, {x: 30}];

foreign js make_simple_array = """
return [10, 20, 30];

foreign js make_str str = """
return "foo " + str + " bar"

Expand Down Expand Up @@ -95,6 +98,8 @@ spec = Test.group "Polyglot JS" <|
Test.specify "should expose array interfaces for JS arrays" <|
vec = Vector.from_polyglot_array make_array
vec.map .x . should_equal [10, 20, 30]
vec2 = Vector.from_polyglot_array make_simple_array
vec2.to_array.at 0 . should_equal 10

Test.specify "should correctly marshall strings" <|
str = make_str "x" + " baz"
Expand Down

0 comments on commit 934e642

Please sign in to comment.