Skip to content

Commit

Permalink
Expose external constructor for byte arrays. (#2579)
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch authored Oct 16, 2024
1 parent 526e165 commit 59e1540
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/bytes.toit
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,14 @@ class Buffer extends BufferConsumer:
// We copy the code of the 'with-initial-size' constructor, so we don't get
// a deprecation warning.
init-size_ = INITIAL-BUFFER-LENGTH_
buffer_ = init-size_ > MAX-INTERNAL-SIZE_ ? (ByteArray_.external_ init-size_) : (ByteArray init-size_)
buffer_ = init-size_ > MAX-INTERNAL-SIZE_ ? (ByteArray.external init-size_) : (ByteArray init-size_)

/**
Constructs a new buffer with the given $init-size_.
If the $init-size_ isn't big enough, the buffer grows when necessary.
*/
constructor.with-initial-size .init-size_/int:
buffer_ = init-size_ > MAX-INTERNAL-SIZE_ ? (ByteArray_.external_ init-size_) : (ByteArray init-size_)
buffer_ = init-size_ > MAX-INTERNAL-SIZE_ ? (ByteArray.external init-size_) : (ByteArray init-size_)

/** See $BufferConsumer.size. */
size -> int:
Expand Down
22 changes: 22 additions & 0 deletions lib/core/collections.toit
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,27 @@ interface ByteArray extends io.Data:
bytes.write-to-byte-array result --at=0 from to
return result

/**
Constructs a byte array where the data is not on the Toit heap.
The byte array's backing store is allocated using 'malloc' and is thus
not located on the Toit heap. This has the following consequences:
- The garbage collector can't move the data, which can lead to fragmentation.
- External byte arrays can be handed over to the system. The system would
then "neuter" the byte array, rendering it unusable in Toit. This can
be useful for performance reasons, as can sometimes avoid copying the data.
Only few functions neuter byte arrays and typically only on request.
External byte arrays are not automatically faster than normal byte arrays.
Unless you know what you are doing or have a specific use-case in mind,
you should use normal byte arrays.
Note: bigger byte arrays are always external, even if allocated using
the normal $(constructor size).
*/
constructor.external size/int:
#primitive.core.byte-array-new-external

/**
The number of bytes in this instance.
*/
Expand Down Expand Up @@ -1643,6 +1664,7 @@ class ByteArray_ extends ByteArrayBase_:
constructor size/int --filler/int=0:
#primitive.core.byte-array-new

/** Deprecated. Use $(ByteArray.external size) instead. */
constructor.external_ size/int:
#primitive.core.byte-array-new-external

Expand Down
2 changes: 1 addition & 1 deletion tests/esp32/ble3-shared.toit
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ main-central:
task::
i := 0
while not done:
ba := ByteArray_.external_ 100
ba := ByteArray.external 100
keep-alive[i % keep-alive.size] = ba
ByteArray 10
yield
Expand Down
2 changes: 1 addition & 1 deletion tests/serialization-test.toit
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ main:
class Unserializable:

test-throwing-process-send:
l := List 10: ByteArray_.external_ 100
l := List 10: ByteArray.external 100
expect-throw "TOO_MANY_EXTERNALS": process-send_ 0 0 l
expect-not (process-send_ 100000000 -10 #[])
l = []
Expand Down

0 comments on commit 59e1540

Please sign in to comment.