Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance: adds application snapshot and minimizes App::make #980

Open
wants to merge 46 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
5d2c26d
Replaced the 'sandbox' approach with a 'snapshot' approach.
May 13, 2024
72ad7b4
Adjusted the unit tests to reflect the new approach.
May 13, 2024
567100a
Removes some unnecessary event listeners.
May 13, 2024
addacbd
Adds more snapshot tests.
May 13, 2024
2de2e7b
Cleans up redundant code.
May 13, 2024
1af885d
Fixes style ci issues.
May 13, 2024
ad92af6
Fixes style ci issues.
May 13, 2024
8bd1193
Adds test for scoped container instances.
May 14, 2024
d2a2092
Gives event listeners access to the app snapshot.
May 14, 2024
24fc7c4
Adjusted method names.
May 14, 2024
ca23364
Fixes style ci issue.
May 14, 2024
d95ced3
Reverts deleting notification manager listener.
May 15, 2024
616bc37
Registers the listener and adds a test to ensure drivers are flushed.
May 15, 2024
fb89581
Fixes style ci.
May 15, 2024
e499184
Fixes style ci.
May 15, 2024
0290363
Fixes style ci.
May 15, 2024
8940e19
Adds a test to make sure all Application and Container properties can…
May 16, 2024
9b69cd7
Fixes style ci.
May 16, 2024
ef0a0d3
Merge branch 'refs/heads/2.x' into better_approach_to_memory_leaks_v2
Jun 30, 2024
d1af9d4
Testing.
Sep 8, 2024
a241248
Merge branch '2.x' into better_approach_to_memory_leaks_v2
Jan 3, 2025
e0e05c1
App resetter experimentation.
Jan 5, 2025
924fd9d
Cleans up reset logic.
Jan 5, 2025
395a7ff
Ignores line endings in tests.
Jan 5, 2025
8056951
Adds 'forget view engine' test.
Jan 5, 2025
943f25a
Laravel pint.
Jan 5, 2025
4687d48
Fixes str cache test with new Laravel version.
Jan 5, 2025
deb1411
Merge branch '2.x' into perf/application-snapshot
Jan 5, 2025
46a95c5
style.ci
Jan 5, 2025
599b853
style.ci
Jan 5, 2025
0307f79
Adds notice.
Jan 5, 2025
fd39022
Renames variables.
Jan 6, 2025
98265ce
Adjusts naming.
Jan 17, 2025
a263b30
Adds custom OctaneEventListener.
Feb 9, 2025
7464531
pint
Feb 9, 2025
063d76f
pint
Feb 9, 2025
3795538
naming
Feb 9, 2025
91d456d
removes pint
Feb 9, 2025
533f6cf
Adds comment.
Feb 9, 2025
f5ec121
Resets naming
Feb 9, 2025
cee272f
Adds listener tests.
Feb 9, 2025
0d317cf
formatting.
Feb 9, 2025
46107e6
styleci
Feb 9, 2025
2e995b0
styleci
Feb 9, 2025
56996a7
Remembers the log default driver
Feb 9, 2025
7fd23e0
Removes TODO
Feb 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/ApplicationGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

