slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.94k stars 1.95k forks source link

Slim3 Body opens a tempfile for each request #1681

Closed mbretter closed 8 years ago

mbretter commented 8 years ago

I've found that the response object creates the body by default from a tempfile, this means that for each request a tmpfile is created causing i/o.

wouldn't it be better to use:

$this->body = $body ? $body : new Body(fopen('php://memory', 'r+'));

by default instead?

geggleto commented 8 years ago

https://github.com/slimphp/Slim/blob/3.x/Slim/Http/Response.php#L126 offending line

akrabat commented 8 years ago

No. From the manual:

php://memory and php://temp are read-write streams that allow temporary data to be stored in a file-like wrapper. The only difference between the two is that php://memory will always store its data in memory, whereas php://temp will use a temporary file once the amount of data stored hits a predefined limit (the default is 2 MB).

i.e. for most cases Response's body uses memory, unless it's going to be too big, in which case it will drop to using a temporary file.

mbretter commented 8 years ago

thx

tuupola commented 8 years ago

As a sidenote php://temp is usually written to /tmp which is most of the time mounted as tmpfs which is stored in memory, and not disk. Your mileage may vary.

mbretter commented 8 years ago

I'm caging my installations using apparmor, so I'm not using /tmp, however 2MB of html/xml/json/whatever output should be enough in most cases. On the other hand, exceeding this limit could result in a critical performance drop, especially on VMs where I/O is definitely a bottleneck. Maybe some doc hints would be good, or a config switch?

akrabat commented 8 years ago

We'd welcome a PR to Slim-Website :)

mbretter commented 8 years ago

hehehe :D