Skip to content

fix: incremental Performance Degradation with session.customRequest("variables") Calls #2120

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

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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