Skip to content

故障排除

¥Troubleshooting

性能

¥Performance

性能是 Rolldown 的首要目标。但是,构建性能并非完全由 Rolldown 本身决定。它也受到其运行环境和所用插件的显著影响。

¥Performance is a primary goal for Rolldown. However, build performance isn't solely determined by Rolldown itself. It's also significantly affected by the environment it runs in and the plugins used.

虽然我们不断努力改进 Rolldown 以最大限度地减少这些外部因素,但它仍然存在固有的局限性和仍在进行优化的字段。本指南深入分析了潜在的瓶颈以及如何缓解它们。

¥While we continuously strive to improve Rolldown to minimize these external factors, there are inherent limitations and areas where optimizations are still ongoing. This guide provides insights into potential bottlenecks and how you can mitigate them.

环境

¥Environment

操作系统及其配置会影响构建时间,尤其是文件系统操作。

¥The operating system and its configuration can impact build times, particularly file system operations.

Windows

与其他操作系统(如 macOS 或 Linux)相比,Windows 上的文件系统访问通常较慢。尤其是防病毒软件会使情况更加糟糕。但即使没有杀毒软件的干扰,基准文件系统性能也往往会变慢。它比 macOS 慢 3 倍,比 Linux 慢 10 倍。当大多数转换都在没有插件的情况下完成时,这会成为瓶颈。

¥File system access on Windows is generally slower compared to other operating systems like macOS or Linux. Especially, antivirus software can make this much worse. But even without interference from antivirus programs, baseline file system performance tends to be slower. It is 3 times slower than macOS and 10 times slower than Linux. This becomes a bottleneck when most of the transforms are done without a plugin.

要提高 Windows 上的性能,请考虑使用其他文件系统环境:

¥To improve performance on Windows, consider using alternative file system environments:

  1. Dev Drive:一项针对开发者工作负载设计的较新的 Windows 功能,使用弹性文件系统 (ReFS​​)。与标准 Windows NTFS 文件系统相比,使用 Dev Drive 进行文件系统操作可以将速度提高 2 到 3 倍。

    ¥Dev Drive: A newer Windows feature designed for developer workloads, using the Resilient File System (ReFS). Using a Dev Drive can lead to a 2x to 3x speedup compared to the standard Windows NTFS file system for file system operations.

  2. Windows Linux 子系统 (WSL):WSL 允许 Linux 环境在 Windows 上轻松运行,从而显著提升文件系统性能。与使用标准 Windows NTFS 文件系统进行文件系统操作相比,在 WSL 中放置项目文件并运行构建过程可使速度提升约 10 倍。

    ¥Windows Subsystem for Linux (WSL): WSL lets Linux environment to run on Windows easily, which offers significantly better file system performance. Placing your project files and running the build process within WSL can result in speedups of around 10x compared to the standard Windows NTFS file system for file system operations.

基准参考

使用的基准测试脚本在此博客文章 (你能多快打开 1000 个文件?) 中描述。

¥The benchmark script used is described in this blog post (How fast can you open 1000 files?).

结果如下:

¥The results were:

文件系统/线程124816
Windows NTFS286 毫秒153 毫秒85 毫秒106 毫秒110 毫秒
Windows 开发驱动器 (ReFS​​)124 毫秒67 毫秒35 毫秒48 毫秒55 毫秒
WSL (ext4)24 毫秒13 毫秒7.8ms9.0ms13 毫秒

基准测试在以下环境中运行:

¥The benchmark was ran on the following environment:

  • OS:Windows 11 Pro 23H2 22631.5189

  • CPU:AMD Ryzen 9 5900X

  • 内存:DDR4-3600 32GB

    ¥Memory: DDR4-3600 32GB

  • SSD:Western Digital Black SN850X 1TB

插件

¥Plugins

插件扩展了 Rolldown 的功能,但也会带来性能开销。

¥Plugins extend Rolldown's functionality, but can also introduce performance overhead.

插件钩子过滤器

¥Plugin Hook Filters

Rolldown 提供了一项名为“插件钩子过滤器”的功能。这允许你精确指定插件钩子应该处理哪些模块,从而减少 JavaScript 和 Rust 之间的通信开销。有关过滤器内部工作原理的详细信息,请参阅 插件开发指南 - Hook 过滤器

¥Rolldown provides a feature called Plugin Hook Filters. This allows you to specify precisely which modules a plugin hook should process, reducing the communication overhead between JavaScript and Rust. For detailed information on how filters work internally, refer to the Plugin Development Guide - Hook Filters.

如果你是插件用户,并且你使用的插件未指定钩子过滤器,你可以使用 Rolldown 导出的 withFilter 实用程序函数来应用它们。

¥If you are a plugin user and the plugin you use does not have hook filters specified, you can apply them by using the withFilter utility function exported by Rolldown.

js
import yaml from '@rollup/plugin-yaml';
import { defineConfig } from 'rolldown';
import { withFilter } from 'rolldown/filter';

export default defineConfig({
  plugins: [
    // Run the transform hook of the `yaml` plugin only for modules which end in `.yaml`
    withFilter(
      yaml({
        /*...*/
      }),
      { transform: { id: /\.yaml$/ } },
    ),
  ],
});

利用内置功能

¥Leverage Built-in Features

Rolldown 包含几个旨在提高效率的内置功能。尽可能优先使用这些原生功能,而不是执行类似任务的外部 Rollup 插件。依赖内置功能通常意味着处理完全在 Rust 内部进行,从而允许并行处理。

¥Rolldown includes several built-in features designed for efficiency. Where possible, prefer using these native capabilities over external Rollup plugins that perform similar tasks. Relying on built-in functionality often means the processing happens entirely within Rust, allowing to process in parallel.

查看 Rolldown 功能 页面,了解 Rollup 中不存在的功能。

¥Check the Rolldown Features page for capabilities that does not exist in Rollup.

例如,以下常见的 Rollup 插件可以用 Rolldown 的内置功能替换:

¥For example, the following common Rollup plugins may be replaced with Rolldown's built-in features:

  • @rollup/plugin-aliasresolve.alias 选项

    ¥@rollup/plugin-alias: resolve.alias option

  • @rollup/plugin-commonjs:开箱即用

    ¥@rollup/plugin-commonjs: supported out of the box

  • @rollup/plugin-injectinject 选项

    ¥@rollup/plugin-inject: inject option

  • @rollup/plugin-replacereplacePlugin

  • @rollup/plugin-node-resolve:开箱即用

    ¥@rollup/plugin-node-resolve: supported out of the box

  • @rollup/plugin-json:开箱即用

    ¥@rollup/plugin-json: supported out of the box

  • @rollup/plugin-swc, @rollup/plugin-babel, @rollup/plugin-sucrase:通过 Oxc 开箱即用(复杂配置可能仍需要插件)

    ¥@rollup/plugin-swc, @rollup/plugin-babel, @rollup/plugin-sucrase: supported out of the box via Oxc (complex configurations might still require the plugin)

  • @rollup/plugin-terseroutput.minify 选项

    ¥@rollup/plugin-terser: output.minify option