MIT's Codon compiler allows Python to 'speak' natively with computers

Researchers at MIT created Codon, which dramatically increases the speed of Python code by allowing users to run it as effectively as C or C++.
Christopher McFadden
codon-python-compiler.jpg
Could Codon be a glimpse into the future of Python?

Arsenii Palivoda/iStock 

Python is one of the most popular computer languages, but it has a severe Achilles heel; it can be cumbersome compared to lower-level languages like C or C++. To rectify this, researchers from MIT’s Computer Science and Artificial Intelligence Laboratory (CSAIL) set out to change this through the development of Codon. This Python-based compiler allows users to write Python code that runs as efficiently as a program in C or C++.

“Regular Python compiles to what’s called bytecode, and then that bytecode gets executed in a virtual machine, which is a lot slower,” says Ariya Shajii, an MIT CSAIL graduate student and lead author on a recent paper about Codon presented in February at the 32nd ACM SIGPLAN International Conference on Compiler Construction. “With Codon, we’re doing a native compilation, so you’re running the result directly on your CPU—there’s no intermediate virtual machine or interpreter,” Shajii added.

In order to identify the type at runtime, Saman Amarasinghe, an MIT professor and lead investigator for the CSAIL who is also a co-author of the Codon paper, notes that "if you have a dynamic language [like Python], every time you have some data, you need to keep a lot of additional metadata around it.”

"To determine the type at runtime. Codon does away with this metadata, so “the code is faster, and the data is much smaller,” he added.

Shajii says that Codon doesn't waste time during runtime because it doesn't check data or type as a matter of course. What's more, this makes Codon perform on par with C++ in terms of speed, "we usually see 10x to 100x improvement,” he says.

But Codon’s approach comes with inevitable trade-offs. “We do this static type checking, and we disallow some of the dynamic features of Python, like changing types at runtime dynamically,” says Shajii. “There are also some Python libraries we haven’t implemented yet,” he added.

Amarasinghe adds, "Python has been battle-tested by numerous people, and Codon hasn’t reached anything like that yet. It must run many more programs, get feedback, and harden up more. It will take some time to get to [Python’s] level of hardening.”

“In regular Python, it leaves all of the types for runtime,” says Shajii. “With Codon, we do type checking during the compilation process, which lets us avoid all of that expensive type manipulation at runtime," Shajii said.

Codon was designed for genomics and bioinformatics, but it can also be applied to similar applications that process massive data sets, GPU, and parallel programming.

In the future, businesses could use Codon for app development and testing. According to Shajii, "A pattern we see is that people use Python for their prototyping and testing because it's user-friendly, but when it comes down to it, they have to rewrite [their app] or have someone else rewrite it in C or C++ to test it on a larger data set."