Skip to content

Commit 207f99d

Browse files
authored
Sync hashing of notebook outputs, hence sync nb diffing (microsoft#242386)
1 parent 3e9713f commit 207f99d

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

src/vs/workbench/contrib/notebook/common/services/notebookSimpleWorker.ts

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55
import { IDiffResult, ISequence, LcsDiff } from '../../../../../base/common/diff/diff.js';
6-
import { doHash, numberHash } from '../../../../../base/common/hash.js';
6+
import { doHash, hash, numberHash } from '../../../../../base/common/hash.js';
77
import { IDisposable } from '../../../../../base/common/lifecycle.js';
88
import { URI } from '../../../../../base/common/uri.js';
99
import { IRequestHandler, IWorkerServer } from '../../../../../base/common/worker/simpleWorker.js';
@@ -18,7 +18,7 @@ import { filter } from '../../../../../base/common/objects.js';
1818

1919
class MirrorCell {
2020
private readonly textModel: MirrorModel;
21-
private _hash?: Promise<number>;
21+
private _hash?: number;
2222
public get eol() {
2323
return this._eol === '\r\n' ? DefaultEndOfLine.CRLF : DefaultEndOfLine.LF;
2424
}
@@ -46,11 +46,11 @@ class MirrorCell {
4646
return this.textModel.getValue();
4747
}
4848

49-
async getComparisonValue(): Promise<number> {
49+
getComparisonValue(): number {
5050
return this._hash ??= this._getHash();
5151
}
5252

53-
private async _getHash() {
53+
private _getHash() {
5454
let hashValue = numberHash(104579, 0);
5555

5656
hashValue = doHash(this.language, hashValue);
@@ -65,15 +65,13 @@ class MirrorCell {
6565
}
6666
}
6767

68-
// note: hash has not updated within the Promise.all since we must retain order
69-
const digests = await Promise.all(this.outputs.flatMap(op =>
70-
op.outputs.map(o => crypto.subtle.digest('sha-1', o.data.buffer))
71-
));
68+
const digests = this.outputs.flatMap(op =>
69+
op.outputs.map(o => hash(Array.from(o.data.buffer)))
70+
);
7271
for (const digest of digests) {
73-
hashValue = numberHash(new Int32Array(digest)[0], hashValue);
72+
hashValue = numberHash(digest, hashValue);
7473
}
7574

76-
7775
return hashValue;
7876
}
7977
}
@@ -148,21 +146,21 @@ class MirrorNotebookDocument {
148146

149147
class CellSequence implements ISequence {
150148

151-
static async create(textModel: MirrorNotebookDocument) {
149+
static create(textModel: MirrorNotebookDocument) {
152150
const hashValue = new Int32Array(textModel.cells.length);
153-
await Promise.all(textModel.cells.map(async (c, i) => {
154-
hashValue[i] = await c.getComparisonValue();
155-
}));
151+
textModel.cells.map((c, i) => {
152+
hashValue[i] = c.getComparisonValue();
153+
});
156154
return new CellSequence(hashValue);
157155
}
158156

159-
static async createWithCellId(textModel: MirrorNotebookDocument): Promise<Map<string, number>> {
157+
static createWithCellId(textModel: MirrorNotebookDocument): Map<string, number> {
160158
const hashValue = new Map<string, number>();
161-
await Promise.all(textModel.cells.map(async (c, i) => {
162-
const value = await c.getComparisonValue();
159+
textModel.cells.map((c, i) => {
160+
const value = c.getComparisonValue();
163161
const id: string = (c.metadata?.id || '') as string;
164162
hashValue.set(id, value);
165-
}));
163+
});
166164
return hashValue;
167165
}
168166

@@ -220,10 +218,8 @@ export class NotebookEditorSimpleWorker implements IRequestHandler, IDisposable
220218
const original = this._getModel(originalUrl);
221219
const modified = this._getModel(modifiedUrl);
222220

223-
const [originalSeq, modifiedSeq] = await Promise.all([
224-
CellSequence.create(original),
225-
CellSequence.create(modified),
226-
]);
221+
const originalSeq = CellSequence.create(original);
222+
const modifiedSeq = CellSequence.create(modified);
227223

228224
const diff = new LcsDiff(originalSeq, modifiedSeq);
229225
const diffResult = diff.ComputeDiff(false);
@@ -232,12 +228,11 @@ export class NotebookEditorSimpleWorker implements IRequestHandler, IDisposable
232228
const modifiedMetadata = filter(modified.metadata, key => !modified.transientDocumentMetadata[key]);
233229
return {
234230
metadataChanged: JSON.stringify(originalMetadata) !== JSON.stringify(modifiedMetadata),
235-
cellsDiff: await this.$computeDiffWithCellIds(original, modified) || diffResult,
236-
// linesDiff: cellLineChanges
231+
cellsDiff: this.$computeDiffWithCellIds(original, modified) || diffResult,
237232
};
238233
}
239234

240-
async $computeDiffWithCellIds(original: MirrorNotebookDocument, modified: MirrorNotebookDocument): Promise<IDiffResult | undefined> {
235+
$computeDiffWithCellIds(original: MirrorNotebookDocument, modified: MirrorNotebookDocument): IDiffResult | undefined {
241236
const originalCellIndexIds = original.cells.map((cell, index) => ({ index, id: (cell.metadata?.id || '') as string }));
242237
const modifiedCellIndexIds = modified.cells.map((cell, index) => ({ index, id: (cell.metadata?.id || '') as string }));
243238
const originalCellIds = originalCellIndexIds.map(c => c.id);
@@ -250,18 +245,19 @@ export class NotebookEditorSimpleWorker implements IRequestHandler, IDisposable
250245

251246
const diffResult: IDiffResult = { changes: [], quitEarly: false, };
252247

253-
const computeCellHashesById = async (notebook: MirrorNotebookDocument) => {
248+
const computeCellHashesById = (notebook: MirrorNotebookDocument) => {
254249
const hashValue = new Map<string, number>();
255-
await Promise.all(notebook.cells.map(async (c, i) => {
256-
const value = await c.getComparisonValue();
250+
notebook.cells.map((c, i) => {
251+
const value = c.getComparisonValue();
257252
// Verified earlier that these cannot be empty.
258253
const id: string = (c.metadata?.id || '') as string;
259254
hashValue.set(id, value);
260-
}));
255+
});
261256
return hashValue;
262257
};
263258

264-
const [originalSeq, modifiedSeq] = await Promise.all([computeCellHashesById(original), computeCellHashesById(modified)]);
259+
const originalSeq = computeCellHashesById(original);
260+
const modifiedSeq = computeCellHashesById(modified);
265261

266262
while (modifiedCellIndexIds.length) {
267263
const modifiedCell = modifiedCellIndexIds.shift()!;

0 commit comments

Comments
 (0)