Skip to content

Commit

Permalink
fix: incremental Performance Degradation with session.customRequest("…
Browse files Browse the repository at this point in the history
…variables") Calls

Fixes #2119
  • Loading branch information
connor4312 committed Oct 31, 2024
1 parent 4e9f6b8 commit 5e1cc31
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions src/adapter/variableStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,20 +428,8 @@ class VariableContext {
}

let variable: Variable | undefined;
if (
p.name === '[[FunctionLocation]]'
&& p.value
&& (p.value.subtype as string) === 'internal#location'
) {
variable = this.createVariable(
FunctionLocationVariable,
{
name: p.name,
presentationHint: { visibility: 'internal', attributes: ['readOnly'] },
sortOrder: SortOrder.Internal,
},
p.value,
);
if (isFunctionLocation(p)) {
variable = this.createFunctionLocationVariable(p);
} else if (p.value !== undefined) {
variable = this.createVariableByType(
{
Expand All @@ -461,6 +449,18 @@ class VariableContext {
return flatten(await Promise.all(properties));
}

public createFunctionLocationVariable(p: FunctionLocationDescriptor) {
return this.createVariable(
FunctionLocationVariable,
{
name: p.name,
presentationHint: { visibility: 'internal', attributes: ['readOnly'] },
sortOrder: SortOrder.Internal,
},
p.value,
);
}

private async createPropertyVar(
p: AnyPropertyDescriptor,
owner: Cdp.Runtime.RemoteObject,
Expand Down Expand Up @@ -971,16 +971,19 @@ class NodeVariable extends Variable {
}

class FunctionVariable extends ObjectVariable {
private readonly baseChildren = once(() => super.getChildren({ variablesReference: this.id }));

public override async toDap(previewContext: PreviewContextType): Promise<Dap.Variable> {
const [dap, children] = await Promise.all([
const [dap, props] = await Promise.all([
super.toDap(previewContext),
this.baseChildren(),
this.context.cdp.Runtime.getProperties({
objectId: this.remoteObject.objectId!,
ownProperties: true,
nonIndexedPropertiesOnly: true,
}),
]);

if (children.some(c => c instanceof FunctionLocationVariable)) {
dap.valueLocationReference = this.id;
const location = props?.internalProperties?.find(isFunctionLocation);
if (location) {
dap.valueLocationReference = this.context.createFunctionLocationVariable(location).id;
}

return dap;
Expand Down Expand Up @@ -1580,15 +1583,8 @@ export class VariableStore {
}

public async getLocations(variablesReference: number): Promise<Cdp.Debugger.Location> {
const container = this.vars.get(variablesReference);
if (!container) {
throw errors.locationNotFound();
}

// note: is actually free because the container will have cached its children
const children = await container.getChildren({ variablesReference });
const locationVar = children.find(isInstanceOf(FunctionLocationVariable));
if (!locationVar) {
const locationVar = this.vars.get(variablesReference);
if (!locationVar || !(locationVar instanceof FunctionLocationVariable)) {
throw errors.locationNotFound();
}

Expand Down Expand Up @@ -1630,3 +1626,15 @@ function errorFromException(details: Cdp.Runtime.ExceptionDetails): Dap.Error {
|| details.text;
return errors.createUserError(message);
}

type FunctionLocationDescriptor = Cdp.Runtime.InternalPropertyDescriptor & {
value: Cdp.Debugger.Location;
};

function isFunctionLocation(
p: Cdp.Runtime.InternalPropertyDescriptor,
): p is FunctionLocationDescriptor {
return p.name === '[[FunctionLocation]]'
&& !!p.value
&& (p.value.subtype as string) === 'internal#location';
}

0 comments on commit 5e1cc31

Please # to comment.