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 Sep 6, 2024
1 parent aa815c8 commit 8fb920f
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/DebugBar/DebugBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,25 @@ public function collect()
)
);

// The RequestDataCollector stores a copy of the session data and the
// session stores the collected data when it gets stacked. This leads
// to exponential memory usage growth if multiple requests have
// their data stacked before the stacked data is rendered. We can avoid
// that by temporarily removing the stacked data from the session.
$http = $this->getHttpDriver();
if ($http->isSessionStarted()) {
$stack = $http->getSessionValue($this->stackSessionNamespace);
$http->deleteSessionValue($this->stackSessionNamespace);
}

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

if ($http->isSessionStarted()) {
$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 8fb920f

Please sign in to comment.