跳到主内容
@wquguru
精选86Cloudflare 博客(RSS)产品发布/更新

Cloudflare发布tsrs:TypeScript的Rust移植版

Introducing tsrs: a Port of TypeScript to Rust

原文
发到 X
推荐理由

AI Agent辅助代码移植与维护的新范式,结合Wasm实现端侧/边缘端开发环境,开发者值得关注。

Cloudflare and VoidZero are now working with Max Schwenk of Owner.com on tsrs, a Rust port of the TypeScript compiler. We are doing this work in close collaboration with the TypeScript team, and incubating tsrs as part of the Oxc JavaScript compiler toolchain.

Cloudflare 和 VoidZero 正与 Owner.com 的 Max Schwenk 合作开发 tsrs,这是 TypeScript 编译器的 Rust 移植版本。我们与 TypeScript 团队密切协作开展此项工作,并将 tsrs 作为 Oxc JavaScript 编译器工具链的一部分进行孵化。

Our goal is to help developers spend less time waiting for their code to be checked and more time building. Making TypeScript faster and more memory-efficient also helps bring complete development environments to more places, including right in your browser.

我们的目标是帮助开发者减少等待代码检查的时间,将更多时间用于构建。使 TypeScript 更快且更节省内存,也有助于将完整的开发环境推广到更多地方,包括直接在浏览器中运行。

To be clear, we are not forking TypeScript. We believe the TypeScript team should remain in full control of the language’s design and direction. They have maintained a long-term vision while guiding TypeScript’s adoption across the JavaScript community. We do not want to change that.

需要明确的是,我们并没有对 TypeScript 进行分叉(fork)。我们相信 TypeScript 团队应继续全面掌控该语言的设计和发展方向。他们在引导 TypeScript 在 JavaScript 社区广泛采用的过程中保持了长期愿景。我们无意改变这一点。

The TypeScript team can move quite fast on many things, but for some features they may spend years intentionally exploring options and making prototypes before picking the right implementation. A Rust port has to follow those decisions closely, or it will eventually fall apart and become incompatible. We have to preserve the TypeScript language strictly based on its original implementation – and the TypeScript team should remain solely responsible for deciding what that looks like.

TypeScript 团队在许多方面进展迅速,但对于某些功能,他们可能会花上数年时间去有意探索各种选项并制作原型,然后再选择正确的实现方式。Rust 移植版本必须紧密跟随这些决策,否则最终会崩溃并与之不兼容。我们必须严格基于原始实现来保留 TypeScript 语言——而 TypeScript 团队应独自负责决定其具体形态。

Performance and memory improvements

性能和内存改进

Early benchmarks of our Rust port of TypeScript are promising: tsrs is up to 8x faster and uses ~70% less memory compared to TypeScript 7.0 when using the same number of type check workers:

我们对 TypeScript 的 Rust 移植版本的早期基准测试前景乐观:在使用相同数量的类型检查工作者时,tsrs 的速度比 TypeScript 7.0 快多达 8 倍,内存使用量减少了约 70%:

As you can see, the next version of TypeScript already brings many performance improvements, making TypeScript 7.1 beta up to 2.5x faster than TypeScript 7.0. Some of these optimizations were upstreamed from tsrs, and we’re hoping to find more.

如您所见,TypeScript 的下一个版本已经带来了许多性能改进,使得 TypeScript 7.1 beta 比 TypeScript 7.0 快多达 2.5 倍。其中一些优化是从 tsrs 反向移植(upstreamed)的,我们希望还能找到更多。

You may have seen recent benchmarks of different TypeScript ports with various performance claims, often due to using larger machine types and different options compared to TypeScript’s defaults. We worked with the TypeScript team to ensure the above benchmarks are comparing the same configuration.

您可能已经看到过关于不同 TypeScript 移植版本的近期基准测试,它们往往声称具有各种性能优势,这通常是因为与 TypeScript 的默认设置相比,使用了更大规格的机器类型和不同的选项。我们与 TypeScript 团队合作,确保上述基准测试是在比较相同的配置。

By default, tsrs ships with an adaptive configuration dependent on the machine type that uses more memory but is even faster:

默认情况下,tsrs 附带一种自适应配置,它依赖于机器类型,会使用更多内存但速度更快:

Try tsrs

尝试 tsrs

TypeScript in Rust aims to be a faithful port of TypeScript. You can try an experimental release by installing the tsrs package from npm:

TypeScript in Rust 旨在成为 TypeScript 的忠实移植版本。您可以通过从 npm 安装 tsrs 包来试用实验性版本:

代码 · 2 行
npm install tsrs
npx tsrs

Why now?

为什么是现在?

We’ve seen many attempts at porting TypeScript to other languages over the years. Because of the complexity involved, only one of the ports succeeded: the TypeScript team’s port of TypeScript from JavaScript to Go.

多年来,我们看到了许多将 TypeScript 移植到其他语言的尝试。由于涉及的复杂性,只有一个移植项目取得了成功:TypeScript 团队将 TypeScript 从 JavaScript 移植到 Go 的版本。

However, frontier AI models can now do large-scale ports almost independently in a matter of days. We have now seen slopforks, faithful ports, and completely re-architected versions of TypeScript in Rust or C++. There is clearly a demand for even faster TypeScript.

然而,前沿的 AI 模型现在几乎可以在几天内独立完成大规模移植。我们现在已经看到了 slopforks、忠实移植版以及在 Rust 或 C++ 中完全重新架构的 TypeScript 版本。显然,人们对更快的 TypeScript 有着明确的需求。

Why Rust?

为什么选择 Rust?

You might be thinking: “TypeScript’s Go port already made TypeScript 10x faster and far more memory efficient than the previous version written in TypeScript. Why isn’t that enough?”

你可能会想:“TypeScript 的 Go 移植版已经使 TypeScript 比之前用 TypeScript 编写的版本快了 10 倍,并且内存效率更高。这还不够吗?”

However, it’s not just about speed. Rust offers some unique advantages: running in WebAssembly. The Rust + Wasm story has always been strong, and one of the reasons comes down to the fundamentals of Rust’s language design. When you compile a Go program to Wasm, you have to compile Go’s garbage collector along with it. But Rust’s memory management model enables straightforward memory interactions between a WebAssembly guest and host. This is one of the key things that makes Rust a much better fit for the WebAssembly use-case: memory efficiency.

然而,这不仅仅是关于速度。Rust 提供了一些独特的优势:在 WebAssembly 中运行。Rust + Wasm 的故事一直很强壮,其中一个原因归结于 Rust 语言设计的基础。当你将 Go 程序编译为 Wasm 时,你必须连同其垃圾回收器一起编译。但 Rust 的内存管理模型使得 WebAssembly 客户机与主机之间的内存交互变得直接。这是使 Rust 更适合 WebAssembly 用例的关键因素之一:内存效率。

Cloudflare Workers have supported Wasm for a long time. Just a few weeks ago we announced supporting native Rust in Workers with the new Emscripten target for wasm-bindgen. When you run entire software build and deployment systems in tight loops on Workers, the benefits of a small memory footprint pay off quickly. It means you can run these workloads so efficiently that it’s now possible to run fully encapsulated development systems in places where they couldn’t have been run before.

Cloudflare Workers 长期以来一直支持 Wasm。就在几周前,我们宣布通过 wasm-bindgen 的新 Emscripten 目标在 Workers 中支持原生 Rust。当你在 Workers 上的紧密循环中运行整个软件构建和部署系统时,小内存占用的好处会迅速显现。这意味着你可以如此高效地运行这些工作负载,以至于现在可以在以前无法运行的地方运行完全封装的开发系统。

This enables new possibilities when combined with agents: You are now able to run a development toolchain end-to-end in a fully sandboxed environment on Cloudflare. But with Wasm, you can also run those environments safely client-side in your browser, or on an embedded device, or an IoT application.

这与代理结合时开启了新的可能性:你现在能够在 Cloudflare 上在一个完全沙盒化的环境中端到端地运行开发工具链。但是有了 Wasm,你也可以在浏览器中的客户端侧安全地运行这些环境,或者在嵌入式设备或 IoT 应用程序上运行。

How will the tsrs port be maintained?

tsrs 移植版将如何维护?

