Skip to content

feat(pass-style,marshal): ByteArray, a new binary Passable type#1538

Merged
erights merged 1 commit intomasterfrom
markm-binary
May 1, 2025
Merged

feat(pass-style,marshal): ByteArray, a new binary Passable type#1538
erights merged 1 commit intomasterfrom
markm-binary

Conversation

@erights
Copy link
Copy Markdown
Contributor

@erights erights commented Apr 4, 2023

Closes: #1331
Refs: ocapn/ocapn#5 (comment) #2414

Description

Recognize Immutable ArrayBuffers, when also frozen, as the new pass-style byteArray. Add a branch for each of the dispatches on pass-style that must now understand byte-arrays as well. As of this PR, many of those additional branches are stubbed out with "not yet implemented" errors as placeholders.

Security Considerations

The underlying shim implementation, if we're still using that, uses a normal mutable ArrayBuffer internally, but safely encapsulates it so nothing outside the shim can mutate it.

Scaling Considerations

The underlying shim implementation helps us handle bulk binary data in a largely no-copy or minimal-copy manner.

Documentation Considerations

Will add a new pass-style, so everywhere we enumerate all the pass-styles, we'll need to add byte-array.

Testing Considerations

  • TODO tests. For each stubbed out case, there instead should be a test.failing test.

Compatibility Considerations

Old code that dispatched only on the pass-styles it knew about will not correctly handle passables that contain byte-arrays.

The shim cannot reasonably support TypedArrays of DataViews backed by Immutable ArrayBuffers.

Upgrade Considerations

  • TODO BREAKING.md
  • TODO NEWS.md

@erights erights requested review from dckc and gibson042 April 4, 2023 03:44
@erights erights self-assigned this Apr 4, 2023
Copy link
Copy Markdown
Contributor

@mhofman mhofman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should try to align copyBytes with the prior art of the Web platform's Blob.

@erights erights force-pushed the markm-binary branch 2 times, most recently from 80cc7f2 to fca467e Compare September 16, 2023 03:35
@erights erights force-pushed the markm-binary branch 4 times, most recently from 15cbf4f to a6a6ca0 Compare September 26, 2023 05:51
@erights erights force-pushed the markm-binary branch 2 times, most recently from 4bf3d8c to ce5f3bf Compare November 7, 2023 19:22
@erights erights changed the title WIP fix: CopyBytes new binary Passable type WIP feat(pass-style,marshal)!: ByteArray, a new binary Passable type Jan 11, 2024
@erights erights force-pushed the markm-binary branch 3 times, most recently from b25c593 to fe561d2 Compare January 11, 2024 06:24
@erights erights force-pushed the markm-to-immutable-3 branch from b32ab45 to 544a213 Compare November 17, 2024 00:50
@erights erights force-pushed the markm-to-immutable-3 branch 2 times, most recently from d0f1cd5 to bc99d9e Compare January 15, 2025 23:18
@erights erights force-pushed the markm-to-immutable-3 branch from bc99d9e to 0509bcc Compare February 17, 2025 03:22
@erights erights requested a review from kumavis April 30, 2025 02:30
@erights erights force-pushed the markm-to-immutable-3 branch from 0509bcc to 07017b0 Compare April 30, 2025 02:51
@erights erights force-pushed the markm-binary branch 2 times, most recently from 4502570 to fa09e05 Compare April 30, 2025 03:29
@erights erights marked this pull request as ready for review April 30, 2025 03:30
Copy link
Copy Markdown
Member

@gibson042 gibson042 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks mostly good, but I have reservations about treating ByteArray as a Container [of Passables].

@erights
Copy link
Copy Markdown
Contributor Author

erights commented May 1, 2025

Wow, @gibson042 , those suggestions were extremely helpful, thanks!

PTAL

@erights erights requested a review from gibson042 May 1, 2025 02:46
Copy link
Copy Markdown
Member

@gibson042 gibson042 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM modulo updating packages/patterns/README.md.

Copy link
Copy Markdown
Member

@gibson042 gibson042 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think this PR should also update arb-passable.js, although accommodations will be necessary to exclude ByteArrays in encodePassable.test.js until they have a defined encoding, e.g.:

-/** @param {typeof import('@fast-check/ava').fc} fc */
-export const makeArbitraries = fc => {
+/**
+ * @param {typeof import('@fast-check/ava').fc} fc
+ * @param {Array<'byteArray'>} [exclusions]
+ */
+export const makeArbitraries = (fc, exclusions = []) => {
   const arbString = fc.oneof(fc.string(), fc.fullUnicodeString());
 
   const keyableLeaves = [
     fc.constantFrom(null, undefined, false, true),
     arbString,
     arbString.map(s => Symbol.for(s)),
     // primordial symbols and registered lookalikes
     fc.constantFrom(
       ...Object.getOwnPropertyNames(Symbol).flatMap(k => {
         const v = Symbol[k];
         if (typeof v !== 'symbol') return [];
         return [v, Symbol.for(k), Symbol.for(`@@${k}`)];
       }),
     ),
     fc.bigInt(),
     fc.integer(),
+    ...[fc.uint8Array().map(arr => arr.buffer.transferToImmutable())].filter(
+      () => !exclusions.includes('byteArray'),
+    ),
     fc.constantFrom(-0, NaN, Infinity, -Infinity),
     fc.record({}),
     fc.constantFrom(exampleAlice, exampleBob, exampleCarol),
   ];

@gibson042
Copy link
Copy Markdown
Member

gibson042 commented May 1, 2025

And to capture my thoughts on the encoding itself: given that we want both shortlex ordering and (to abuse vocabulary somewhat) isomorphic rank-order comparisons of encodePassable output, that encoding should be something like a length prefix (using the same Elias delta variant as for bigints) followed by hexadecimal:

buffer data encoding
[] a1:0:
[0] a1:1:00
[255] a1:1:ff
[0 255] a1:2:00ff
[0 … 0] (10**9 + 2 zeros) a~10:1000000002:00…00

@erights
Copy link
Copy Markdown
Contributor Author

erights commented May 1, 2025

Oh, I think this PR should also update arb-passable.js, although accommodations will be necessary to exclude ByteArrays in encodePassable.test.js until they have a defined encoding,

Done

@erights erights force-pushed the markm-to-immutable-3 branch from e2fd1ff to 75ea396 Compare May 1, 2025 21:30
@erights
Copy link
Copy Markdown
Contributor Author

erights commented May 1, 2025

Thanks! LGTM modulo updating packages/patterns/README.md.

I just read patterns/README.md carefully, and did not spot the flaw you have in mind. Is this indeed the document you have in mind? What needs updating?

Copy link
Copy Markdown
Member

@gibson042 gibson042 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just read patterns/README.md carefully, and did not spot the flaw you have in mind. Is this indeed the document you have in mind? What needs updating?

Ah, no change is needed now that we acknowledge ByteArray as primitive.

Base automatically changed from markm-to-immutable-3 to master May 1, 2025 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support passable immutable ArrayBuffer as copy-data

4 participants