Go Performance Optimization Tips 9/10

Source: Internet
Author: User

As a built-in type, channels (channel) get a lot of support from the runtime, and its own design is ingenious. But anyway, it's essentially a queue, and locks are unavoidable when multiple goroutine operate concurrently. At some point, this competitive mechanism can lead to performance problems.

Here is an example of simply using the channel to send and receive data, for "accurate" measurement of the transmit and receive operation performance, we put the make channel operation outside, as far as possible to avoid additional consumption.

In the study of the Go Runtime source code implementation, you will see a lot of use of "batch operation" to improve the performance of the sample. Here, we can learn from this, see the effect of comparison.



From the test results, the performance improvement is very high, the visible batch operation is an effective scheme.

In this case, is it possible to use slice instead of an array block? Intuitively, the slice reduces the size of data passing through the channel, reducing data duplication and seems to further improve performance. You might want to do a test.



The result is similar to the previous chapter, slice not only improves performance, but allocates more memory on the heap, which is not worth the candle. Of course, this case is not necessarily absolute, and all performance improvements need to be analyzed according to the context.

Latest news, please scan the code attention

Go Performance Optimization Tips 9/10

Related Article

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.