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 32 commits into
base: 2.x
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
32 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
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
Next Next commit
Replaced the 'sandbox' approach with a 'snapshot' approach.
  • Loading branch information
a.stecher committed May 13, 2024
commit 5d2c26de8ffd9f3e33167fde42910832cdcd5440
31 changes: 31 additions & 0 deletions src/ApplicationSnapshot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Laravel\Octane;

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

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();
}

}
24 changes: 0 additions & 24 deletions src/CurrentApplication.php

This file was deleted.

95 changes: 47 additions & 48 deletions src/Worker.php
Original file line number Diff line number Diff line change
@@ -27,16 +27,24 @@ class Worker implements WorkerContract
protected $requestHandledCallbacks = [];

/**
* The root application instance.
* The current application instance.
*
* @var \Illuminate\Foundation\Application
* @var Application
*/
protected $app;

/**
* A clone of the warmed up initial application
*
* @var ApplicationSnapshot
*/
protected $applicationSnapshot;

public function __construct(
protected ApplicationFactory $appFactory,
protected Client $client
) {
protected Client $client
)
{
}

/**
@@ -72,9 +80,9 @@ public function handle(Request $request, RequestContext $context): void
// We will clone the application instance so that we have a clean copy to switch
// back to once the request has been handled. This allows us to easily delete
// certain instances that got resolved / mutated during a previous request.
CurrentApplication::set($sandbox = clone $this->app);
$this->createApplicationSnapshot();

$gateway = new ApplicationGateway($this->app, $sandbox);
$gateway = new ApplicationGateway($this->app, $this->app);

try {
$responded = false;
@@ -97,30 +105,23 @@ public function handle(Request $request, RequestContext $context): void

$responded = true;

$this->invokeRequestHandledCallbacks($request, $response, $sandbox);
$this->invokeRequestHandledCallbacks($request, $response, $this->app);

$gateway->terminate($request, $response);
} catch (Throwable $e) {
$this->handleWorkerError($e, $sandbox, $request, $context, $responded);
$this->handleWorkerError($e, $this->app, $request, $context, $responded);
} finally {
$sandbox->flush();

$this->app->make('view.engine.resolver')->forget('blade');
$this->app->make('view.engine.resolver')->forget('php');

// After the request handling process has completed we will unset some variables
// plus reset the current application state back to its original state before
// it was cloned. Then we will be ready for the next worker iteration loop.
unset($gateway, $sandbox, $request, $response, $octaneResponse, $output);

CurrentApplication::set($this->app);
unset($gateway, $request, $response, $octaneResponse, $output);
}
}

/**
* Handle an incoming task.
*
* @param mixed $data
* @param mixed $data
* @return mixed
*/
public function handleTask($data)
@@ -130,27 +131,20 @@ public function handleTask($data)
// We will clone the application instance so that we have a clean copy to switch
// back to once the request has been handled. This allows us to easily delete
// certain instances that got resolved / mutated during a previous request.
CurrentApplication::set($sandbox = clone $this->app);
$this->createApplicationSnapshot();

try {
$this->dispatchEvent($sandbox, new TaskReceived($this->app, $sandbox, $data));
$this->dispatchEvent($this->app, new TaskReceived($this->app, $this->app, $data));

$result = $data();

$this->dispatchEvent($sandbox, new TaskTerminated($this->app, $sandbox, $data, $result));
$this->dispatchEvent($this->app, new TaskTerminated($this->app, $this->app, $data, $result));
} catch (Throwable $e) {
$this->dispatchEvent($sandbox, new WorkerErrorOccurred($e, $sandbox));
$this->dispatchEvent($this->app, new WorkerErrorOccurred($e, $this->app));

return TaskExceptionResult::from($e);
} finally {
$sandbox->flush();

// After the request handling process has completed we will unset some variables
// plus reset the current application state back to its original state before
// it was cloned. Then we will be ready for the next worker iteration loop.
unset($sandbox);

CurrentApplication::set($this->app);
$this->app->flush();
}

return new TaskResult($result);
@@ -161,33 +155,30 @@ public function handleTask($data)
*/
public function handleTick(): void
{
CurrentApplication::set($sandbox = clone $this->app);
$this->createApplicationSnapshot();

try {
$this->dispatchEvent($sandbox, new TickReceived($this->app, $sandbox));
$this->dispatchEvent($sandbox, new TickTerminated($this->app, $sandbox));
$this->dispatchEvent($this->app, new TickReceived($this->app, $this->app));
$this->dispatchEvent($this->app, new TickTerminated($this->app, $this->app));
} catch (Throwable $e) {
$this->dispatchEvent($sandbox, new WorkerErrorOccurred($e, $sandbox));
$this->dispatchEvent($this->app, new WorkerErrorOccurred($e, $this->app));
} finally {
$sandbox->flush();

unset($sandbox);

CurrentApplication::set($this->app);
$this->app->flush();
}
}

/**
* Handle an uncaught exception from the worker.
*/
protected function handleWorkerError(
Throwable $e,
Application $app,
Request $request,
Throwable $e,
Application $app,
Request $request,
RequestContext $context,
bool $hasResponded
): void {
if (! $hasResponded) {
bool $hasResponded
): void
{
if (!$hasResponded) {
$this->client->error($e, $app, $request, $context);
}

@@ -197,9 +188,9 @@ protected function handleWorkerError(
/**
* Invoke the request handled callbacks.
*
* @param \Illuminate\Http\Request $request
* @param \Symfony\Component\HttpFoundation\Response $response
* @param \Illuminate\Foundation\Application $sandbox
* @param \Illuminate\Http\Request $request
* @param \Symfony\Component\HttpFoundation\Response $response
* @param \Illuminate\Foundation\Application $sandbox
*/
protected function invokeRequestHandledCallbacks($request, $response, $sandbox): void
{
@@ -225,7 +216,7 @@ public function onRequestHandled(Closure $callback)
*/
public function application(): Application
{
if (! $this->app) {
if (!$this->app) {
throw new RuntimeException('Worker has not booted. Unable to access application.');
}

@@ -239,4 +230,12 @@ public function terminate(): void
{
$this->dispatchEvent($this->app, new WorkerStopping($this->app));
}

protected function createApplicationSnapshot(): void
{
if (!isset($this->applicationSnapshot)) {
$this->applicationSnapshot = ApplicationSnapshot::createSnapshotFrom($this->app);
}
$this->applicationSnapshot->loadSnapshotInto($this->app);
}
}