Bachelor's Degree in Computer Science Performance Comparison Between C and Rust Compiled to Webassembly
Total Page:16
File Type:pdf, Size:1020Kb
UMEÅ UNIVERSITY June 4, 2021 Department of Computing Science Report 5DV199 Bachelor’s degree in computer science Performance comparison between C and Rust compiled to WebAssembly Magnus Medin (dv18mmn) [email protected] Supervisor Ola Ringdahl Contents 1 Introduction 1 1.1 Background . .1 1.2 Purpose and research questions . .2 1.3 Delimitations . .2 2 Related work 2 3 Theoretical background 4 3.1 Language . .4 3.2 Compilation . .5 4 Method 5 4.1 Code analysis . .6 4.2 Testing . .6 4.3 Data collection . .6 5 Result 7 6 Discussion 9 6.1 Limitations and further research . 10 6.2 Conclusion and recommendations . 11 Abstract Web applications are used more and more and mainly they are developed with the help of Javascript, but when you have heavy demanding processes in a web app, javascript can be too slow. The new language WebAssembly is an alternative that seeks to solve this problem. But unlike Javascript, there are many compilers for many different languages that have WebAssembly as their compilation goal. This means that the choice of language to compile from could make a difference when it comes to execution time. This article takes a closer look at two of the most popular languages, namely C and Rust, to see if language choice makes a difference. The differences between the languages are measured with the help of tests done between a server and a client and also with the help of an analysis of the WebAssembly code. The result shows that Rust is faster than C in matrix multiplication and insertion sort but the addition test shows that the languages are similar in speed. Acknowledgement I would like to thank William, Emil and Tobias, for inspiration, help and entertainment during these unusual study conditions. Wasm preformance comparison 1 Introduction 1 Introduction When it comes to web applications, Javascript (JS) is top of mind for most developers. Almost all websites use it and JS has been used since the 90’s. The web has evolved since then and so has Javascript. But now when the internet is used alot, especially during a pandemic, it is becoming clearer that languages are needed that can complete and help the omnipotent programming language. Web apps biggest problem right now is that they are slow, Javascript is not made for collaboration video editing program in the cloud or a fast paced 3d shooting game in the browser. Many solutions have been presented over the years but when a solution becomes a World Wide Web Consortium recommendation and companies like Google, Mozilla, Microsoft and Apple stand behind the language it is worth taking a closer look. WebAssembly or WASM [11] is an “Assembly” like language for the web. It defines a portable binary-code format and can be compiled from some of the most widely used programming languages, and is able to run on the most popular browsers. On the official website webassembly.org, 4 design goals are listed, which are the following. • Efficient and fast • Safe • Open and debuggable • Part of the open web platform One thing that a lot of focus is placed on when it comes to WebAsemmly is the execution time, as this is primarily why one should choose it over JS and the creators say that WebAssembly “aims to execute at native speed”, that is, the same speed as if you were running the program locally on the computer. 1.1 Background The compiler Emscripten [1] was announced in 2010 as an attempt for a solution on how to run native C, C++ code in the browser. This was done by compiling the code to Javascript. A subset of JavaScript called asm.js [10] that is statically-typed languages with manual memory management was announced 2013. Emscripten chose to have it as a compilation target as it had better performance than plain javascript. Here they started to 1 June 4, 2021 Wasm preformance comparison 2 Related work see the possibilities with a faster language for the web and a number of interesting programs and games were published with the help of asm.js [3] [2]. However, since JavaScript was not designed to be used as machine code there was potential for improvement if a more adapted language was used. WebAssembly was developed and released in 2017. The advantages of WASM against asm.js is that WASM can skip the parsing step in the browser’s Javscript engine. WASM has a more compact source code because it is a binary instruction format which makes fetching the code faster. Now most of the major browsers have language support for WASM, possibilities for execution outside the browser have been developed and new compilers for multiple languages have been created. 1.2 Purpose and research questions Since the language is often used for faster execution time and that WASM can be compiled from a lot of languages, there is an interest in knowing how widely the choice of language affects the execution time. Therefore, this report focus on two of the more popular languages and make a comparison between them. Is Cs execution time faster than Rust when running on browsers using WebAssembly. To answer this, a benchmark test for both languages is implemented, then tests are run on them, the results from the experiment are analyzed and the WASM source code is reviewed to finally present the results. A discussion about the results is made. 1.3 Delimitations This thesis will only look at two languages. This is due to lack of time and that the results can show how and when there are differences between the languages which could be applied to further work. Another limitation is that the algorithms to be tested are of a simple nature. This is also to be within the time frame of this type of work, to make it easier to compile to Webassemble and to make the code analysis less complex. 2 Related work WebAssembly is a new language and it should be faster than javascript. Therefore, there is a lot of new work and testing in that particular part of 2 June 4, 2021 Wasm preformance comparison 2 Related work WASM. The previously mentioned goal of achieving native speeds has also been tested to get clarity on how close the language is to achieving this and what needs to be done to achieve the goal. The text written by Haas et al [4] describes WebAssembly and tests the speed against native C. It is a collaboration between Microsoft, Google, Apple and Mozilla where they give an overview of WebAssembly and present benchmarks where they compare WASM in comparison to native. The paper showed that WASM was close to native. But they also saw potential for improvements. WASM could take advantage of parallel compilation and utilize compilation in advance. There are javascript engines that get good results from this method. An- other thing they bring up is that one could benefit from code caching on programs that are frequently reused. But there are those who think that WASM should also be tested against larger implementations than polybench tests, Abhinav Jangda et al [8] writes “We argue that a more comprehensive evaluation of WebAssembly should rely on an established benchmark suite of large programs, such as the SPEC CPU benchmark” . In comparison with previously reported results from polybench, WASM performs slightly worse in their test with SPEC. In the report written by D. Herrera et al [6] they used the Wu-Wei version of the Ostrich benchmark suite on a number of devices and different environments. They came to the conclusion that “All browsers which incorporated WebAssembly technology showed impressive gains over their JavaScript engines.” and “WebAssembly provides excellent performance, with WebAssembly in Firefox nearing the performance of na- tive C.”. They also showed that WASM is faster than JS on all platforms and environments they tested on. The interesting part of this study is that WASM is tested on a variety of hardware such as mobiles, tablets and ARM processors and shows similar results as previous studies. There are many articles that test Rust in comparison to C, in the article [7] Jabber examins some popular sorting algorithms against each other and the author presents a result where Rust is faster in all tests except when sorting with insertion sort. In the conclusion, the author writes “It’s quite difficult to say which language is faster because it depends on case to case. But we can say that Rust is a competitor of C in terms of speed and it is faster than many other popular languages like Java and Python.“ There are many related work that look at WebAssembly in comparison to Javascript or native code on many different platforms. There are also many articles that benchmark the languages against each other but when it comes to comparisons between implementations within WebAssembly, there is not much done. An article addressing the topic is published by IBM written by Josh Hannaford [5] where a team looked at which language choice was right for their work with WASM and Node.js. The conclusion was that Rust was the best choice for them “Another major advantage of using Rust with Wasm is the execution speed and the binary size. Both of these are comparable or 3 June 4, 2021 Wasm preformance comparison 3 Theoretical background even a little better than other lower-level, non-garbage collected languages like C and C++.”. 3 Theoretical background WebAssembly is intended as a language similar to assembly for the Web, however, it is starting to move away from web only and is now possible to run as a regular program locally on the computer.