Skip to content
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

Reduce memory footprint of PhpStormStubsSourceStubber and MemoizingParser #627

Merged
merged 2 commits into from
Jun 12, 2020

Conversation

kukulich
Copy link
Collaborator

@kukulich kukulich commented May 28, 2020

Fixes #624

@ondrejmirtes
Copy link
Contributor

Still more than double than before I guess :( @Ocramius is right that this should be supported but if I had to choose between:

  1. Be correct for a small percentage of users that would take advantage of case-insensitive constants and make the library worse for everyone
  2. Be a little bit incorrect (that we were this way until 4.3) and consume much less memory

...it's obvious what I'd choose :)

@kukulich
Copy link
Collaborator Author

kukulich commented May 28, 2020

The rest of the memory usage is probably not related to case insensitive constants.

PR builds: https://github.com/Roave/BetterReflection/actions?query=branch%3Aphpstorm-stubber
The last one is without static properties and the memory usage changed from 216.00 MB to 1.41 GB.

Current PR uses static properties again and it decreased from 2.31 GB to 1.38 GB: https://github.com/Roave/BetterReflection/pull/627/checks?check_run_id=718176772

I've tried revert of case insensitive constants just to test it and it's even worse - 1.85 GB: https://github.com/Roave/BetterReflection/pull/628/checks?check_run_id=718188887

@kukulich
Copy link
Collaborator Author

1.41 GB - Already contains case insensitive constants.
https://github.com/Roave/BetterReflection/runs/717048810?check_suite_focus=true

After #606 has been merged: 2.55 GB
https://github.com/Roave/BetterReflection/runs/717069119?check_suite_focus=true

So it looks it's something in #606.

@Ocramius
Copy link
Member

Ocramius commented May 28, 2020 via email

@kukulich
Copy link
Collaborator Author

@Ocramius Tested just now. It decresed to 178.00 MB (this PR + the revert). Probably problems with serialize/unserialize.

@kukulich
Copy link
Collaborator Author

kukulich commented May 28, 2020

It also faster: 12 seconds vs 18 seconds on my ntb

@Ocramius
Copy link
Member

Yeh, strings are heavy. We can run with the revert, but I'll need to also ensure that a few of the tests run in a separate process.

Also unsure about reverting the constraint that guarantees distinct AST nodes...

@ondrejmirtes
Copy link
Contributor

BTW what we did in CachedParser in PHPStan (same concept as Memoizing) some time ago is that we don't keep all cached keys in memory at once. There's an upper limit and keys get evicted when newer ones appear. Surprisingly this has minimal performance impact, but memory footprint is smaller. See: https://github.com/phpstan/phpstan-src/blob/master/src/Parser/CachedParser.php

@ondrejmirtes
Copy link
Contributor

Funny realization for me - there's zero memory impact if I use $sourceCode or hash('sha256', $sourceCode) as array keys as there's probably some hashing function on the PHP side applied anyway.

@kukulich kukulich force-pushed the memory branch 2 times, most recently from 62a6d3c to 7a5fe50 Compare May 29, 2020 22:01
@kukulich kukulich changed the title Less memory usage in PhpStormStubsSourceStubber Less memory usage May 29, 2020
@kukulich
Copy link
Collaborator Author

I've improved the memory usage with json_encode instead of serialize. It's not perfect, it's just better.

@kukulich kukulich force-pushed the memory branch 7 times, most recently from 3e4d4e3 to a647ea5 Compare June 4, 2020 19:26
@kukulich
Copy link
Collaborator Author

The solution here is not optimal (it's a little ugly) but has better memory usage than serialize/unserialize

Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@Ocramius Ocramius added this to the 4.6.0 milestone Jun 12, 2020
@Ocramius Ocramius self-assigned this Jun 12, 2020
@Ocramius Ocramius changed the title Less memory usage Reduce memory footprint of PhpStormStubsSourceStubber and MemoizingParser Jun 12, 2020
@Ocramius
Copy link
Member

Thanks @kukulich!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tests consuming alot memory right now
3 participants