光束
The Beam

原始链接: https://www.erlang-solutions.com/blog/the-beam-erlangs-virtual-machine/

这篇博文介绍了Erlang虚拟机(BEAM),它是Elixir强大和可靠性的基石。Erlang诞生于20世纪80年代的爱立信,旨在构建容错、并发和实时系统。Erlang生态系统包括语言本身、OTP框架、工具以及BEAM。 BEAM是一个操作系统进程,它管理Erlang代码的执行、进程的创建、调度和并发。进程通过异步消息传递进行通信,从而通过隔离实现容错。BEAM能够并行化程序,对每个进程进行垃圾回收,并提供高效的错误检测,从而确保可扩展性、分布式能力和响应速度。 Elixir利用BEAM,继承了它的特性并使用Erlang库/OTP。Elixir的语法更易于上手,其代码编译成紧凑的字节码供BEAM执行。Elixir提供了清晰的模式和代码简化,使其易于理解和维护。Elixir受益于BEAM的并发模型,我们将在下一章详细探讨。

这个Hacker News帖子讨论了Erlang的BEAM虚拟机及其相关语言Erlang和Elixir,尤其是在编译器构建的背景下。用户们辩论Erlang/Elixir相较于OCaml或Haskell在此领域是否具有优势,提到了模式匹配能力和BEAM的并发支持。虽然BEAM并非选择Erlang/Elixir的主要论据,但它仍然可以提供益处。 讨论深入探讨了Erlang/Elixir的类型系统,一些人认为它们是强类型的,而另一些人则认为其类型系统是“附加的”,不够符合人体工程学。对“强类型”的含义也存在一些争论。Elixir正在逐渐获得静态类型特性。BEAM的性能受到赞扬,尤其是在JSON处理方面,但其在数值计算方面的局限性也得到了承认。对于想要静态类型的开发者来说,像Gleam这样的静态类型BEAM语言也被提及。最后,人们表达了对具有静态类型序列化的语言的渴望。

原文

Welcome to the first chapter of the “Elixir, 7 Steps to Start Your Journey” series. In my previous post, I discussed my journey with the programming language.

In this chapter, we will discuss the Erlang Virtual Machine, the BEAM.

To understand why the Elixir programming language is so powerful and reliable, we must understand its foundations, which means talking about Erlang. 

Elixir runs on the Erlang Virtual Machine and inherits many of its virtues. In this post, you will learn a little about the history of Erlang, the objective with which it was initially created, and why it is fundamental for Elixir.

Erlang as a programming language

Erlang is a programming language created in the mid-1980s by Joe Armstrong, Robert Virding, and Mike Williams at the Ericsson Computer Science Laboratory. Initially designed for telecommunications, it is now a general-purpose language. It was influenced by other programming languages, such as ML and Prolog, and was released as open-source in 1998.

Erlang was designed with distributed, fault-tolerant, massively concurrent, and soft real-time systems in mind, making it an excellent choice for today’s systems. Most are looking for these features, in addition to having confidence in Erlang’s history in productive systems.

Some of the characteristics of this programming language are:

  • It is a declarative language, which means it is based on the principle of describing what should be calculated instead of how
  • Pattern matching is possible at a high level and also on bit sequences.
  • Functions in Erlang are first-class data.

Erlang as the development ecosystem 

Up to this point, we have referred to Erlang as the programming language; however, it should be noted that Erlang can also refer to an entire development ecosystem that is made up of:

  • The Erlang programming language
  • The framework OTP
  • A series of tools and
  • The virtual machine, BEAM

Erlang, as an ecosystem, was explicitly created to support highly available systems, which provide service even when errors or unexpected circumstances occur, and this is due to many of the characteristics of its virtual machine (VM).

So, although Erlang as a programming language is pretty cool on its own, the real magic happens when all the ecosystem elements are combined: the programming language, libraries, OTP, and the virtual machine.

If you want to know more about the history of Erlang, the list of resources below will be very helpful.

Resources

Erlang Virtual Machine, BEAM

The Erlang Virtual Machine, known as the BEAM, runs as an operating system process and is responsible for executing the Erlang code. It is also responsible for creating, scheduling, and managing Erlang processes, which are the fundamental basis of concurrency. 

Thanks to the BEAM schedulers, these processes can be executed in the most efficient way possible, allowing the system to be highly scalable. The processes do not share memory; they communicate through asynchronous message passing. This mechanism is the foundation for a system’s fault tolerance. As they are entirely isolated, the other system processes will not be affected if an error occurs in one of them.

The BEAM is also responsible for parallelizing your concurrent Erlang programs, making the most of a machine’s resources. Initially, the virtual machine model was a single-run queue. However, it evolved into a run queue for each available processor, ensuring no bottlenecks and that Erlang programs work correctly on any system, regardless of the number of machine cores.

Another characteristic is that storage management is automated. Garbage collection is implemented per process, which allows a system’s response time to always remain in the order of milliseconds without performance degradation.

And lastly, one of my favourite features is error detection. The virtual machine provides all the elements necessary for efficient error detection and handling, thus promoting an always-available system regardless of failures.

In summary, the BEAM is responsible for the scalability, distribution, and responsiveness of a system:

  • Manages the concurrency of it.
  • It has a mechanism for error detection and handling.
  • Make the most of the computer’s resources.

 If you’d like to learn more about the duo that is Erlang and Elixir, check out the “What is Elixir” post.

Elixir in the BEAM

Like Erlang, Elixir was also influenced by other programming languages, including Erlang itself. Its code runs on the Erlang Virtual Machine, which means it takes advantage of all its features and can use all the Erlang libraries and the OTP framework.

Different programming languages ​​besides Elixir and Erlang run in the BEAM, but Elixir has ensured that the approach between BEAM and programmers is fluid and quickly understandable.

Elixir code is compiled into bytecode that runs in the BEAM and is more compact than Erlang code. Its syntax is similar to how we communicate daily, allowing for early familiarization with the language, even if it is the first time you program with it. It also reduces the boilerplate and has amazing documentation.

So, when writing code with Elixir, we have the best of both: a solid and battle-tested foundation that allows us to create fail-safe systems and, on the other hand, nice syntax, well-defined patterns, and code simplification, among other things. Thanks to this, Elixir has been so well accepted and has rapidly gained popularity.

Elixir is a cool programming language that allows you to write code that is easy to understand and maintain and takes advantage of the Erlang concurrency model, which we will discuss in the next chapter.

> iex


iex(1)> list = [4,5,21,1,38]


iex(2)> erlang_example = :lists.sort(list);
[1, 4, 5, 21, 38]


iex(3)> elixir_example = Enum.sort(list)
[1, 4, 5, 21, 38]

Example of how you can run Erlang and Elixir code in an interactive Elixir shell

Next chapter

In the next post, “Understanding Processes and Concurrency,” we will discuss how Erlang processes work and their importance in developing robust and scalable systems. We will also see how concurrency works in Erlang and how this relates to Elixir. Do not miss it! You can drop the team a message if you’d like to discuss Elixir in more detail.

联系我们 contact @ memedata.com