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

Apply buffer copy optimization to updateOutput #180755

Merged
merged 1 commit into from Apr 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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