8000 Improve memory efficiency by dword-design · Pull Request #18932 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Improve memory efficiency #18932

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

Merged
merged 1 commit into from
Jun 3, 2016
Merged

Improve memory efficiency #18932

merged 1 commit into from
Jun 3, 2016

Conversation

dword-design
Copy link
Q A
Branch? 2.7
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

This avoids concatenating $message and PHP_EOL (if necessary) as a new value, greatly improving memory efficiency for large $messages.

This avoids concatenating `$message` and `PHP_EOL` (if necessary) as a new value, greatly improving memory efficiency for large `$message`s.
@dword-design
Copy link
Author

This is my first pull request, please don't hurt me :)

@@ -70,7 +70,7 @@ public function getStream()
*/
protected function doWrite($message, $newline)
{
if (false === @fwrite($this->stream, $message.($newline ? PHP_EOL : ''))) {
if (false === @fwrite($this->stream, $message) || ($newline && (false === @fwrite($this->stream, PHP_EOL)))) {
Copy link
Member

Choose a reason for hiding this comment

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

what about:

        if (false === @fwrite($this->stream, $message)) {
            // should never happen
            throw new \RuntimeException('Unable to write output.');
        }
        if ($newline) {
            @fwrite($this->stream, PHP_EOL);
        }

Copy link
Author

Choose a reason for hiding this comment

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

In this case no Exception would be thrown if something goes wrong with writing PHP_EOL.

Copy link
Member

Choose a reason for hiding this comment

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

yep, which won't happen if the first fwrite already succedeed IMHO

Copy link
Author

Choose a reason for hiding this comment

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

Could be but what speaks against the safe solution?

@OskarStark
Copy link
Contributor

greatly improving memory efficiency

can you show some statistics about that?

@javiereguiluz
Copy link
Member

@Dword123 congrats on your first pull request and thanks for contributing to Symfony!

The kind 8000 of improvement that you propose here is always tricky for us. This affects the console output stream, which is something that it's not used frequently or on every request. That's why most of the times these improvements don't provide enough gains to be worthy. So let's see what other contributors think about this.

@mpdude
Copy link
Contributor
mpdude commented Jun 1, 2016

We're using symfony/console over at https://github.com/webfactory/slimdump, a tool that makes SQL dumps to stdout somewhat similar to what mysqldump does.

In that case, the message can be a hex-encoded BLOB (files or the like) and a few dozen MB in size.

If the newline is added to the string before being written, PHP has to allocate additional memory about the size of $message to store the new resulting value.

Asked the other way round: What problem could this change cause?

@nicolas-grekas
Copy link
Member

👍

@nicolas-grekas
Copy link
Member

Thank you @Dword123.

@nicolas-grekas nicolas-grekas merged commit c1df9f2 into symfony:2.7 Jun 3, 2016
nicolas-grekas added a commit that referenced this pull request Jun 3, 2016
This PR was merged into the 2.7 branch.

Discussion
----------

Improve memory efficiency

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

This avoids concatenating `$message` and `PHP_EOL` (if necessary) as a new value, greatly improving memory efficiency for large `$message`s.

Commits
-------

c1df9f2 Improve memory efficiency
mpdude added a commit to webfactory/slimdump that referenced this pull request Jun 3, 2016
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.

6 participants
0