Skip to content

Commit

Permalink
Minimize unnecessary message deserialization (#6946)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne committed Aug 2, 2021
1 parent 9aa2ae6 commit 7314781
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/client/datascience/ipywidgets/ipyWidgetMessageDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export class IPyWidgetMessageDispatcher implements IIPyWidgetMessageDispatcher {
private async mirrorSend(data: any, _cb?: (err?: Error) => void): Promise<void> {
// If this is shell control message, mirror to the other side. This is how
// we get the kernel in the UI to have the same set of futures we have on this side
if (typeof data === 'string') {
if (typeof data === 'string' && data.includes('shell') && data.includes('execute_request')) {
const startTime = Date.now();
// eslint-disable-next-line @typescript-eslint/no-require-imports
const msg = this.deserialize(data);
Expand Down Expand Up @@ -275,12 +275,22 @@ export class IPyWidgetMessageDispatcher implements IIPyWidgetMessageDispatcher {
let message;

if (!this.isUsingIPyWidgets) {
if (!message) {
// Lets deserialize only if we know we have a potential case
// where this message contains some data we're interested in.
let mustDeserialize = false;
if (typeof data === 'string') {
mustDeserialize = data.includes(WIDGET_MIMETYPE) || data.includes(Identifiers.DefaultCommTarget);
} else {
// Array buffers (non-plain text data) must be deserialized.
mustDeserialize = true;
}
if (!message && mustDeserialize) {
message = this.deserialize(data as any) as any;
}

// Check for hints that would indicate whether ipywidgest are used in outputs.
if (
message &&
message.content &&
message.content.data &&
(message.content.data[WIDGET_MIMETYPE] || message.content.target_name === Identifiers.DefaultCommTarget)
Expand Down

0 comments on commit 7314781

Please sign in to comment.