From f7a2f770081faf5cbfbf7835d6a803470c3c6d5a Mon Sep 17 00:00:00 2001 From: divinity76 Date: Mon, 15 May 2023 06:23:25 +0200 Subject: [PATCH] reduce feof() calls (#1041) micro-optimization: when doing large file copies, this will reduce the number of feof() calls. for example, if copying 100MB, this will save approximately 25,599 feof() calls (255 feof() calls for every MB) - also feofs() may do an actual syscall, and syscalls are relatively expensive/time-consuming. --- tinyfilemanager.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tinyfilemanager.php b/tinyfilemanager.php index 67ccd50..1abc12f 100644 --- a/tinyfilemanager.php +++ b/tinyfilemanager.php @@ -971,7 +971,15 @@ if (!empty($_FILES) && !FM_READONLY) { if ($in) { if (PHP_VERSION_ID < 80009) { // workaround https://bugs.php.net/bug.php?id=81145 - while (!feof($in)) { fwrite($out, fread($in, 4096)); } + do { + for (;;) { + $buff = fread($in, 4096); + if ($buff === false || $buff === '') { + break; + } + fwrite($out, $buff); + } + } while (!feof($in)); } else { stream_copy_to_stream($in, $out); }