-
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
Conversation
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
// invocations, there is no benefit to "greedily" initializing them | ||
// in global scope. Thus, we ALWAYS initialize them "lazily" within | ||
// the function itself |
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 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 comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -26,6 +26,7 @@ function _lightComputation(): int | |||
} | |||
|
|||
// [START functions_tips_scopes] | |||
// [START functions_tips_lazy_globals] |
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.
@grayside and I decided to skip this sample, given that it doesn't make sense for PHP. (It's other-language counterparts aren't used in the docs either, so they should probably be deleted.) |
Note: for the "lazy globals" section, I plan to highlight the comment I'm adding here within the sample.
Do not merge until we figure out how to handle preloading (see #1304).