add tracer_enabled check to the database class to prevent unnecessary memory build-up when tracer isn't outputting.

Globalized tracer_enabled to make it easier to access
This commit is contained in:
Matthew Barbour 2019-07-31 08:52:17 -05:00 committed by matthew
parent 7d110f11b6
commit fc294bfb3c
3 changed files with 12 additions and 5 deletions

View file

@ -15,6 +15,11 @@ require_once "vendor/autoload.php";
_version_check(); _version_check();
_sanitise_environment(); _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 // load base files
$_tracer->begin("Bootstrap"); $_tracer->begin("Bootstrap");
$_tracer->begin("Opening files"); $_tracer->begin("Opening files");

View file

@ -190,10 +190,12 @@ class Database
private function count_time(string $method, float $start, string $query, ?array $args): void private function count_time(string $method, float $start, string $query, ?array $args): void
{ {
global $_tracer; global $_tracer, $tracer_enabled;
$dur = microtime(true) - $start; $dur = microtime(true) - $start;
$query = trim(preg_replace('/^[\t ]+/m', '', $query)); // trim leading whitespace if($tracer_enabled) {
$_tracer->complete($start * 1000000, $dur * 1000000, "DB Query", ["query"=>$query, "args"=>$args, "method"=>$method]); $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->query_count++;
$this->dbtime += $dur; $this->dbtime += $dur;
} }

View file

@ -108,6 +108,8 @@ $_shm_event_count = 0;
*/ */
function send_event(Event $event): void function send_event(Event $event): void
{ {
global $tracer_enabled;
global $_shm_event_listeners, $_shm_event_count, $_tracer; global $_shm_event_listeners, $_shm_event_count, $_tracer;
if (!isset($_shm_event_listeners[get_class($event)])) { if (!isset($_shm_event_listeners[get_class($event)])) {
return; return;
@ -116,8 +118,6 @@ function send_event(Event $event): void
// send_event() is performance sensitive, and with the number // send_event() is performance sensitive, and with the number
// of times tracer gets called the time starts to add up // of times tracer gets called the time starts to add up
$tracer_enabled = constant('TRACE_FILE');
if ($tracer_enabled) $_tracer->begin(get_class($event)); if ($tracer_enabled) $_tracer->begin(get_class($event));
// SHIT: http://bugs.php.net/bug.php?id=35106 // SHIT: http://bugs.php.net/bug.php?id=35106
$my_event_listeners = $_shm_event_listeners[get_class($event)]; $my_event_listeners = $_shm_event_listeners[get_class($event)];