Skip to content
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

feat(marshal): encode capData in 1 level of JSON #1804

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/marshal/src/capDataJSON.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// @ts-check

const { Fail } = assert;

/** @param {unknown} x */
const assertJSON = x => {
assert.typeof(x, 'string');
void JSON.parse(x);
};

/**
* @param {import('./types').CapData<unknown>} capData - with "simple" slots;
* that is: slots whose JSON form has no occurrence of `:[`.
* @returns {string}
*/
export const capDataToJSON = ({ body, slots }) => {
assert(Array.isArray(slots));
const slotj = JSON.stringify(slots);
slotj.indexOf(':[') < 0 || Fail`expected simple slots`;
const body1 = body.replace(/^#/, '');
Copy link
Contributor

Choose a reason for hiding this comment

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

why not check body[0] === '#' and do body.slice(1), I think that's a lot more efficient.

Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to assume that the argument is a CapData record whose body is a "#"-prefixed JSON serialization of SmallCaps-encoded data, which would need a lot more explanation than appears here (and a better name).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

body.replace(/^#/, '') handles both smallCaps and qclass, no? (I haven't tested it, though).

Why is .slice(1) significantly more efficient?

Copy link
Contributor

Choose a reason for hiding this comment

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

body.replace(/^#/, '') handles both smallCaps and qclass, no? (I haven't tested it, though).

I guess that depends upon what this function is expected to return. Regardless of the answer to that, though, CapData like { body: `{"@qclass":"bigint","digits":"0"}`, slots: [] } and { body: `#"+0"`, slots: [] } represent exactly the same data (0n) but would have distinct String('{"$body":{"@qclass":"bigint","digits":"0"},"slots":[]}') and String('{"$body":"+0","slots":[]}') return values (respectively) from the current implementation—which seems like a problem because there's no remaining signal differentiating smallcaps from the legacy encoding.

Why is .slice(1) significantly more efficient?

The answer is implementation-specific, but basically comes down to being zero-copy.

$ esbench --eshost-option '-h V8,*XS*' \
'const unprefixed="a".repeat(1000), prefixed = "#" + unprefixed' '{
  "unprefixed.replace": `result = unprefixed.replace(/^#/, "")`,
  "unprefixed.slice": `result = unprefixed.startsWith("#") ? unprefixed.slice(1) : unprefixed`,
  "prefixed.replace": `result = prefixed.replace(/^#/, "")`,
  "prefixed.slice": `result = prefixed.startsWith("#") ? prefixed.slice(1) : prefixed`,
}'
#### Moddable XS
unprefixed.replace: 0.06 ops/ms
unprefixed.slice: 3.56 ops/ms
prefixed.replace: 0.07 ops/ms
prefixed.slice: 0.95 ops/ms

#### V8
unprefixed.replace: 29.41 ops/ms
unprefixed.slice: 100.00 ops/ms
prefixed.replace: 19.61 ops/ms
prefixed.slice: 62.50 ops/ms

Copy link
Contributor Author

Choose a reason for hiding this comment

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

...

Why is .slice(1) significantly more efficient?

The answer is implementation-specific, but basically comes down to being zero-copy.

I guess I trained my regex intuitions in perl where such things are optimized out the wazoo.

Thanks for the esbench details.

assertJSON(body1);
const json = `{"$body":${body1},"slots":${slotj}}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

In #1478 (comment) I suggest body#

assertJSON(json);
return json;
};

export const JSONToCapData = json => {
assert.typeof(json, 'string');
json.startsWith('{"$body":') || Fail`expected $body`;
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this only works when this body is first in the serialized JSON, not second?

json.endsWith('}') || Fail`expected }`;
const pos = json.lastIndexOf(':[');
pos > 0 || Fail`expected slots`;
const body = `#${json.slice('{"$body":'.length, pos - ',"slots"'.length)}`;
const slotj = json.slice(pos + 1, -1);
const slots = JSON.parse(slotj);
Array.isArray(slots) || Fail`expected slots to be Array`;
return { body, slots };
};
141 changes: 141 additions & 0 deletions packages/marshal/test/test-marshal1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
* @file avoid double-JSON encoding capData
*/

// @ts-check

// eslint-disable-next-line import/order
import { test } from './prepare-test-env-ava.js';

import { arbPassable } from '@endo/pass-style/tools.js';
import { fc } from '@fast-check/ava';
import { isKey, keyEQ } from '@endo/patterns';

import { Far, passStyleOf } from '@endo/pass-style';
import { makeTranslationTable } from './translationTable.js';
import { makeMarshal } from '../src/marshal.js';
import { JSONToCapData, capDataToJSON } from '../src/capDataJSON.js';

const smallCaps = /** @type {const} */ ({
serializeBodyFormat: 'smallcaps',
marshalSaveError: err => err,
});

const makeTestMarshal = () => {
const synthesizeRemotable = (_slot, iface) =>
Far(iface.replace(/^Alleged: /, ''), {});
const makeSlot = (v, serial) => {
const sty = passStyleOf(v);
if (sty === 'remotable') return `r${serial}`;
return `a(n) ${sty}`;
};
const tt = makeTranslationTable(makeSlot, synthesizeRemotable);

const m = makeMarshal(tt.convertValToSlot, tt.convertSlotToVal, smallCaps);
return m;
};

const brands = {
IST: Far('IST Brand', {}),
ATOM: Far('ATOM Brand', {}),
};

const suite = [
{ obj: null, json: '{"$body":null,"slots":[]}' },
{ obj: [1, 2, undefined], json: '{"$body":[1,2,"#undefined"],"slots":[]}' },
{ obj: { slots: [] }, json: '{"$body":{"slots":[]},"slots":[]}' },
// example from https://github.com/Agoric/agoric-sdk/issues/7999
{
obj: {
method: 'executeOffer',
offer: {
id: 'bid-1688229012779',
invitationSpec: {
callPipe: [['makeBidInvitation', [brands.ATOM]]],
instancePath: ['auctioneer'],
source: 'agoricContract',
},
offerArgs: {
maxBuy: {
brand: brands.ATOM,
value: 1_000_000_000_000n,
},
offerPrice: {
denominator: {
brand: brands.ATOM,
value: 1n,
},
numerator: {
brand: brands.IST,
value: 7n,
},
},
},
proposal: {
give: {
Bid: {
brand: brands.IST,
value: 3000n,
},
},
},
},
},
json: '{"$body":{"method":"executeOffer","offer":{"id":"bid-1688229012779","invitationSpec":{"callPipe":[["makeBidInvitation",["$0.Alleged: ATOM Brand"]]],"instancePath":["auctioneer"],"source":"agoricContract"},"offerArgs":{"maxBuy":{"brand":"$0","value":"+1000000000000"},"offerPrice":{"denominator":{"brand":"$0","value":"+1"},"numerator":{"brand":"$1.Alleged: IST Brand","value":"+7"}}},"proposal":{"give":{"Bid":{"brand":"$1","value":"+3000"}}}}},"slots":["r0","r1"]}',
},
];
harden(suite);

test('encode example passables in 1 level of JSON', t => {
const m = makeTestMarshal();

for (const { obj, json } of suite) {
// t.log(obj);
const cd = m.toCapData(obj);
const j = capDataToJSON(cd);
t.is(j, json);

const cd2 = JSONToCapData(j);
t.deepEqual(cd, cd2);
const v = m.fromCapData(cd);
keyEQ(obj, v) ? t.pass() : t.deepEqual(obj, v);
}
});

test('encode arbitrary passable in 1 level of JSON', t => {
const m = makeTestMarshal();
fc.assert(
fc.property(fc.record({ x: arbPassable }), ({ x }) => {
const { body, slots } = m.toCapData(x);
// t.log({ body, slots });
const j = capDataToJSON({ body, slots });
const cd = JSONToCapData(j);
// t.log({ cd });

const j2 = capDataToJSON(cd);
t.is(j, j2);

const cd2 = JSONToCapData(j2);
t.deepEqual(cd, cd2);

if (isKey(x)) {
const v = m.fromCapData(cd);
try {
if (keyEQ(x, v)) {
t.pass();
} else {
// explain what's different
t.deepEqual(x, v);
}
} catch (err) {
if (err.message.startsWith('Map comparison not yet implemented:')) {
t.pass();
} else {
t.fail(err);
}
}
}
}),
{ numRuns: 5_000 },
);
});
35 changes: 35 additions & 0 deletions packages/marshal/test/translationTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// #region marshal-table
const makeSlot1 = (val, serial) => {
const prefix = Promise.resolve(val) === val ? 'promise' : 'object';
return `${prefix}${serial}`;
};

export const makeTranslationTable = (
makeSlot = makeSlot1,
makeVal = x => x,
) => {
const valToSlot = new Map();
const slotToVal = new Map();

const convertValToSlot = val => {
if (valToSlot.has(val)) return valToSlot.get(val);
const slot = makeSlot(val, valToSlot.size);
valToSlot.set(val, slot);
slotToVal.set(slot, val);
return slot;
};

const convertSlotToVal = (slot, iface) => {
if (slotToVal.has(slot)) return slotToVal.get(slot);
if (makeVal) {
const val = makeVal(slot, iface);
valToSlot.set(val, slot);
slotToVal.set(slot, val);
return val;
}
throw Error(`no such ${iface}: ${slot}`);
};

return harden({ convertValToSlot, convertSlotToVal });
};
// #endregion marshal-table