Analysis on the buffer class of IO related in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

IO-Important interfaces

Before you introduce buffer, let's recognize two important interfaces, as shown below:

typeinterface {    Read(p []byteint, err error)}typeinterface {    Write(p []byteint, err error)}

The top two interfaces are defined in the Golang SDK installation directory src/io/io.go. In the back of all involved in IO-related operations, basically implemented the two interfaces, such as:

1.package bufio 中的Reader类2package bytes 中的Reader类与Buffer类3package os 中 的File类,这个实现的最为复杂,主要由于在文件操作中,需要系统底层提供服务。...不再一一列举...

Often heard there is a saying: "Using I/o buffer, to help improve efficiency." But, what I want to ask is, is it really improving efficiency?
Under what circumstances will buffer improve the performance of our program? With this problem, let's look at some of the classes mentioned above.

1. The first class of bytes. Reader

This class implements the IO. Reader interface, but this class does not implement IO. Writer interface. This class has no buffer, why? Because of this class, when initializing, the characters are streamed into the object to be saved, and the Write method is not provided to write a new stream of characters. Therefore, this class does not require buffer.

2. The second class of bytes. Buffer

This class implements IO. Reader and Io.writer interface, this class in the process of writing a byte stream, the use of buffer, how to achieve it?
When the class is initialized, a []byte type of slice is passed into the object, and when the Write method writes a byte stream to the object, write calls its own grow method if there is not enough space for the previous incoming []byte] The slice type of byte is expanded so that the buffer inside will increase in length as the write volume increases. If there is no buffer here, when the write capacity is full, either block or loop write, this will cause the system to die or data is destroyed, when the introduction of buffer, the above two problems resolved. But this solution, there is a hidden danger, that is, if a read dead loop, this will cause memory overflow.

3. A third class Bufio. Reader

This class implements IO. Reader interface, which, when instantiated, needs to pass in an IO. Reader type of variable, this problem comes up, an IO. Reader type of variable, must be the implementation of the Read method, then why it is necessary to load into the Bufio. What about the reader object? Originally, Bufio. The Read method in the reader class, when reading a byte stream, checks the length of the passed []byte type variable space, if the length of the passed-in variable is less than Bufio. The capacity that reader initializes will call IO first. Reader's own read method, which writes the content to Bufio. Reader object, and then copy the value to the incoming []byte variable. The advantage of doing this is that the IO is executing. Reader's Read method, read a few more bytes, which is useful for file-like operations.

4. Fourth class of OS. File

This class implements IO. Writer with the Io.reader class, but somewhat special is the OS. File's Read method and write require the help of a system-level approach to files. It is well known that when a file is read, the read and write methods are not cached, that is, you read a few bytes, depending on the capacity of the variable you passed in, if the capacity is 1, then for the file read, Will be very slow, so will the OS. File object, which is passed to the Bufio. Reader object, this can improve the efficiency to some extent, which is the time? When you call the Read method, the incoming variable capacity is too small to increase the read efficiency. However, the Read method provided by Bufio.reader does not guarantee the same number of characters per read. This is related to the way it is implemented, but does not affect our use, just make sure you receive EOF and stop reading.

Summary of # #
When I/O operation is used, the BUFIO package provides a way to read I/O stream with buffer, in the operation file read, message read, etc., can improve the efficiency to some extent, Bufio class, and did not implement the read and write method from the bottom, It only limits the minimum amount of read. This is the Bufio.reader initialization length.
bytes. The buffer provided by buffer is very powerful and this class not only implements IO. Reader interface, which also implements IO. Writer interface. So the Bytes.buffer object not only can read, but also can be appended to write, the process of writing, the capacity can also be automatically extended, so, the function is very powerful. But in use, pay attention to safety, bytes. Buffer will continue to expand, expand, and eventually will panic.

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.