Comparison of virtual machines, implementation of Lua 5

Source: Internet
Author: User

After finishing the Virtual Machine and compiler
Make a comparison. The comparison results are quite frustrating. My virtual machine can only reach Lua's speed of more than half. So I read lua5 again in an unconvinced manner.
Source code. I used to read the Lua source code for a while, and even the Lua 4 and lua5
I read data in different periods. Of course, I also know the huge difference.

In fact, for simple programs, my virtual machine has a speed advantage, and
Much faster. I blame it on Coding's skills. However, the design team is defeated. Because Lua 5
I already have a register-based virtual machine, and I am still using a stack-based virtual machine. Although I have improved it and moved closer to the register-based direction, it is not as good as a pure register virtual machine.

I have carefully considered the implementation difficulty of the register-based Virtual Machine in recent days. Although I can continue to work on it myself, but the current project is very tight and I decided to take it for a while. Let's take a look at it.

On vacation today, I read some Lua maillist subscribed to in the mailbox. I want to see Lua 5.1
What changes have you made. Because I have done some similar work recently, it is easier to understand the progress of Lua. Finally, we found that the improvement of Lua is concentrated in GC.
Above. This is quite satisfactory to me. In fact, the most proud thing I have achieved is GC, which is definitely better than Lua. Lua is currently working on generational GC.
I am considering a parallel solution. It is about to solve some headaches of GC. However, the GC of Lua
After all, it is not a memory arrangement. In a restricted memory environment, the performance is relatively poor. In addition, Lua
For the sake of maximum speed, there are also some problems in deep-level Recursive support, which may lead to full stack. I have written a program that uses recursion to calculate the sum of sequences. The recursion level is too deep, Lua
I cannot handle it. My virtual machine is competent. Of course, this is not a problem for general applications.

After all, Lua has been developing for more than 10 years and there are too many things to learn from. The three weeks of toys in my zone should start with hands-on training. Read it carefully today <
Href = "http://www.tecgraf.puc-rio.br /~ Lhf/FTP/doc/sblp2005.pdf ">
Implementation of Lua 5.0 </a>. If you are interested in scripting implementation, read this article.

From this paper, we can learn a few things. Although we can also learn through reading the source code (I just read the source code first), reading this can be more casual.

Lua5 is already fully register-based Virtual Machine
Is the world's first widely used register-based Script virtual machine. At this point, the current Java JVM and. NET are still
Stack-based. Perl6 is said to be a register-based program. Of course, my language also has this plan.
Haha. Registere-based virtual machines are relatively difficult to write, which seems to be a challenge.

Lua5 optimizes the table implementation, which I mentioned in the previous blog. This paper is also described in detail.

Lua5 has added coroutine implementation. I have considered that since I changed my virtual machine to a system stack independent, it is not difficult to create more than one stack by running several threads. Coroutine of lua5 also has an independent stack for each thread. (Actually there are two stacks)

Lua is also a one-pass compiler based on Recursive descent algorithms. As mentioned in paper, the compiler of one-pass is
Hard-written. I have a deep understanding of this. Write Compiler
In those few days, my mind went on to scream, and I did not dare to submit a line of code to the warehouse for two days. However, the advantage of writing is obvious, just as mentioned in paper, smaller, more
Efficient, more portable, and fully Reentrant

A few days ago, I was wondering whether the object description in a weak language could be smaller than that in Lua. Lua uses an independent word to describe the type, and then uses Union
To store different types of data. I tried a method a few days ago, that is, the last two digits of the pointer will always be 0 due to alignment. (If we use 8
If the alignment is true, the last three digits are all 0. In this way, some additional information is used to store the data type. An integer can be expressed as 30 or 31bit.

Today, I read this paper and found that our predecessors have already used it, for example, smalltalk80. But paper explains why Lua does not do this. Of course, I can try my own language.

For the instruction sets used by lua5, each instruction uses only 4
Bytes, the implementation method is still very clever, this day when I read the source code has been taught. At that time, I wanted to find out where the speed of my language was slow. I = I + 1
This operation. Register-based virtual machines can naturally run faster. In addition, the instruction of my virtual machine is 8 bytes, while Lua only uses 4
Bytes, the data is short, and the efficiency can also be improved.

After reading this paper, I feel that Lua in many places despise Python's inefficiency. This is the same as the Lua round I participated in at gdc2004.
The feeling of table is the same. It can be changed to Lua and Python's "factional struggle ".
We are more confident that we will be more confident. After all, lua4 users were far less likely to be lua5 users two years ago. (when I spoke, I specially asked lua5 users to give a hand this time with Lua
With 5.1 release, Lua's position in the gaming industry will be unshakable.

 

Reprinted: http://blog.codingnow.com/2005/12/compare_with_lua_5.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.