class ApplicationGateway
{
use DispatchesEvents;

public function __construct(protected Application $app, protected Application $sandbox)
public function __construct(protected OctaneEventDispatcher $dispatcher, protected Application $snapshot, protected Application $sandbox)
{
}

Expand All @@ -27,14 +25,14 @@ public function handle(Request $request): Response
{
$request->enableHttpMethodParameterOverride();

$this->dispatchEvent($this->sandbox, new RequestReceived($this->app, $this->sandbox, $request));
$this->dispatcher->dispatchEvent(new RequestReceived($this->snapshot, $this->sandbox, $request));

if (Octane::hasRouteFor($request->getMethod(), '/'.$request->path())) {
return Octane::invokeRoute($request, $request->getMethod(), '/'.$request->path());
}

return tap($this->sandbox->make(Kernel::class)->handle($request), function ($response) use ($request) {
$this->dispatchEvent($this->sandbox, new RequestHandled($this->sandbox, $request, $response));
return tap($this->snapshot->initialInstance(Kernel::class)->handle($request), function ($response) use ($request) {
$this->dispatcher->dispatchEvent(new RequestHandled($this->sandbox, $request, $response));
});
}

Expand All @@ -43,9 +41,9 @@ public function handle(Request $request): Response
*/
public function terminate(Request $request, Response $response): void
{
$this->sandbox->make(Kernel::class)->terminate($request, $response);
$this->snapshot->initialInstance(Kernel::class)->terminate($request, $response);

$this->dispatchEvent($this->sandbox, new RequestTerminated($this->app, $this->sandbox, $request, $response));
$this->dispatcher->dispatchEvent(new RequestTerminated($this->snapshot, $this->sandbox, $request, $response));

$route = $request->route();

Expand Down
43 changes: 43 additions & 0 deletions src/ApplicationSnapshot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Laravel\Octane;

use Illuminate\Container\Container;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Facade;

// this class creates a snapshot of the app container at a specific point in time.
// We can later reset all properties of the app to those of the snapshot
// This allows 'cloning' the original app without having to create a new instance
class ApplicationSnapshot extends Application
{
public static function createSnapshotFrom(Application $app): ApplicationSnapshot
{
$previousInstance = Container::getInstance();
$snapshot = new ApplicationSnapshot;
foreach (get_object_vars($app) as $key => $value) {
$snapshot->$key = $value;
}
Container::setInstance($previousInstance);

return $snapshot;
}

public function loadSnapshotInto(Application $app): void
{
foreach (get_object_vars($this) as $key => $value) {
$app->$key = $value;
}
Facade::clearResolvedInstances();
}

// fast access to an original app instance
public function initialInstance(string $abstract)
{
if (! array_key_exists($abstract, $this->resolved)) {
return null;
}

return $this->instances[$abstract] ?? $this->bindings[$abstract]['concrete']();
}
}
15 changes: 1 addition & 14 deletions src/Concerns/ProvidesDefaultConfigurationOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static function prepareApplicationForNextRequest(): array
\Laravel\Octane\Listeners\EnforceRequestScheme::class,
\Laravel\Octane\Listeners\EnsureRequestServerPortMatchesScheme::class,
\Laravel\Octane\Listeners\GiveNewRequestInstanceToApplication::class,
\Laravel\Octane\Listeners\GiveNewRequestInstanceToPaginator::class,
\Laravel\Octane\Listeners\ForgetViewEngines::class,
];
}

Expand All @@ -29,22 +29,9 @@ public static function prepareApplicationForNextOperation(): array
return [
\Laravel\Octane\Listeners\CreateConfigurationSandbox::class,
\Laravel\Octane\Listeners\CreateUrlGeneratorSandbox::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToAuthorizationGate::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToBroadcastManager::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToDatabaseManager::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToDatabaseSessionHandler::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToFilesystemManager::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToHttpKernel::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToLogManager::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToMailManager::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToNotificationChannelManager::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToPipelineHub::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToCacheManager::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToSessionManager::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToQueueManager::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToRouter::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToValidationFactory::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToViewFactory::class,
\Laravel\Octane\Listeners\FlushDatabaseRecordModificationState::class,
\Laravel\Octane\Listeners\FlushDatabaseQueryLog::class,
\Laravel\Octane\Listeners\RefreshQueryDurationHandling::class,
Expand Down
24 changes: 0 additions & 24 deletions src/CurrentApplication.php

This file was deleted.

21 changes: 0 additions & 21 deletions src/DispatchesEvents.php

This file was deleted.

13 changes: 9 additions & 4 deletions src/Listeners/CreateConfigurationSandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
class CreateConfigurationSandbox
{
/**
* Handle the event.
*
* @param mixed $event
* @var \Illuminate\Config\Repository
*/
private $config;

public function __construct()
{
$this->config = app('config');
}

public function handle($event): void
{
$event->sandbox->instance('config', clone $event->sandbox['config']);
$event->sandbox->instance('config', clone $this->config);
}
}
14 changes: 13 additions & 1 deletion src/Listeners/CreateUrlGeneratorSandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@

namespace Laravel\Octane\Listeners;

use Illuminate\Routing\UrlGenerator;

class CreateUrlGeneratorSandbox
{
/**
* @var UrlGenerator
*/
private $url;

public function __construct()
{
$this->url = app('url');
}

/**
* Handle the event.
*
* @param mixed $event
*/
public function handle($event): void
{
$event->sandbox->instance('url', clone $event->sandbox['url']);
$event->sandbox->instance('url', clone $this->url);
}
}
20 changes: 15 additions & 5 deletions src/Listeners/EnforceRequestScheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@

namespace Laravel\Octane\Listeners;

use Illuminate\Routing\UrlGenerator;

class EnforceRequestScheme
{
/**
* Handle the event.
*
* @param mixed $event
* @var UrlGenerator
*/
private $url;

private bool $octaneHttps;

public function __construct()
{
$this->octaneHttps = (bool) config('octane.https');
$this->url = app('url');
}

public function handle($event): void
{
if (! $event->sandbox->make('config')->get('octane.https')) {
if (! $this->octaneHttps) {
return;
}

$event->sandbox->make('url')->forceScheme('https');
$this->url->forceScheme('https');

$event->request->server->set('HTTPS', 'on');
}
Expand Down
5 changes: 0 additions & 5 deletions src/Listeners/EnsureRequestServerPortMatchesScheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

class EnsureRequestServerPortMatchesScheme
{
/**
* Handle the event.
*
* @param mixed $event
*/
public function handle($event): void
{
$port = $event->request->getPort();
Expand Down
16 changes: 14 additions & 2 deletions src/Listeners/FlushArrayCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@

class FlushArrayCache
{
/**
* @var ?\Illuminate\Cache\ArrayStore
*/
private $arrayCache = null;

public function __construct()
{
if (app()->make('config')->get('cache.stores.array')) {
$this->arrayCache = app()->make('cache')->store('array');
}
}

/**
* Handle the event.
*
* @param mixed $event
*/
public function handle($event): void
{
if (config('cache.stores.array')) {
$event->sandbox->make('cache')->store('array')->flush();
if ($this->arrayCache) {
$this->arrayCache->flush();
}
}
}
27 changes: 18 additions & 9 deletions src/Listeners/FlushAuthenticationState.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@

class FlushAuthenticationState
{
/**
* @var ?\Illuminate\Auth\AuthManager
*/
private $auth = null;

public function __construct()
{
if (app()->resolved('auth.driver')) {
app()->forgetInstance('auth.driver');
}

if (app()->resolved('auth')) {
$this->auth = app()->make('auth');
}
}

/**
* Handle the event.
*
* @param mixed $event
*/
public function handle($event): void
{
if ($event->sandbox->resolved('auth.driver')) {
$event->sandbox->forgetInstance('auth.driver');
}

if ($event->sandbox->resolved('auth')) {
with($event->sandbox->make('auth'), function ($auth) use ($event) {
$auth->setApplication($event->sandbox);
$auth->forgetGuards();
});
if ($this->auth) {
$this->auth->forgetGuards();
}
}
}
16 changes: 14 additions & 2 deletions src/Listeners/FlushDatabaseQueryLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@

class FlushDatabaseQueryLog
{
/**
* @var ?\Illuminate\Database\DatabaseManager
*/
private $db = null;

public function __construct()
{
if (app()->resolved('db')) {
$this->db = app()->make('db');
}
}

/**
* Handle the event.
*
* @param mixed $event
*/
public function handle($event): void
{
if (! $event->sandbox->resolved('db')) {
if (! $this->db) {
return;
}

foreach ($event->sandbox->make('db')->getConnections() as $connection) {
foreach ($this->db->getConnections() as $connection) {
$connection->flushQueryLog();
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/Listeners/FlushDatabaseRecordModificationState.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@

class FlushDatabaseRecordModificationState
{
/**
* @var ?\Illuminate\Database\DatabaseManager
*/
private $db = null;

public function __construct()
{
if (app()->resolved('db')) {
$this->db = app()->make('db');
}
}

/**
* Handle the event.
*
* @param mixed $event
*/
public function handle($event): void
{
if (! $event->sandbox->resolved('db')) {
if (! $this->db) {
return;
}

foreach ($event->sandbox->make('db')->getConnections() as $connection) {
foreach ($this->db->getConnections() as $connection) {
$connection->forgetRecordModificationState();
}
}
Expand Down
Loading
Loading