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

fix: Avoid extra stream copy to memory/temp if we can determine the size #38407

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/private/Files/ObjectStore/S3ObjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,24 @@ protected function writeMultiPart(string $urn, StreamInterface $stream, string $
public function writeObject($urn, $stream, string $mimetype = null) {
$psrStream = Utils::streamFor($stream);

// ($psrStream->isSeekable() && $psrStream->getSize() !== null) evaluates to true for a On-Seekable stream
// so the optimisation does not apply
$buffer = new Psr7\Stream(fopen("php://memory", 'rwb+'));
Utils::copyToStream($psrStream, $buffer, $this->putSizeLimit);
$buffer->seek(0);
if ($buffer->getSize() < $this->putSizeLimit) {
$streamSize = $psrStream->getSize();
if ($psrStream->isSeekable() && (int)$streamSize !== 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I find it a bit weird to cast here and not above.
It should work though because $buffer->getSize will overwrite it later.

$loadStream = $psrStream;
} else {
// For cases where the stream reports seekable but no size is returned
$buffer = new Psr7\Stream(fopen("php://temp", 'rwb+'));
Utils::copyToStream($psrStream, $buffer, $this->putSizeLimit);
$buffer->seek(0);
$streamSize = $buffer->getSize();
$loadStream = $streamSize < $this->putSizeLimit
? $buffer
: new Psr7\AppendStream([$buffer, $psrStream]);
}

if ($streamSize < $this->putSizeLimit) {
// buffer is fully seekable, so use it directly for the small upload
$this->writeSingle($urn, $buffer, $mimetype);
$this->writeSingle($urn, $loadStream, $mimetype);
} else {
$loadStream = new Psr7\AppendStream([$buffer, $psrStream]);
$this->writeMultiPart($urn, $loadStream, $mimetype);
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/private/Files/Stream/SeekableHttpStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public function stream_tell() {
public function stream_stat() {
if ($this->getCurrent()) {
$stat = fstat($this->getCurrent());
$stat = $stat ? $stat : [];
$stat['size'] = $this->totalSize;
return $stat;
} else {
Expand Down
12 changes: 10 additions & 2 deletions tests/lib/Files/ObjectStore/S3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ public function testUploadNonSeekable() {

$s3 = $this->getInstance();

$s3->writeObject('multiparttest', NonSeekableStream::wrap(fopen(__FILE__, 'r')));
// We need an actual non-seekable resource here as NonSeekableStream won't be enough
// when it is passed to the GuzzleHttp\Psr7\Utils::streamFor
// which checks the actual stream meta data using stream_get_meta_data
@posix_mkfifo('/tmp/fifo', 0644);
$stream = fopen('/tmp/fifo', 'rw+');
stream_set_blocking($stream, false);
fwrite($stream, file_get_contents(__FILE__));

$s3->writeObject('multiparttest', NonSeekableStream::wrap($stream));

$result = $s3->readObject('multiparttest');

Expand Down Expand Up @@ -119,7 +127,7 @@ public function testEmptyUpload() {
$s3 = $this->getInstance();

$emptyStream = fopen("php://memory", "r");
fwrite($emptyStream, null);
fwrite($emptyStream, '');

$s3->writeObject('emptystream', $emptyStream);

Expand Down