PGO (Profile Guided Optimization) is a technique that optimizes based on runtime profiling data. It enhances the startup time of Node.js applications by several factors through the following two aspects:
When using require
to import a module a
in one file, it goes through a series of path resolutions to obtain the absolute path of the a
module file. Similarly, when require
-ing the same module a
in another file, the resolved absolute path may differ. PGO maps the results of various require
calls across different files to establish a two-dimensional map of relationships. With this relationship data, the require
function is modified to include a logic that checks the mapping before the path resolution. If a corresponding relationship is found in the map, it directly returns the associated content; otherwise, it falls back to the original path resolution logic, thereby achieving acceleration.
Repeatedly checking for file existence and repeatedly reading fragmented files are common operations in the require
logic. PGO's Require Cache
not only stores the aforementioned relationships but also:
- Textual information of the source files.
- V8 byte code compiled from the source files.
All this information is structured and stored in a cache file. By loading this cache file at process startup, there is no need for any deserialization step, enabling direct use of the map.
With this file, we only need to load the cache file once at the beginning of the process. Then, each time a module is require
-d, it directly retrieves the corresponding file from the cache, fetches the source code text and its byte code from the cache, and loads them directly.
By relying on this mechanism, we save:
- Path resolution time (repeated
statx
, more complex encapsulation logic in Node.js); - File reading time (repeated
openat
, more complex encapsulation logic in Node.js); - Compilation and execution of source code text reduced to byte code compilation and execution.
PGO is currently integrated with Serverless Devs. It can be directly used through Serverless Devs' s cli
.
- Add
pre-deploy
in theservice actions
ofs.yaml
, and configure therun
command tos cli pgo
, as shown in the image below.
-
Change the
runtime
ins.yaml
tonodejs14
. -
Deploy the function.
s deploy
- Invoke function
s cli fc-api invokeFunction --serviceName fctest --functionName functest1 --event '{}'
Parameters can be passed using s cli pgo gen --parameter key parameter value
.
remove-nm
: Automatically removesnode_modules
after PGO build,s cli pgo gen --remove-nm
.
Alibaba Node.js Architecture