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

feat(sdk): optimize cold-start performance #320

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions .changeset/sharp-cameras-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@plutolang/pluto-infra": patch
---

feat(sdk): optimize cold-start performance

Move import statements to container initialization to enhance runtime performance.

By shifting import statements to container initialization rather than at invocation time, we ensure that the import process is completed with more resources available, leading to improved cold-start performance.
6 changes: 3 additions & 3 deletions examples/quickstart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
"destroy": "pluto destroy"
},
"dependencies": {
"@plutolang/pluto": "latest"
"@plutolang/pluto": "workspace:*"
},
"devDependencies": {
"@types/node": "^20",
"typescript": "^5.2.2",
"@plutolang/base": "latest",
"@plutolang/pluto-infra": "latest",
"@plutolang/base": "workspace:*",
"@plutolang/pluto-infra": "workspace:*",
"@pulumi/pulumi": "^3.88.0"
},
"main": "dist/index.js"
Expand Down
20 changes: 12 additions & 8 deletions packages/pluto-infra/src/utils/closure-serializing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ function modifyEntrypointFile(entrypointFilePath: string, closure: ComputeClosur
// replaces the placeholder "__handler_: undefined" in the entrypoint file with "__handler_:
// __handler_".
//
// We postpone the require statement by placing it within an asynchronous function. This approach
// is necessary because the module being imported might need information from the environment,
// which is initialized in the platform adaptation function, such as the AWS_ACCOUNT_ID.
// We postpone the require statement outside the function to ensure that the import process
// completes during container initialization.
const uniqueFnName = `handler_${path.basename(closureDirpath)}`;
const userClosureImportStat = `
const ${uniqueFnName} = require("./${path.basename(closureDirpath)}").default;
var __handler_ = async (...args) => {
const handler = require("./${path.basename(closureDirpath)}").default;
return await handler(...args);
return await ${uniqueFnName}(...args);
};\n`;
const entrypointFileContent = fs
.readFileSync(entrypointFilePath, "utf-8")
Expand Down Expand Up @@ -225,13 +225,17 @@ export async function dumpClosureToDir_python(
throw new Error(`This closure has a placeholder but no child closure.`);
}

// Append the closure import snippet to the end of the entrypoint file. Place the import
// statement outside the function to ensure the import process completes during container
// initialization.
const uniqueFnName = `__child_${depth}_${child.exportName}`;
const closureImportStat = `
from child_${depth} import ${child.exportName} as ${uniqueFnName}
def ${closure.placeholder}(*args, **kwargs):
from child_${depth} import ${child.exportName}
return ${child.exportName}(*args, **kwargs)
return ${uniqueFnName}(*args, **kwargs)
`;
const content = fs.readFileSync(entrypoint, "utf-8");
fs.writeFileSync(entrypoint, closureImportStat + content);
fs.writeFileSync(entrypoint, content + "\n\n" + closureImportStat);
}

return entrypoint;
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading