codecadwallader / codemaid

CodeMaid is an open source Visual Studio extension to cleanup and simplify our C#, C++, F#, VB, PHP, PowerShell, JSON, XAML, XML, ASP, HTML, CSS, LESS, SCSS, JavaScript and TypeScript coding.
http://www.codemaid.net
GNU Lesser General Public License v3.0
1.92k stars 362 forks source link

Extremely slow line joining with longer JSON content #793

Open ghost opened 3 years ago

ghost commented 3 years ago

Environment

Description

Extremely slow line joining with bigger JSON files

[
  {
    "someVariable": "",
    "someOtherVariable": ""
  },
  ...
]

Steps to recreate

  1. Create a ~50 lines JSON file
  2. Create a ~500 lines JSON file
  3. Hit "Join lines" for both entire file

Current behavior

The ~50 lines file takes roughly less than half a second on my setup The ~500 lines file causes VS to hang for ~20 seconds on my setup

Expected behavior

I would expect completion time to be linear, and/or that it didn't cause VS to hang while processing the file. "Cleanup active document" happens almost instantly for both files.

codecadwallader commented 3 years ago

Thanks for reporting the issue. I am able to reproduce it.

Our join lines feature is a very basic RegEx find/replace of new line characters with spaces utilizing the VS API. https://github.com/codecadwallader/codemaid/blob/dev/CodeMaid/Integration/Commands/JoinLinesCommand.cs#L89

If you perform the same type of operation directly through Visual Studio's find and replace dialog it definitely does not take as long as it does through the API.

ghost commented 3 years ago

I dug into this, ran the profiler and found the slowdown is caused by the LineChanged listener: https://github.com/codecadwallader/codemaid/blob/253fa4cb31bdfd1d91651db759e9af70df6b5ea7/CodeMaid/Integration/Events/TextEditorEventListener.cs#L73 image I am no longer troubled by this issue but I can send a PR if you could tell me a bit how you'd go about it (as I'm not used to this source or extension development), otherwise, feel free to fix it once you have time!

codecadwallader commented 3 years ago

Thanks for running a profiler, that's very helpful. When I look at that I would say the time is spent in ReplacePattern (41.25%), not in LineChanged (0.10%). I would also expect ReplacePattern to be what consumes most of the time since that's where the actual update work is being accomplished. Were there any other events shown coming off of ReplacePattern that explained the other 41.15%?

ghost commented 3 years ago

@codecadwallader Hmm you are right. I assumed the elapsed time did not include all the calls, as there were no other events within ReplacePattern call tree other than the child LineChanged... And I just ran the profiler again to confirm that. image Makes me think there's some interaction with the editor that's not being recorded