Skip to content

Commit

Permalink
Merge #11481
Browse files Browse the repository at this point in the history
11481: Closure emit requires in global scope, to have them run in Lambda init phase r=RobbieMcKinstry a=pjoe

<!--- 
Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation.
-->

# Description

Emit `require` for importing modules in global scope.
This allows these to run during the init phase of AWS Lambda, where more CPU is available, resulting in greatly imrpoved cold start performance (often 5x or more).

Fixes #11468

## Checklist

<!--- Please provide details if the checkbox below is to be left unchecked. -->
- [x] I have added tests that prove my fix is effective or that my feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the Pulumi Service,
then the service should honor older versions of the CLI where this change would not exist.
You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Service API version
  <!-- `@Pulumi` employees: If yes, you must submit corresponding changes in the service repo. -->


Co-authored-by: Pelle Johnsen <pelle.johnsen@gmail.com>
  • Loading branch information
bors[bot] and pjoe committed Dec 9, 2022
2 parents 43b03d6 + 6483001 commit 78bd0eb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
@@ -0,0 +1,4 @@
changes:
- type: feat
scope: sdk/nodejs
description: Emit closure requires in global scope for improved cold start on Lambda
11 changes: 11 additions & 0 deletions sdk/nodejs/runtime/closure/serializeClosure.ts
Expand Up @@ -151,6 +151,7 @@ function serializeJavaScriptText(

let environmentText = "";
let functionText = "";
const emittedRequires = new Set<string>();

const outerFunctionName = emitFunctionAndGetName(outerClosure.func);

Expand Down Expand Up @@ -202,6 +203,16 @@ function serializeJavaScriptText(

const parameters = [...Array(functionInfo.paramCount)].map((_, index) => `__${index}`).join(", ");

for (const [keyEntry, { entry: valEntry }] of functionInfo.capturedValues) {
if (valEntry.module !== undefined) {
if(!emittedRequires.has(keyEntry.json)) {
emittedRequires.add(keyEntry.json);
functionText += `const ${keyEntry.json} = require("${valEntry.module}");\n`;
}
delete capturedValues[keyEntry.json];
}
}

functionText += "\n" +
"function " + varName + "(" + parameters + ") {\n" +
" return (function() {\n" +
Expand Down
12 changes: 8 additions & 4 deletions sdk/nodejs/tests/runtime/tsClosureCases.ts
Expand Up @@ -859,10 +859,11 @@ return () => { let x = eval("undefined + null + NaN + Infinity + __filename"); r
title: "Capture built in module by ref",
func: () => os,
expectText: `exports.handler = __f0;
const os = require("os");
function __f0() {
return (function() {
with({ os: require("os") }) {
with({ }) {
return () => os;
Expand All @@ -886,10 +887,11 @@ return () => os;
return { v };
},
expectText: `exports.handler = __f0;
const os = require("os");
function __f0(__0, __1, __2) {
return (function() {
with({ os: require("os") }) {
with({ }) {
return (a, b, c) => {
const v = os;
Expand All @@ -915,10 +917,11 @@ return (a, b, c) => {
title: "Capture module through indirect function references",
func: func,
expectText: `exports.handler = __f0;
const os = require("os");
function __f1() {
return (function() {
with({ os: require("os") }) {
with({ }) {
return () => os;
Expand Down Expand Up @@ -6645,10 +6648,11 @@ return function (thisArg, _arguments, P, generator) {
}
}).apply(undefined, undefined).apply(this, arguments);
}
const mockpackage_1 = require("mockpackage");
function __f1() {
return (function() {
with({ mockpackage_1: require("mockpackage") }) {
with({ }) {
return () => mockpackage_1.z.object({
message: mockpackage_1.z.string(),
Expand Down

0 comments on commit 78bd0eb

Please sign in to comment.