Packages icon indicating copy to clipboard operation
Packages copied to clipboard

[PHP] Apparent performance problems

Open BenjaminSchaaf opened this issue 5 years ago • 33 comments

Multiple people are reporting performance problems with PHP specifically: https://forum.sublimetext.com/t/st4-slow-after-upgrade-from-st3/57633/3.

Not entirely clear whether this is a core or packages issue, but I think here is a good starting point.

  • Sublime Version: 4106

BenjaminSchaaf avatar May 24 '21 02:05 BenjaminSchaaf

syntax cache size on my machine:

image

jfcherng avatar May 24 '21 04:05 jfcherng

Not entirely clear whether this is a core or packages issue, but I think here is a good starting point.

I guess it is a core issue because it happens even in safe mode.

I was thinking that if this is happening just to load the PHP syntax when the first PHP file is loaded, then there should be an option like "syntaxes_to_preload", and there you could add those you normally use.

In my case, 99% of the files I open are PHP or JS code, then I could add something like this to settings:

syntaxes_to_preload: [php, js]

And when I reach the first PHP file, there would be a big chance that the syntax is already prealoaded and the file would open immediately, like in ST3.

PapayaMedia avatar May 24 '21 13:05 PapayaMedia

I guess it is a core issue because it happens even in safe mode.

packages in this context refers to the default packages (this repository). Those are used in safe mode and provide things like PHP syntax highlighting. This very well could just be a problem with the PHP syntax.

BenjaminSchaaf avatar May 24 '21 14:05 BenjaminSchaaf

Uh oh... I was thinking on Package Control. Thank you for the clarification.

PapayaMedia avatar May 24 '21 14:05 PapayaMedia

For the record, with https://github.com/sublimehq/Packages/pull/2789 (which does not use with_prototype but the special CSS and JS languages include PHP) I have the following cache sizes and loading a php file from a cold cache (i.e. folder cleared and after a restart) takes ~3s on my machine.

~/.cache/sublime-text/Cache/PHP ▶ ll
.rw-r--r--  28k fichte 25 May  0:35 CSS (for PHP).sublime-syntax.cache
.rw-r--r--  10M fichte 25 May  0:36 CSS (for PHP).sublime-syntax.rcache
.rw-r--r--  20k fichte 25 May  0:35 JavaScript (for PHP).sublime-syntax.cache
.rw-r--r-- 9.4M fichte 25 May  0:36 JavaScript (for PHP).sublime-syntax.rcache
.rw-r--r-- 8.9k fichte 25 May  0:35 PHP.sublime-syntax.cache
.rw-r--r-- 9.2M fichte 25 May  0:36 PHP.sublime-syntax.rcache
.rw-r--r--  48k fichte 25 May  0:35 PHP Source.sublime-syntax.cache
.rw-r--r-- 9.1M fichte 25 May  0:36 PHP Source.sublime-syntax.rcache
.rw-r--r-- 1.7k fichte 25 May  0:35 Regular Expressions (PHP).sublime-syntax.cache

I suspect the large list of "known symbols" in PHP Source.sublime-syntax to be at fault.

Edit: No, even after commenting out all the support matches, the cache sizes and load time barely changed (got like 1MB off each).

Edi2: I should note that I only have long load times for the first PHP file I open in a session. Afterwards, even files with 2k lines (like syntax_test_php.php) load instantly.

FichteFoll avatar May 24 '21 22:05 FichteFoll

I did some more experiments with the issue. There are a couple with_protoype pushes for heredocs with Php interpolation (6 in total). Removing those reduces cache sizes and load times by half. Still not great, but better.

Regardless, this was already there in ST3 and removing it would also remove functionality.

https://github.com/sublimehq/Packages/blob/f996a6fa9dcbda126d1e92a6bd47fe2be7892f63/PHP/PHP%20Source.sublime-syntax#L1304-L1339

FichteFoll avatar May 24 '21 23:05 FichteFoll

Comparing runtimes with master and st3 branches on my 4106 yielded very similar results in both file size and initial load time, both around 10MB rcache size and 2s load time.

#2789 is a bit slower even (about 50%, so 3s) because it introduces another level of indirection with PHP interpolation inside JS and CSS strings which wasn't there before.

FichteFoll avatar May 24 '21 23:05 FichteFoll

In my test, the large cache size comes from

  • Those with_prototypes in order to support variable interpolation in PHP Source.sublime-syntax.
  • The embedded-html context in PHP Source.sublime-syntax.
  • The with_prototype in PHP.sublime-syntax in order to support HTML out of <?php ... ?> tag.

