Typescript compiler and tool set is getting ported to native code, to the language "go"
Project Corsa
TypeScript's Journey to Native Code for 10x Faster Performance
In a groundbreaking move for the TypeScript ecosystem, Anders Hejlsberg, the lead architect of TypeScript at Microsoft, introduced Project Corsa: an ambitious initiative to port the TypeScript compiler and tooling to native code.
This project promises to address long-standing performance issues, enabling TypeScript to scale more effectively, especially for large-scale projects. The results so far have been promising, with up to 10x faster compile times compared to the current JavaScript-based compiler. In this article, we’ll explore the details of this exciting project, the challenges it aims to solve, and the potential benefits for TypeScript developers.
The Problem with JavaScript for Large-Scale Projects
Since its inception over a decade ago, TypeScript has been written in TypeScript itself. This approach provided numerous benefits, such as rapid development and ease of iteration, but it also came with significant challenges. One of the most pressing issues is performance, particularly as projects grow larger and more complex.
JavaScript, while an excellent runtime for UI and browser-based applications, is not optimized for computationally intensive tasks like compiling. The JavaScript runtime often struggles with memory management and performance, especially when working with large codebases like Visual Studio Code, which has over 1.5 million lines of code.
Some of the key challenges with JavaScript include
JIT Compilation: JavaScript is just-in-time compiled, meaning it incurs a performance penalty at startup.
Flexible Object Models: JavaScript’s dynamic typing and object model introduce overhead that makes efficient memory use difficult.
Lack of Concurrency: JavaScript does not support shared memory concurrency, which is vital for speeding up operations on multi-core systems.
With these constraints in mind, the TypeScript team began exploring ways to overcome these hurdles and boost performance.
Enter Project Corsa: Moving to Native Code
Project Corsa is the TypeScript team’s solution to this performance problem. The goal is not a complete rewrite of the TypeScript compiler, but rather a port of the existing codebase from TypeScript to native code.
The team spent months evaluating different languages for the port, including C, Rust, and C++, before settling on Go. While Go may not be the most commonly used language for compiler development, it provided the ideal mix of features needed for this task:
Native Code Support: Go generates efficient machine code that runs natively on all major platforms.
Automatic Memory Management: Go’s garbage collector allows for effective memory handling without the overhead of manual management.
Concurrency: Go’s built-in concurrency model is perfect for parallelizing the compiler’s operations, making it a key factor in the 10x performance gain.
By porting the TypeScript compiler to Go, the TypeScript team could achieve both better performance and better memory usage.
Performance Boost: 10x Faster Compilation
The most exciting result of Project Corsa has been the dramatic performance improvement. The new native code compiler is not just a little faster—it’s 10 times faster than the old JavaScript-based compiler.
For example, compiling Visual Studio Code, a large project with 1.5 million lines of code, which previously took around 1 minute, now takes just 5 seconds. This means that developers can expect batch compiles, project loads, and incremental builds to be completed 10x faster, greatly improving productivity and the overall development experience.
The performance gains come from a combination of factors:
Native Code: Moving away from JavaScript to Go results in lower overhead and more efficient execution.
Concurrency: By parallelizing tasks like parsing and type-checking, the compiler can leverage multi-core processors more effectively.
The Power of Concurrency: Parallelizing Compilation
One of the most significant contributors to the 10x performance boost is the ability to leverage concurrency. The TypeScript compiler performs several tasks—parsing, binding, type-checking, and emitting code—each of which can be parallelized to a degree.
Parsing and Binding: These tasks involve reading and analyzing each source file. Since each file is independent of the others, they can be processed in parallel. This means that, with enough CPU cores, the overall time for these operations can be reduced dramatically. For example, with an 8-core system, the time could be 8x faster.
Type Checking: Type checking is more challenging to parallelize because types often span multiple files. However, the team uses a multi-threaded approach by splitting the project into smaller chunks. This allows the compiler to check different parts of the project concurrently, reducing the time needed for type-checking by 2 to 3 times.
New Language Service: A Modern Approach to TypeScript Tools
Alongside the native compiler, Project Corsa also powers a new language service for TypeScript. The language service is responsible for features like IntelliSense, error highlighting, and code navigation, which are essential for a smooth developer experience.
With the new Go-based implementation, the language service is now up to 5 times faster than before. This is particularly noticeable when loading large projects, such as Visual Studio Code, which has over 4,500 files and 1.5 million lines of code. In the new system, restarting the language server and parsing all files takes just a few seconds, compared to the slower, more cumbersome approach of the old JavaScript-based language service.
Additionally, the new language service now uses the Language Server Protocol (LSP), which makes it easier to integrate TypeScript with other editors and IDEs. This move brings TypeScript in line with modern language services, offering better compatibility with tools outside the Microsoft ecosystem.
The Road Ahead: New Features and AI Integration
While Project Corsa is already yielding impressive results, the team is far from done. Some of the upcoming features include:
Support for JavaScript, JSX, and Project References: The compiler is currently limited to TypeScript and doesn’t fully support JavaScript or JSX. However, the team plans to add these features soon, along with support for incremental compilation and project references.
AI-Assisted Features: With the performance gains from Project Corsa, the TypeScript team is exploring new ways to integrate AI capabilities. This includes real-time type-checking of code, AI-driven refactoring, and enhanced semantic analysis.
Conclusion: A Bright Future for TypeScript
Project Corsa represents a significant step forward for TypeScript, addressing long-standing performance issues while preserving the core features and semantics that developers rely on. By moving the compiler to native code, leveraging concurrency, and modernizing the language service, the TypeScript team is setting the stage for an even more powerful and efficient development experience.
As the project progresses, developers can look forward to even more improvements, including broader language support and AI-powered tools. TypeScript’s future has never looked brighter, and Project Corsa is at the heart of that future.
Reference:
Why Go?
Why a port instead of a rewrite? What's the difference?
More discussions