How I Found the Best Programming Language in the World. Part 1

Java Scala Functional Programming

Since I’m going to provoke trolls on the subject of programming languages, and even name the best one in the world (I mean absolutely the best, for sure), I recommend all of them to check out my previous post “Choosing a Programming Language”. Everything mentioned there is still up-to-date, and I wouldn’t’ like to repeat myself here.

Read it? Good. Today I am going to talk about the best programming language in the world, and I’ll name it closer to the end of this post. Three days ago I didn’t even think that unicorns existed, as each language sucks in this way or another, and each one does it in its own way (actually, there are some languages that always suck, but we won’t talk about them). As an engineer, I totally understood that the language question was impossible without various trade offs, starting from garbage collected vs. manual memory management and so on (Rust tries to gain without pain in this question though). Although we are talking about general-purpose languages, all of them are presented one way or another, focusing on some ideas, principles, and purposes they’re good at. However, they leave all the things that are not consistent with these principles barely possible, not effective, and/or inconvenient. To put it simply, they’re basically impractical. Nevertheless, there is a language that combines the incongruous.

As I have said, I faced the problem of choosing a programming language only in adulthood, about seven years ago, when I realized that Java was not what it used to be. At the same time, in search of productivity and a naïve desire to make Scala the main language in the company for the next 10 years, we bought early access (some chapters) of the not-yet-published book by Odersky et al. So, I had discovered Scala years before it appeared in CVs and heads of people coming for an interview, and some of you even didn’t go to school at that time. Working for a company that has produced customized IDEs for more than 10 years, I have seen lots of language trash and pure gold among it. Therefore, I’d like to ask all the captain Obviouses to think a little bit, before sending the author to a language history — do you really think that a guy who’s been searching for a language for seven years, and still cannot find it, doesn’t know about the history?

In recent years, the question of choosing a programming language has become more acute. We should begin a new technological revolution at Xored, and we have some ideas of smart technologies that allow making various Magic, but I wasn’t ready to start anything for a very long time. Why? The answer to the question still surprises me. If I got such answer on an interview, the guy would definitely get a “minus” from me. The answer sounds really silly and childish: I did not know in what programming language I wanted to see the stack of our brilliant technologies. The only excuse I have is that I’m really focused on a long way. I’m not talking about some certain project. It is something I want to invest the best years of my life in. That’s why this question is so important to me.

Setting out on a long and difficult journey, it’s really dangerous to make a wrong choice of vehicle. All recent years I’ve been looking (waiting?) for a programming language (a platform) that wouldn’t make me want to reject it due to some reason. Having quite ambitious goals, I had certain functional and non-functional requirements to such programming language.

Requirements

What I want is high-performance code in the browser. I also want to be able to run my code on unikernel/cloud os. I want a high-performance server-side with large volumes of information processed. I want iOS and Android (high-performance as well). I also want to develop all of this with no hassle. I don’t want to bother about code generation and work with memory directly through a narrow gate as sun.misc.Unsafe. I also don’t want to develop large arrays like a mathematician in Fortran.

I want to write very low-level code (close to bare metal), and have performance comparable to native C/C++. I want GC when I need it, and I don’t want GC when it’s not necessary. I want to write object-oriented and functional code. I want to overload operations, define higher-order functions and use closures. I want generics, and I may want templates and/or macros to implement some future concepts. I want to understand how all of this is implemented in the language, starting with memory and concurrency models. I also want to control these things the way I want to.

I want everything on the low-level. On the high level, I want all the power comparable to Scala. I want to write code fast and easily, feeling it with my spinal cord. I want the compiler to think for me, and not the other way around, as is the case when writing in Scala or С++. I want to be productive as a programmer and write programs much faster than, say, in Java or JavaScript.

Here’s a dump of things I want. Well, it’s obvious that I want everything at the same time. I might be the first one to say that there’s no such language. But there is.

Cross-Platform. Close to Bare Metal

I want a cross-platform support with a mandatory compilation to JavaScript. Not just a compilation to JavaScript, but also high-performance code in the browser. In addition to all features of the language giving me the opportunity to develop UI and client-server interaction, I need a serious number crunching on the client-side. I also need to process a million or two of tuples in a split second, which is a very desirable requirement. It’s even possible that I’d want the client code to be a complete P2P network. Anyway, there are not less requirements to a client side code.