First, we are working with Theo Browne to merge tsc-rs and tsrs into a single package that supports all of TypeScript’s features including Language Server Protocol (LSP) and content mappers. Second, we’ve seen a lot of success with how we maintain Vinext in sync with its upstream, Next.js. We explained how we do this in a recent blog post:

首先,我们正在与 Theo Browne 合作,将 tsc-rs 和 tsrs 合并为一个支持 TypeScript 所有功能的单一包,包括 Language Server Protocol (LSP) 和内容映射器。其次,我们在保持 Vinext 与其上游 Next.js 同步方面取得了很大成功。我们在最近的一篇博客文章中解释了我们是怎样做到这一点的:

Each morning, an agent reviews the changes, fetches diffs, and opens tracking issues for anything that could affect Vinext. Every night, the compatibility matrix is regenerated as we run the Next.js test suite against Vinext. When one of these tests or issues reveals a gap, agents are now in the position where they can identify the change across both codebases, build a reproduction, port any relevant tests, and propose a fix.

每天早晨,一个代理会审查变更、获取差异,并为任何可能影响 Vinext 的内容打开跟踪问题。每天晚上,当我们针对 Vinext 运行 Next.js 测试套件时,兼容性矩阵会被重新生成。当这些测试或问题中的一个揭示出差距时,代理现在处于能够识别两个代码库中该变更的位置,构建复现步骤,移植任何相关测试,并提出修复方案。

We will apply the same process to TypeScript, taking extreme care that we don’t introduce diverging behaviors from upstream TypeScript. When we come across performance improvements, we’ll attempt to contribute them back to the TypeScript Go codebase so everyone wins.

我们将对 TypeScript 应用相同的流程,并极其小心地确保我们不会引入与上游 TypeScript 相背离的行为。当我们遇到性能改进时,我们将尝试将它们贡献回 TypeScript Go 代码库,以便大家都能受益。

Finally, we recognize that there are some differences to the upstream version of TypeScript right now, like for example the order in which diagnostics are printed. We aim to reduce those differences to a minimum by the time we publish a stable release.

最后,我们认识到目前与上游版本的 TypeScript 存在一些差异,例如诊断信息的打印顺序。我们的目标是在发布稳定版本时将这些差异减少到最低限度。

What’s next?

接下来是什么?

Cloudflare is already the home of many free and open source Rust tools that power the JavaScript ecosystem, such as Vite, Rolldown, Oxlint, and Oxfmt. With more than 10 million developers building on Cloudflare, we care a lot about making the underlying developer toolchain faster, more portable, and easier to run in more places. We are excited to experiment with deeper tsrs integrations into our stack, for example for type-aware lint rules in Oxlint.

Cloudflare 已经是许多免费且开源的 Rust 工具的家园,这些工具为 JavaScript 生态系统提供动力,例如 Vite、Rolldown、Oxlint 和 Oxfmt。在 Cloudflare 上构建应用的开发者超过 1000 万,我们非常关心使底层开发者工具链更快、更具可移植性,并在更多地方更容易运行。我们很高兴尝试将 tsrs 更深入地集成到我们的技术栈中,例如在 Oxlint 中用于类型感知的 lint 规则。

Like with Vinext, our goal is to run tsrs as a well-maintained healthy open source project — without getting in the way of TypeScript itself. The Oxc team has already begun refactoring the codebase to make it faster, use less memory, and make it maintainable. We know the TypeScript team is taking this seriously – they are experimenting with memory and performance improvements for tsc. We are excited to collaborate with the TypeScript team and the JavaScript community to build the best and fastest implementations of TypeScript together.

与 Vinext 一样,我们的目标是让 tsrs 作为一个维护良好的健康开源项目运行——而不干扰 TypeScript 本身。Oxc 团队已经开始重构代码库,使其更快、占用更少内存,并且更易于维护。我们知道 TypeScript 团队非常重视这一点——他们正在探索 tsc 的内存和性能改进。我们很高兴能与 TypeScript 团队及 JavaScript 社区合作,共同构建最好、最快的 TypeScript 实现。

更进一步:量化金融体系

看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力

进入量化体系 →

关联讨论

同一事件的更多信源

相似阅读

关联信息,但可能不是同一事件