PowerShellEditorServices icon indicating copy to clipboard operation
PowerShellEditorServices copied to clipboard

Variable Expansion: ScriptProperty ToStrings are slow to perform

Open JustinGrote opened this issue 3 years ago • 6 comments

image

Per @SeeminglyScience it's probably because the ToString is trying to marshal to the pipeline thread and deadlocking.

JustinGrote avatar Jan 27 '22 02:01 JustinGrote

@SeeminglyScience looks like it's not hard-hanging, just a performance issue. Could this have to do with the marshaling still?

https://user-images.githubusercontent.com/15258962/151478261-4f93b844-b432-41ee-ba6b-399335ff7db7.mp4

JustinGrote avatar Jan 28 '22 02:01 JustinGrote

Narrowed it down to this loop for enumerables that is "slow", I'll dig deeper into VariableDetails later. https://github.com/PowerShell/PowerShellEditorServices/blob/624fe30eda7b7dc918978e238a59b26b81a40dc7/src/PowerShellEditorServices/Services/DebugAdapter/Debugging/VariableDetails.cs#L338-L344

JustinGrote avatar Jan 28 '22 03:01 JustinGrote

@SeeminglyScience looks like it's not hard-hanging, just a performance issue. Could this have to do with the marshaling still?

Yep!

In certain circumstances (like this one) it will timeout after 250ms and then just invoke it anyway from a different thread (while still targeting the same runspace) potentially causing state corruption. So I'd say that still perfectly explains it and potentially makes it a little bit more scary.

SeeminglyScience avatar Jan 28 '22 03:01 SeeminglyScience

Here's the culprit: https://github.com/PowerShell/PowerShellEditorServices/blob/624fe30eda7b7dc918978e238a59b26b81a40dc7/src/PowerShellEditorServices/Services/DebugAdapter/Debugging/VariableDetails.cs#L198

If you set it to null instead of SafeToString(), everything is fast again.

JustinGrote avatar Jan 28 '22 04:01 JustinGrote

More specifically: https://github.com/PowerShell/PowerShellEditorServices/blob/3274c230cc6675859d5c89d6c176219c2c9430f7/src/PowerShellEditorServices/Utility/Extensions.cs#L25

@SeeminglyScience how would I get this obj.ToString() method on the correct invocation? It does feel like about a 250ms delay on each invocation.

JustinGrote avatar Jan 28 '22 04:01 JustinGrote

Adding @SeeminglyScience as I've hit my limits on figuring this out for now :)

JustinGrote avatar Jan 30 '22 16:01 JustinGrote

And fixed, with your test too!

andyleejordan avatar Dec 20 '22 20:12 andyleejordan