Skip to content

Commit

Permalink
Reduce memory usage for consecutive requests with stacked data
Browse files Browse the repository at this point in the history
Currently, the session stores stacked data, which in turn might contain
the session data if a RequestCollector is active. This leads to
exponential growth in memory usage and rapid performance degradation if
multiple requests add stacked data before the next time the debug bar is
rendered.

We can avoid this be temporarily removing the stacked data from the
session while collection data.
  • Loading branch information
dotdash committed Aug 21, 2024
1 parent aa815c8 commit 0875abb
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/DebugBar/DebugBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,21 @@ public function collect()
)
);

// With the RequestDataCollector stores the session data and the
// session store the collected data in form of stacked data, which
// leads to exponential memory usage growth if multiple requests have
// their data stacked. Avoid that by temporarily removing the stacked
// data from the session.
$http = $this->initStackSession();
$stack = $http->getSessionValue($this->stackSessionNamespace);
$http->deleteSessionValue($this->stackSessionNamespace);

foreach ($this->collectors as $name => $collector) {
$this->data[$name] = $collector->collect();
}

$stack = $http->setSessionValue($this->stackSessionNamespace, $stack);

// Remove all invalid (non UTF-8) characters
array_walk_recursive($this->data, function (&$item) {
if (is_string($item) && !mb_check_encoding($item, 'UTF-8')) {
Expand Down

0 comments on commit 0875abb

Please sign in to comment.