fix(server): reduce yjs binary size (#8681)

This commit is contained in:
forehalo
2024-11-04 05:45:05 +00:00
parent cdaac5602c
commit 63b58aad5c

View File

@@ -40,25 +40,26 @@ export class DocStorageOptions implements IDocStorageOptions {
) {} ) {}
mergeUpdates = async (updates: Uint8Array[]) => { mergeUpdates = async (updates: Uint8Array[]) => {
const doc = await this.recoverDoc(updates);
const yjsResult = Buffer.from(Y.encodeStateAsUpdate(doc));
const useYocto = await this.config.runtime.fetch( const useYocto = await this.config.runtime.fetch(
'doc/experimentalMergeWithYOcto' 'doc/experimentalMergeWithYOcto'
); );
if (useYocto) { if (useYocto) {
const doc = await this.recoverDoc(updates);
metrics.jwst.counter('codec_merge_counter').add(1); metrics.jwst.counter('codec_merge_counter').add(1);
const yjsResult = Buffer.from(Y.encodeStateAsUpdate(doc));
let log = false; let log = false;
let yoctoResult: Buffer | null = null;
try { try {
const yocto = yotcoMergeUpdates(updates.map(Buffer.from)); yoctoResult = yotcoMergeUpdates(updates.map(Buffer.from));
if (!compare(yjsResult, yocto)) { if (!compare(yjsResult, yoctoResult)) {
metrics.jwst.counter('codec_not_match').add(1); metrics.jwst.counter('codec_not_match').add(1);
this.logger.warn(`yocto codec result doesn't match yjs codec result`); this.logger.warn(`yocto codec result doesn't match yjs codec result`);
log = true; log = true;
if (this.config.node.dev) { if (this.config.node.dev) {
this.logger.warn(`Expected:\n ${yjsResult.toString('hex')}`); this.logger.warn(`Expected:\n ${yjsResult.toString('hex')}`);
this.logger.warn(`Result:\n ${yocto.toString('hex')}`); this.logger.warn(`Result:\n ${yoctoResult.toString('hex')}`);
} }
} }
} catch (e) { } catch (e) {
@@ -73,10 +74,16 @@ export class DocStorageOptions implements IDocStorageOptions {
); );
} }
return yjsResult; if (
} else { this.config.affine.canary &&
return this.simpleMergeUpdates(updates); yoctoResult &&
yoctoResult.length > 2 /* simple test for non-empty yjs binary */
) {
return yoctoResult;
} }
}
return yjsResult;
}; };
historyMaxAge = async (spaceId: string) => { historyMaxAge = async (spaceId: string) => {
@@ -89,11 +96,6 @@ export class DocStorageOptions implements IDocStorageOptions {
return this.config.doc.history.interval; return this.config.doc.history.interval;
}; };
@CallMetric('doc', 'yjs_merge_updates')
private simpleMergeUpdates(updates: Uint8Array[]) {
return Y.mergeUpdates(updates);
}
@CallMetric('doc', 'yjs_recover_updates_to_doc') @CallMetric('doc', 'yjs_recover_updates_to_doc')
private recoverDoc(updates: Uint8Array[]): Promise<Y.Doc> { private recoverDoc(updates: Uint8Array[]): Promise<Y.Doc> {
const doc = new Y.Doc(); const doc = new Y.Doc();