-
Notifications
You must be signed in to change notification settings - Fork 1k
chore(functions): add lazy global var details #1302
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ function _lightComputation(): int | |
} | ||
|
||
// [START functions_tips_scopes] | ||
// [START functions_tips_lazy_globals] | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
|
@@ -41,6 +42,11 @@ function scopeDemo(ServerRequestInterface $request): string | |
$cachePath = sys_get_temp_dir() . '/cached_value.txt'; | ||
|
||
$response = ''; | ||
|
||
// Because the PHP runtime does NOT cache global variables between | ||
// invocations, there is no benefit to "greedily" initializing them | ||
// in global scope. Thus, we ALWAYS initialize them "lazily" within | ||
// the function itself | ||
Comment on lines
+47
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This guidance isn't quite right. The problem is not the lack of persisting global variables in this case, if that were the case the code below would work. The problem is that when the container starts, PHP code doesn't execute until a request comes in that loads the file. Moving forward, we should keep in mind the larger goal of these samples: it's not about working with global variables, it's about using the language facility available to balance the amount of work done on cold start vs. function execution. Future: We should research https://www.php.net/manual/en/opcache.preloading.php as a suggestion of this sample or a default configuration of the FF. In theory, if we set preload to the index.php file, we do get globals that can persist across requests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (file_exists($cachePath)) { | ||
// Read cached value from file | ||
$response .= "Reading cached value." . PHP_EOL; | ||
|
@@ -61,4 +67,5 @@ function scopeDemo(ServerRequestInterface $request): string | |
return $response; | ||
} | ||
|
||
// [END functions_tips_lazy_globals] | ||
// [END functions_tips_scopes] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sample is shown in the docs in adjacent sections of the same doc, I don't think it makes a lot of sense to reuse across both. I think either we don't need this snippet or it needs to be separate for clarity.