Skip to content

Commit 00d8db6

Browse files
committed
feat(patterns): pattern-based compression
1 parent b8f81f1 commit 00d8db6

File tree

6 files changed

+1051
-15
lines changed

6 files changed

+1051
-15
lines changed

packages/patterns/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export {
7474
getInterfaceGuardPayload,
7575
getInterfaceMethodKeys,
7676
} from './src/patterns/getGuardPayloads.js';
77+
export { mustCompress, mustDecompress } from './src/patterns/compress.js';
7778

7879
// eslint-disable-next-line import/export
7980
export * from './src/types.js';
+292
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
// @ts-nocheck So many errors that the suppressions hamper readability.
2+
// TODO fix and then turn at-ts-check back on
3+
import {
4+
assertChecker,
5+
makeTagged,
6+
passStyleOf,
7+
recordNames,
8+
recordValues,
9+
} from '@endo/marshal';
10+
import {
11+
kindOf,
12+
assertPattern,
13+
maybeMatchHelper,
14+
matches,
15+
checkMatches,
16+
mustMatch,
17+
} from './patternMatchers.js';
18+
import { isKey } from '../keys/checkKey.js';
19+
import { keyEQ } from '../keys/compareKeys.js';
20+
21+
/** @import {Compress, Decompress, MustCompress, MustDecompress} from '../types.js' */
22+
23+
const { fromEntries } = Object;
24+
const { Fail, quote: q } = assert;
25+
26+
const isNonCompressingMatcher = pattern => {
27+
const patternKind = kindOf(pattern);
28+
if (patternKind === undefined) {
29+
return false;
30+
}
31+
const matchHelper = maybeMatchHelper(patternKind);
32+
return matchHelper && matchHelper.compress === undefined;
33+
};
34+
35+
/**
36+
* When, for example, all the specimens in a given store match a
37+
* specific pattern, then each of those specimens must contain the same
38+
* literal superstructure as their one shared pattern. Therefore, storing
39+
* that literal superstructure would be redumdant. If `specimen` does
40+
* match `pattern`, then `compress(specimen, pattern)` will return a bindings
41+
* array which is hopefully more compact than `specimen` as a whole, but
42+
* carries all the information from specimen that cannot be derived just
43+
* from knowledge that it matches this `pattern`.
44+
*
45+
* @type {Compress}
46+
*/
47+
const compress = (specimen, pattern) => {
48+
if (isNonCompressingMatcher(pattern)) {
49+
if (matches(specimen, pattern)) {
50+
return harden({ compressed: specimen });
51+
}
52+
return undefined;
53+
}
54+
55+
// Not yet frozen! Used to accumulate bindings
56+
const bindings = [];
57+
const emitBinding = binding => {
58+
bindings.push(binding);
59+
};
60+
harden(emitBinding);
61+
62+
/**
63+
* @param {Passable} innerSpecimen
64+
* @param {Pattern} innerPattern
65+
* @returns {boolean}
66+
*/
67+
const compressRecur = (innerSpecimen, innerPattern) => {
68+
assertPattern(innerPattern);
69+
if (isKey(innerPattern)) {
70+
return keyEQ(innerSpecimen, innerPattern);
71+
}
72+
const patternKind = kindOf(innerPattern);
73+
const specimenKind = kindOf(innerSpecimen);
74+
switch (patternKind) {
75+
case undefined: {
76+
return false;
77+
}
78+
case 'copyArray': {
79+
if (
80+
specimenKind !== 'copyArray' ||
81+
innerSpecimen.length !== innerPattern.length
82+
) {
83+
return false;
84+
}
85+
return innerPattern.every((p, i) => compressRecur(innerSpecimen[i], p));
86+
}
87+
case 'copyRecord': {
88+
if (specimenKind !== 'copyRecord') {
89+
return false;
90+
}
91+
const specimenNames = recordNames(innerSpecimen);
92+
const pattNames = recordNames(innerPattern);
93+
94+
if (specimenNames.length !== pattNames.length) {
95+
return false;
96+
}
97+
const specimenValues = recordValues(innerSpecimen, specimenNames);
98+
const pattValues = recordValues(innerPattern, pattNames);
99+
100+
return pattNames.every(
101+
(name, i) =>
102+
specimenNames[i] === name &&
103+
compressRecur(specimenValues[i], pattValues[i]),
104+
);
105+
}
106+
case 'copyMap': {
107+
if (specimenKind !== 'copyMap') {
108+
return false;
109+
}
110+
const {
111+
payload: { keys: pattKeys, values: valuePatts },
112+
} = innerPattern;
113+
const {
114+
payload: { keys: specimenKeys, values: specimenValues },
115+
} = innerSpecimen;
116+
// TODO BUG: this assumes that the keys appear in the
117+
// same order, so we can compare values in that order.
118+
// However, we're only guaranteed that they appear in
119+
// the same rankOrder. Thus we must search one of these
120+
// in the other's rankOrder.
121+
if (!keyEQ(specimenKeys, pattKeys)) {
122+
return false;
123+
}
124+
return compressRecur(specimenValues, valuePatts);
125+
}
126+
default:
127+
{
128+
const matchHelper = maybeMatchHelper(patternKind);
129+
if (matchHelper) {
130+
if (matchHelper.compress) {
131+
const subCompressedRecord = matchHelper.compress(
132+
innerSpecimen,
133+
innerPattern.payload,
134+
compress,
135+
);
136+
if (subCompressedRecord === undefined) {
137+
return false;
138+
} else {
139+
emitBinding(subCompressedRecord.compressed);
140+
return true;
141+
}
142+
} else if (matches(innerSpecimen, innerPattern)) {
143+
assert(isNonCompressingMatcher(innerPattern));
144+
emitBinding(innerSpecimen);
145+
return true;
146+
} else {
147+
return false;
148+
}
149+
}
150+
}
151+
throw Fail`unrecognized kind: ${q(patternKind)}`;
152+
}
153+
};
154+
155+
if (compressRecur(specimen, pattern)) {
156+
return harden({ compressed: bindings });
157+
} else {
158+
return undefined;
159+
}
160+
};
161+
harden(compress);
162+
163+
/**
164+
* `mustCompress` is to `compress` approximately as `mustMatch` is to `matches`.
165+
* Where `compress` indicates pattern match failure by returning `undefined`,
166+
* `mustCompress` indicates pattern match failure by throwing an error
167+
* with a good pattern-match-failure diagnostic. Thus, like `mustMatch`,
168+
* `mustCompress` has an additional optional `label` parameter to be used on
169+
* the outside of that diagnostic if needed. If `mustCompress` does return
170+
* normally, then the pattern match succeeded and `mustCompress` returns a
171+
* valid compressed value.
172+
*
173+
* @type {MustCompress}
174+
*/
175+
export const mustCompress = (specimen, pattern, label = undefined) => {
176+
const compressedRecord = compress(specimen, pattern);
177+
if (compressedRecord !== undefined) {
178+
return compressedRecord.compressed;
179+
}
180+
// `compress` is validating, so we don't need to redo all of `mustMatch`.
181+
// We use it only to generate the error.
182+
// Should only throw
183+
checkMatches(specimen, pattern, assertChecker, label);
184+
throw Fail`internal: ${label}: inconsistent pattern match: ${q(pattern)}`;
185+
};
186+
harden(mustCompress);
187+
188+
/**
189+
* `decompress` reverses the compression performed by `compress`
190+
* or `mustCompress`, in order to recover the equivalent
191+
* of the original specimen from the `bindings` array and the `pattern`.
192+
*
193+
* @type {Decompress}
194+
*/
195+
const decompress = (compressed, pattern) => {
196+
if (isNonCompressingMatcher(pattern)) {
197+
return compressed;
198+
}
199+
200+
assert(Array.isArray(compressed));
201+
passStyleOf(compressed) === 'copyArray' ||
202+
Fail`Pattern ${pattern} expected bindings array: ${compressed}`;
203+
let i = 0;
204+
const takeBinding = () => {
205+
i < compressed.length ||
206+
Fail`Pattern ${q(pattern)} expects more than ${q(
207+
compressed.length,
208+
)} bindings: ${compressed}`;
209+
const binding = compressed[i];
210+
i += 1;
211+
return binding;
212+
};
213+
harden(takeBinding);
214+
215+
const decompressRecur = innerPattern => {
216+
assertPattern(innerPattern);
217+
if (isKey(innerPattern)) {
218+
return innerPattern;
219+
}
220+
const patternKind = kindOf(innerPattern);
221+
switch (patternKind) {
222+
case undefined: {
223+
throw Fail`decompress expected a pattern: ${q(innerPattern)}`;
224+
}
225+
case 'copyArray': {
226+
return harden(innerPattern.map(p => decompressRecur(p)));
227+
}
228+
case 'copyRecord': {
229+
const pattNames = recordNames(innerPattern);
230+
const pattValues = recordValues(innerPattern, pattNames);
231+
const entries = pattNames.map((name, j) => [
232+
name,
233+
decompressRecur(pattValues[j]),
234+
]);
235+
// Reverse so printed form looks less surprising,
236+
// with ascenting rather than descending property names.
237+
return harden(fromEntries(entries.reverse()));
238+
}
239+
case 'copyMap': {
240+
const {
241+
payload: { keys: pattKeys, values: valuePatts },
242+
} = innerPattern;
243+
return makeTagged(
244+
'copyMap',
245+
harden({
246+
keys: pattKeys,
247+
values: valuePatts.map(p => decompressRecur(p)),
248+
}),
249+
);
250+
}
251+
default:
252+
{
253+
const matchHelper = maybeMatchHelper(patternKind);
254+
if (matchHelper) {
255+
if (matchHelper.decompress) {
256+
const subCompressed = takeBinding();
257+
return matchHelper.decompress(
258+
subCompressed,
259+
innerPattern.payload,
260+
decompress,
261+
);
262+
} else {
263+
assert(isNonCompressingMatcher(innerPattern));
264+
return takeBinding();
265+
}
266+
}
267+
}
268+
throw Fail`unrecognized pattern kind: ${q(patternKind)} ${q(
269+
innerPattern,
270+
)}`;
271+
}
272+
};
273+
274+
return decompressRecur(pattern);
275+
};
276+
harden(decompress);
277+
278+
/**
279+
* `decompress` reverses the compression performed by `compress`
280+
* or `mustCompress`, in order to recover the equivalent
281+
* of the original specimen from `compressed` and `pattern`.
282+
*
283+
* @type {MustDecompress}
284+
*/
285+
export const mustDecompress = (compressed, pattern, label = undefined) => {
286+
const value = decompress(compressed, pattern);
287+
// `decompress` does some checking, but is not validating, so we
288+
// need to do the full `mustMatch` here to validate as well as to generate
289+
// the error if invalid.
290+
mustMatch(value, pattern, label);
291+
return value;
292+
};

0 commit comments

Comments
 (0)