Fixes memory problem of getBodySize when provided with large BodySize#184
Conversation
|
which problem are you trying to solve? can you add a unit test? |
To reiterate:Implementations to Compare (Replace
|
| File Size (MB) | Before Change (before.php) |
After Change (after.php) |
|---|---|---|
| 10 | ❌PHP Fatal error: Allowed memory size of 134217728 bytes exhausted |
✅ |
| 20 | ❌PHP Fatal error: Allowed memory size of 134217728 bytes exhausted |
✅ |
| 30 | ❌PHP Fatal error: Allowed memory size of 134217728 bytes exhausted |
✅ |
| 40 | ❌PHP Fatal error: Allowed memory size of 134217728 bytes exhausted |
✅ |
| 50 | ❌PHP Fatal error: Allowed memory size of 134217728 bytes exhausted |
✅ |
Proof that strlen($file) Works:
| File Size (MB) | Before Change (before.php) |
After Change (after.php) |
|---|---|---|
| 1 | ✅ | ✅ |
| 5 | ❌PHP Fatal error: Allowed memory size of 134217728 bytes exhausted |
✅ |
| File Size (MB) | Output of before.php |
Output of after.php |
|---|---|---|
| 1 | 1448272\n1048576\n1448304 |
1447976\n1048576\n1448008 |
| 5 | ❌ | 5654592\n5242880\n5654624 |
Note: The \ns are newlines, they were just changed to fit into the table
Verdict
Feel free to experiment and look at the results for yourself, but on my end, they speak for themselves. Both methods return the same size. But one can handle far larger sizes than the other, without failing. At smaller file sizes, the benefits are minuscule, however, by testing with larger and larger files, the difference is soon evident.
|
running the before/after example you suggsted on my machine leads identical results |
|
Have you tried different files (with increasing sizes) other than |
|
ok, I did a few more tests using misleading was the output of with a 80MB file on macOS I can reproduce a fatal error and also that the new code is considerably faster: |
|
did you test your change with different real world files of different formats? |
Environment: Same as above.Images (
|
| ID | Size | before.php |
after.php |
Resolution |
|---|---|---|---|---|
| 1 | 8.7M | ❌ | ✅ | 7680x4320 (8K) |
| 2 | 7.4M | ❌ | ✅ | 3840x2160 (4K) |
| 3 | 490K | ✅ | ✅ | 1920x1080 (2K) |
Text (.txt):
Creation: This
| ID | Size (MB) | before.php |
after.php |
|---|---|---|---|
| 1 | 1 | ✅ | ✅ |
| 2 | 5 | ❌ | ✅ |
| 3 | 10 | ❌ | ✅ |
Note: There is also a speedup in completion time across the board. (time php before/after.php)
Two common file formats with real world usage tested. Returns the same number, one just can handle higher file sizes without reaching the memory limit of 128M, alongside a speed benefit.
An alternate way of calculating the
BodySize, using less memory.