From 756f0d62f4371be3ea2f62f59cf1b7ef7a2dea59 Mon Sep 17 00:00:00 2001 From: viest Date: Sat, 26 Feb 2022 00:20:28 +0800 Subject: [PATCH 1/2] Feat flush view data The view data keeps appending causing memory to increase. For example. There is a timed task to send emails, I send 100 emails with different data, the view data length will be 100, the data volume is large but the memory is relatively small, it can easily lead to killed. ```php for ($index = 0; $index < 100; $index++) { /* @var Factory $viewFactor */ $viewFactor = Mail::getViewFactory(); $view = $viewFactor->make('email.view', [ // ...... ]); // get html $data = $view->render(); /* @var CompilerEngine $viewEngine */ $viewEngine = $view->getEngine(); $viewEngine->flushViewData(); // send mail via raw data // ...... } ``` --- src/Views/Concerns/CollectsViewExceptions.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Views/Concerns/CollectsViewExceptions.php b/src/Views/Concerns/CollectsViewExceptions.php index e064842b..4cf31860 100644 --- a/src/Views/Concerns/CollectsViewExceptions.php +++ b/src/Views/Concerns/CollectsViewExceptions.php @@ -18,6 +18,11 @@ public function collectViewData($path, array $data): void 'data' => $this->filterViewData($data), ]; } + + public function flushViewData(): void + { + $this->lastCompiledData = []; + } public function filterViewData(array $data): array { From 169041261f894f611b948fd5ba21e7169bfda5e8 Mon Sep 17 00:00:00 2001 From: viest Date: Mon, 28 Feb 2022 10:12:51 +0800 Subject: [PATCH 2/2] Update CollectsViewExceptions.php --- src/Views/Concerns/CollectsViewExceptions.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Views/Concerns/CollectsViewExceptions.php b/src/Views/Concerns/CollectsViewExceptions.php index 4cf31860..22b6773a 100644 --- a/src/Views/Concerns/CollectsViewExceptions.php +++ b/src/Views/Concerns/CollectsViewExceptions.php @@ -21,6 +21,9 @@ public function collectViewData($path, array $data): void public function flushViewData(): void { + // If we need to render multiple views at the same time, + // $lastCompiledData will hold all the view data, which can lead to excessive memory usage. + // We can flush the view data so that we can prevent memory transitions and also improve retrieval efficiency. $this->lastCompiledData = []; }