diff --git a/core/_bootstrap.php b/core/_bootstrap.php index 6dffbf14..a003ed6e 100644 --- a/core/_bootstrap.php +++ b/core/_bootstrap.php @@ -15,6 +15,11 @@ require_once "vendor/autoload.php"; _version_check(); _sanitise_environment(); +// The trace system has a certain amount of memory consumption every time it is used, +// so to prevent running out of memory during complex operations code that uses it should +// check if tracer output is enabled before making use of it. +$tracer_enabled = constant('TRACE_FILE')!==null; + // load base files $_tracer->begin("Bootstrap"); $_tracer->begin("Opening files"); diff --git a/core/database.php b/core/database.php index 003816d4..2cbf077f 100644 --- a/core/database.php +++ b/core/database.php @@ -190,10 +190,12 @@ class Database private function count_time(string $method, float $start, string $query, ?array $args): void { - global $_tracer; + global $_tracer, $tracer_enabled; $dur = microtime(true) - $start; - $query = trim(preg_replace('/^[\t ]+/m', '', $query)); // trim leading whitespace - $_tracer->complete($start * 1000000, $dur * 1000000, "DB Query", ["query"=>$query, "args"=>$args, "method"=>$method]); + if($tracer_enabled) { + $query = trim(preg_replace('/^[\t ]+/m', '', $query)); // trim leading whitespace + $_tracer->complete($start * 1000000, $dur * 1000000, "DB Query", ["query"=>$query, "args"=>$args, "method"=>$method]); + } $this->query_count++; $this->dbtime += $dur; } diff --git a/core/send_event.php b/core/send_event.php index ae2c42f3..24897658 100644 --- a/core/send_event.php +++ b/core/send_event.php @@ -108,6 +108,8 @@ $_shm_event_count = 0; */ function send_event(Event $event): void { + global $tracer_enabled; + global $_shm_event_listeners, $_shm_event_count, $_tracer; if (!isset($_shm_event_listeners[get_class($event)])) { return; @@ -116,8 +118,6 @@ function send_event(Event $event): void // send_event() is performance sensitive, and with the number // of times tracer gets called the time starts to add up - $tracer_enabled = constant('TRACE_FILE'); - if ($tracer_enabled) $_tracer->begin(get_class($event)); // SHIT: http://bugs.php.net/bug.php?id=35106 $my_event_listeners = $_shm_event_listeners[get_class($event)];