[NIO] Process optimization from 3 million to 7 million--dawn

Source: Internet
Author: User

Dawn libraries, using Kilim, but has been far from Kilim the original code, mainly retained the association of the two basic primitives, and then to the upper layer, has been all replaced.


Libraries, I've studied the ASIO in boost, and I've tested the speed of a simple context switch on my machine. This speed is not comparable between different machines because the CPU may be different from each other. However, the comparison of different libraries on the same machine has some referential significance.


I remember that at that time, the maximum continuous throughput of the ASIO was approximately 4 million times per second when the process was single-threaded.

I was ready to use Kilim, so at the same time also tested the Kilim, I remember that the continuous throughput test is about 2.6 million times, then a little bit disappointed, but take ASIO reference, also reached more than 50%, so, also adopted the Kilim.


In the previous period of time to write dawn, I specifically optimized the process of this piece, the first use of red and black tree timer, run the maximum continuous throughput is about 3.6038 billion per second. I suspect that the red-black tree has had excessive rollover in unstable situations, resulting in extra overhead.

So, the next day, wrote a time round, changed up, constantly adjust parameters, test throughput, found still about 3.8 million. No matter how you tune, you can't go up. With Jprofiler detection, can not see obvious defects, at that time gave me the feeling is slow in memory access, because my time round every tick with a list. I think it is slow here, but this is already the simplest list, and how to optimize it?

In this way, I paused the optimization, full of effort to work and dawn and bug modification.


Two weeks later today, when I woke up in the morning, I lay on the bed, just thinking about it, I thought of this problem, suddenly think, if the tick on the list into a circular queue it? Try, get up immediately, write a loop queue, in order to avoid the loop queue overflow, I added a chain list inside the loop queue, put the elements beyond the loop queue capacity into the list. I turned the loop queue size to 100,000. Using the loop queue instead of the linked list, open the test case, this time incredibly can run to 5.6 million times per second, explaining the idea. I tuned the parameters, probably set the loop queue size to 50,000, the best balance, the throughput is about 6 million per second. Look at the time, also wrote for nearly one hours, so I lay down to see the news.

It is not good enough to think that the loop queue + linked list is not as well, because when the loop queue is full, the list is enabled and the overhead is increased, and the throughput is reduced at busy times. If you use only circular queues instead of linked lists, you are afraid of overflow. Suddenly, I think, if the list of each node expansion point expansion of the array, not to knot.

Immediately, wrote a linkedarraylist, array linked list, so it is not afraid of overflow, will also maintain a high performance. Replace just the loop queue, run the test case, no accident, the first run to more than 6 million, after more than 10 minutes of parameter adjustment, finally, the array length is set to 5000, a single thread, 200,000 coprocessor, the association context switch reached 700多万次 per second.


To reach this speed, or more than I expected, I began to feel that can be more than 5 million satisfied, and eventually to more than 7 million.

So I tested the timing task, unexpectedly executed to 11 million times per second, the best history of the past is about more than 9 million, which is enough to show that this improvement reduces the cost of the co-scheduling.


In fact, the speed of scheduling does not determine the meaning, but this can give the upper layer of the Application a better foundation, let out more CPU resources to the application, or allow the application to use more of the process, so that the system is busy when running more efficient.


All right, here we go, now that I've reached the limit of my current level.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[NIO] Process optimization from 3 million to 7 million--dawn

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.