We can write something of high performance working on a large amount of data in both Java and JavaScript. We can, more-or-less, solve such things by actively using off-heap data structures, that implies either a ton of generated code (with all the subsequent problems) or a pitiful semblance of programming. Technically, you develop in Java but it’s not your mother’s Java, but a miserable subset of the language. The same story takes place in the world of browsers: absolutely the same situation with typed arrays and asm.js (leave various NaCl/PNaCl to yourself – I’m not interested).

In theory, I can use lots of things for my tasks: scalajs in Scala and gopherjsin Go, as well as lots of JVM-based stuff (the beloved Fantom, Kotlin, Ceylon) with a compilation to JavaScript. The latter will work well if I can elegantly use the off-heap data in a case with compilation to a bytecode, and typed arrays in a case with compilation to JavaScript.

Actually, I was moving this way, but, getting back to the subject of the article, it’s the way against the language. I programmed against the language, or rather JVM platforms, that is actually orthogonal to the task solved. To solve my problems, I need everything the platform gives but in a different, DIY way. I need an off-heap memory management and a system of value types of my own. To make all of this work properly, I also need code generation (compile-time and/or run-time). Thus, the following question arises: “Do I really need all of these languages and platforms (JVM, the browser), if they don’t help and even stand in the way?”

JVM Off-Heap Data Structures

There are definitely lots of decent projects moving “against the platform”. If you’re interested in Java contest, I can recommend you various sources. You can start with the Mechanical Sympathy blog. These guys have produced lots of interesting things with JVM. As for performance, they show how to show 15 to 35 times better results compared to ProtoBuf. As for Off-Heap data structures, this posthas an example showing a 40-time increase in productivity compared to the use of language (Java) as designed.

I would also like to highlight the little known (or rather quite unknown) landz project. The author offers a new platform on top of JVM with lock-free/garbage-free off-heap memory allocator, lock-free collections, async-await, channels, off-heap collections, etc. Pretty cool… I should also mention objectlayout.org and other things, but all of these are inconvenient and is being made despite the platform design, not thanks to it.

Browsers, asm.js and emscripten

Emscripten works wonders in browsers. For those who don’t know emscripten: you can play Quake3. The list of portable games (etc.) can be found here. As far as I understand, Unity and high-end game engines drag themselves to the browser exactly this way. Unity compiles all the stuff, including .NET (Mono) runtime to JavaScript (asm.js), with the help of emscripten. It’ is certainly not the JavaScript written with your own hands. Such JavaScript can only be a result of compilation. I have serious doubts that a program in JavaScript (as designed) will be somewhere close to asm.js (I’m talking about number crunching and large amounts of data). If so, I want asm.js generation (and a different memory model) where necessary.

More Appropriate Language? (low level)

A careful reader will suggest that we need completely different languages (and platforms, I should add) to solve such low-level problems, and will be right. I thought so too, and even tried to write my own language. Something like LLJS that would compile to both JavaScript and JVM bytecode, running the bytes along off-heap memory / typed arrays. By the way, if such language existed (LLJS requires more work), I would use it and close the subject. In this case, this post wouldn’t have existed, and, probably, I wouldn’t have found the best programming language in the world.

In addition to languages more suitable for the task, I pinned great hopes on Go that is pass by value in contrast to JVM-based. Besides, its memory model is simpler and clearer. But when we begin to compile to the “traditional” JavaScript (gopherjs), that is somewhat close to JVM in partially interesting for me low-level questions, — performance falls by 20 times (for the sake of interest, you can take a look at the sad experience of a guy from some company that has decided they can make an analogue of React.js, but, because of Go, a very fast one).

Seems like I started to flush the beloved JVM, but it’s too early for that. First of all, we should take a look at the high level: Scala and other friends, but we’ll talk about them in the following post.

Comments

    3,751

    Ropes — Fast Strings

    Most of us work with strings one way or another. There’s no way to avoid them — when writing code, you’re doomed to concatinate strings every day, split them into parts and access certain characters by index. We are used to the fact that strings are fixed-length arrays of characters, which leads to certain limitations when working with them. For instance, we cannot quickly concatenate two strings. To do this, we will at first need to allocate the required amount of memory, and then copy there the data from the concatenated strings.