Giter Club home page Giter Club logo

Comments (11)

lcobucci avatar lcobucci commented on May 24, 2024

@codercms are you always experiencing that low performance or it depends on the text length? Does anything change if /maxmemory:NN gets added to php://temp? The filesystem should only be used when the text is big (default limit is 2Mb).

from laminas-diactoros.

Ocramius avatar Ocramius commented on May 24, 2024

my suggestion is to implement pure RAM string stream.

Overall agree, php://memory could suffice here, but we also are not sure if the provided string|StreamInterface will fit in memory, hence why a more conservative php://temp was used here.

I suggest sending a patch, and then we discuss the implications there: it is also acceptable to tell end-users that TextResponse is not intended for large payloads (which is in fact the original design anyway)

from laminas-diactoros.

codercms avatar codercms commented on May 24, 2024

@lcobucci there is two cases:

  1. I'm experiencing low performance because TextResponse doing excess allocation of PHP native stream (allocation time + string copying time)
  2. With large text length performance difference is even more

Patched TextReponse:

$size = 10 * 1024 * 1024;
$body = new Stream("php://temp/maxmemory:{$size}", 'wb+');

It is still slower than approach without PHP native stream allocation.
320 RPS without native PHP stream
260 RPS with native PHP stream

from laminas-diactoros.

codercms avatar codercms commented on May 24, 2024

@Ocramius I guess this patch will not affect end users, because php native stream is only allocated when string was passed to the constructor - this means that the memory for response is already allocated.

from laminas-diactoros.

Ocramius avatar Ocramius commented on May 24, 2024

Looking back at this, I think that for text, we can use php://memory, and let consumers with large text/plain payloads implement their own streamed response perhaps?

Unsure if this should be considered a BC break.

from laminas-diactoros.

Ocramius avatar Ocramius commented on May 24, 2024

Similar for html, which is the more common response object.

from laminas-diactoros.

codercms avatar codercms commented on May 24, 2024

@Ocramius I think there is one more approach (in two different ways):

  1. Create a “fake” stream object that is just simulating stream behavior (under the hood its just a string wrapper which implements StreamInterface) - end user will have to create stream object
  2. Create a true plain string response which will create a “fake” stream object under the hood - no additional actions for end user

from laminas-diactoros.

Ocramius avatar Ocramius commented on May 24, 2024

I didn't really understand the difference between those suggested approaches 🤔

from laminas-diactoros.

codercms avatar codercms commented on May 24, 2024

@Ocramius actually its only one approach which can be implemented in two manners

from laminas-diactoros.

Ocramius avatar Ocramius commented on May 24, 2024

But basically, if I understand it correctly, avoiding usage of a stream at all, and having a string variable instead (since html/text responses are generally well within the memory limits)

from laminas-diactoros.

codercms avatar codercms commented on May 24, 2024

@Ocramius yes it is. The performance problem occurs when the Response object allocates stream for string (I mean PHP stream), but actually there is no need to allocate memory one more time.

from laminas-diactoros.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.