From e91887e4632644ba2b0c8b08fdd9d4efa6654ca0 Mon Sep 17 00:00:00 2001 From: Adrien Foulon <6115458+Tofandel@users.noreply.github.com> Date: Fri, 22 Mar 2019 12:53:16 +0100 Subject: [PATCH] Fix performance issue of cloned instances I noted a performance issue using TCPDF when doing a lot of clones of the instance, it's caused by the destructor being called multiple times to cleanup things that already have been cleaned This checks if the files have already been cleaned before redoing an expensive filesystem operation again (glob) --- tcpdf.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tcpdf.php b/tcpdf.php index a1b4c1c..3bba7b0 100644 --- a/tcpdf.php +++ b/tcpdf.php @@ -7737,6 +7737,7 @@ class TCPDF { return ''; } + protected static $cleaned_ids = array(); /** * Unset all class variables except the following critical variables. * @param $destroyall (boolean) if true destroys all class variables, otherwise preserves critical variables. @@ -7749,7 +7750,11 @@ class TCPDF { if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) { mb_internal_encoding($this->internal_encoding); } + if (isset(self::$cleaned_ids[$this->file_id])) { + $destroyall = false; + } if ($destroyall AND !$preserve_objcopy) { + self::$cleaned_ids[$this->file_id] = true; // remove all temporary files $tmpfiles = glob(K_PATH_CACHE.'__tcpdf_'.$this->file_id.'_*'); if (!empty($tmpfiles)) {