-
Notifications
You must be signed in to change notification settings - Fork 68
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
Optimise memory usage when formatting logger entries #10216
Conversation
Test the buildOption 1. Jetpack Beta
Option 2. Jurassic Ninja - available for logged-in A12s🚀 Launch a JN site with this branch 🚀 ℹ️ Install this Tampermonkey script to get more options. Build info:
Note: the build is updated when a new commit is pushed to this PR. |
Size Change: 0 B Total Size: 1.36 MB ℹ️ View Unchanged
|
$formatted_lines = []; | ||
$log_entry = array_shift( $entries ); | ||
while ( null !== $log_entry ) { | ||
foreach ( explode( "\n", $log_entry ) as $line ) { | ||
$formatted_lines[] = $line_prefix . $line; | ||
} | ||
unset( $log_entry ); | ||
$log_entry = array_shift( $entries ); | ||
} | ||
|
||
return implode( "\n", $formatted_lines ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I prefer to avoid while
loops if an iterator is available. In this case, a simple foreach
loop should do the trick. Even if there is context here, the $entries
array should be quite short, so this seems overoptimized, especially considering that array_shift
will modify the array every time.
$formatted_lines = []; | |
$log_entry = array_shift( $entries ); | |
while ( null !== $log_entry ) { | |
foreach ( explode( "\n", $log_entry ) as $line ) { | |
$formatted_lines[] = $line_prefix . $line; | |
} | |
unset( $log_entry ); | |
$log_entry = array_shift( $entries ); | |
} | |
return implode( "\n", $formatted_lines ); | |
$formatted_lines = []; | |
foreach ( $entries as $log_entry ) { | |
foreach ( explode( "\n", $log_entry ) as $line ) { | |
$formatted_lines[] = $line_prefix . $line; | |
} | |
} | |
return implode( "\n", $formatted_lines ); |
For me, the decreased number of anonymous functions created, combined with much fewer sprintf()
calls, should be more than sufficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
foreach
was my first choice. But I had to add an unset
because I caught a memory overflow on my local setup when testing a complex transactions page. This eliminated the option of using foreach
😞
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, could it be that you were still using sprintf()
for the whole prefix? I'm really surprised that PHP would not collect the foreach()
garbage. Anyhow, let's unblock others, we could experiment with this later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the quick reaction!
Fixes #10201
Changes proposed in this Pull Request
Optimizations to the memory consumption when formatting a logger entry:
Testing instructions
npm run changelog
to add a changelog file, choosepatch
to leave it empty if the change is not significant. You can add multiple changelog files in one PR by running this command a few times.Post merge