Skip to content

Commit

Permalink
Apply buffer copy optimization to updateOutput oto (#180755)
Browse files Browse the repository at this point in the history
For #168142

Follow up on 9fd77b4. Applies the same optimization to `updateOutput` as well
  • Loading branch information
mjbvz authored Apr 24, 2023
1 parent 6a283f7 commit 1ed09f8
Showing 1 changed file with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1421,11 +1421,7 @@ export class BackLayerWebView<T extends ICommonCellInfo> extends Themable {
renderer = content.renderer;
const first = output.outputs.find(op => op.mime === content.mimeType)!;


// Copy the underlying buffer so we only send over the data we need
const valueBytes = new Uint8Array(first.data.buffer);
transfer.push(valueBytes.buffer);

const valueBytes = copyBufferIfNeeded(first.data.buffer, transfer);
message = {
...messageBase,
outputId: output.outputId,
Expand All @@ -1436,7 +1432,7 @@ export class BackLayerWebView<T extends ICommonCellInfo> extends Themable {
metadata: output.metadata,
output: {
mime: first.mime,
valueBytes: valueBytes,
valueBytes,
},
allOutputs: output.outputs.map(output => ({ mime: output.mime })),
},
Expand Down Expand Up @@ -1474,16 +1470,20 @@ export class BackLayerWebView<T extends ICommonCellInfo> extends Themable {
const outputCache = this.insetMapping.get(content.source)!;
this.hiddenInsetMapping.delete(content.source);
let updatedContent: ICreationContent | undefined = undefined;

const transfer: ArrayBuffer[] = [];
if (content.type === RenderOutputType.Extension) {
const output = content.source.model;
const firstBuffer = output.outputs.find(op => op.mime === content.mimeType)!;

const valueBytes = copyBufferIfNeeded(firstBuffer.data.buffer, transfer);
updatedContent = {
type: RenderOutputType.Extension,
outputId: outputCache.outputId,
metadata: output.metadata,
output: {
mime: content.mimeType,
valueBytes: firstBuffer.data.buffer,
valueBytes,
},
allOutputs: output.outputs.map(output => ({ mime: output.mime }))
};
Expand All @@ -1496,7 +1496,7 @@ export class BackLayerWebView<T extends ICommonCellInfo> extends Themable {
cellTop: cellTop,
outputOffset: offset,
content: updatedContent
});
}, transfer);

outputCache.versionId = content.source.model.versionId;
return;
Expand Down Expand Up @@ -1730,6 +1730,19 @@ export class BackLayerWebView<T extends ICommonCellInfo> extends Themable {
}
}

function copyBufferIfNeeded(buffer: Uint8Array, transfer: ArrayBuffer[]): Uint8Array {
if (buffer.byteLength === buffer.buffer.byteLength) {
// No copy needed but we can't transfer either
return buffer;
} else {
// The buffer is smaller than its backing array buffer.
// Create a copy to avoid sending the entire array buffer.
const valueBytes = new Uint8Array(buffer);
transfer.push(valueBytes.buffer);
return valueBytes;
}
}

function getTokenizationCss() {
const colorMap = TokenizationRegistry.getColorMap();
const tokenizationCss = colorMap ? generateTokensCSSForColorMap(colorMap) : '';
Expand Down

0 comments on commit 1ed09f8

Please sign in to comment.