(Those embeds are fine as they barely increase the cache size, but you can't use embed to implement variable interpolation.)

Unfortunately, they are all language features in PHP...

Remove some heredocs may help a little bit, but the cache size still very large with a comparison to other languages.

jfcherng avatar May 25 '21 00:05 jfcherng

I second all your results. I'v done the same steps with regards using PHP/JS/CSS from ST3 and commenting out all the support stuff without reasonable difference. ST4 is definitly slower even with unchanged syntax definitions. So syntax complexity is not the only nor primary reason.

I don't see such delays with JSP (part of Java Rewrite) and latest changes introduced to Rails package (PR). JSP and HAML both make huge use of embedding JS/CSS and even Ruby/Java into each other very much the same way as we do in #2789. Syntax cache of both packages is way smaller.

But I had to notice an huge impact of latest Windows 21H1. All my syntax tests and benchmark files now take 2-3 times more time than they used to do with 20H1. I first blamed ST4107, but it is the same for ST4095.

Running the same tests on my bussiness notebook (Windows 10 1903) which normally used to be 30% slower than my private one still shows the same benchmark times with ST4107.

So some of the most recent performance drops seem to be caused by OS upgrade. I haven't activly updated drivers or something like that.

I turned off hardware accelleration to avoid performance impacts by opengl drivers for benchmarking, but finally turned out it doesn't matter much.

deathaxe avatar May 25 '21 15:05 deathaxe

Finally found the initial discussion: https://forum.sublimetext.com/t/st4-high-cpu-loads-while-saving-php-files-on-win-possible-issue-in-php-package/57435/9

deathaxe avatar May 25 '21 15:05 deathaxe

We can keep interpolation support in HEREDOCs using embed. It just means to create an intermediate inherited syntax definition for each of those the same way as we did for the others already.

  • JSON (for PHP).sublime-syntax
  • SQL (for PHP).sublime-syntax
  • ...

The only disadvantage is the number of syntaxes increasing.

deathaxe avatar May 25 '21 20:05 deathaxe

We can keep interpolation support in HEREDOCs using embed.

But do we gain anything from that? Does it reduce the cache footprint? Or are the loaded lazily?

FichteFoll avatar May 25 '21 23:05 FichteFoll

We can keep interpolation support in HEREDOCs using embed.

But do we gain anything from that? Does it reduce the cache footprint? Or are the loaded lazily?

In my test, they are loaded lazily.

jfcherng avatar May 26 '21 01:05 jfcherng

Yes we do.

First of all a prototype: behaves much more sane than with_prototype. I never hit any inclusion loops with the former one while the latter one caused those much more easily. Somehow they behave different in that way. Furthermore prototype respects meta_include_prototype which with_prototype doesn't. It helps to keep things under control, too.

Another thing is with_prototype converting any embed within the syntax being pushed on stack into another push with_withprototype, so we loose all benefints embed has to offer.

By just extending the embedded syntaxes and include interpolations context into normal prototype we can work around all of it. Only disadvantage is: If those embedded syntaxes embed others, those wouldn't support interpolation without creating further intermediate syntaxes.

It is tedious but the only way out atm.

deathaxe avatar May 26 '21 16:05 deathaxe

Yes we do.

Well, I know there are general advantages to it, but I was curious about the relevance to this issue in particular.

FichteFoll avatar May 26 '21 20:05 FichteFoll

I do a simple "replace with_prototype with embed" (to utilize lazy loading) experiment.

With ST 4107 clean portable, unmodified syntax: Snipaste_2021-05-27_11-26-26

With ST 4107 clean portable, "replace with_prototype with embed":

The size of PHP.sublime-syntax.rcache drops from 11MB to about 8MB without losing language features. Snipaste_2021-05-27_11-38-42 These followings are lazy loaded only when it's used. Snipaste_2021-05-27_11-39-50

It helps a little bit, but 8MB is still too large imo...


Not sure will prototype: helps the loading speed further or not.

jfcherng avatar May 27 '21 03:05 jfcherng

Followed that route any further https://github.com/deathaxe/sublime-packages/tree/wip/php/PHP

It's based on PR #2789

Doesn't make much of a difference.

  • First load of test file after startup takes 5-6seconds.
  • Indexer still hangs around for a long time as well after safing a file
  • Cache size is nearly the same (48MB).

There must be something else going on.

deathaxe avatar May 28 '21 16:05 deathaxe

I am facing a hang situation since I upgraded to sublime 4 a few days back. Every time you're typing () {} [] then it hangs and takes about 40 to 60 seconds. And it usually happens with files that have a large codebase.

My current file where I experience this issue has 50K+ lines.

To confirm this issue is from PHP syntax highlighting - I turned it off and switched to text mode and noticed no lag.

RishishPandey avatar May 31 '21 05:05 RishishPandey

And it usually happens with files that have a large codebase.

Do you know any public codebase which suffers from that? 50K+ line single PHP file sounds crazy (in PHP world) though... but ST should be able to deal with it.

jfcherng avatar May 31 '21 06:05 jfcherng

Do you know any public codebase which suffers from that?

No - I don't.

50K+ line single PHP file sounds crazy (in PHP world) though... but ST should be able to deal with it.

hahaha - yes ST is able to do it - it's just PHP syntax highlighter that is causing the issue. I switched to javascript syntax and it has no such issues.

RishishPandey avatar May 31 '21 06:05 RishishPandey