Ask HN: What piece of code you wrote are you most pride of?
I&#x27;m not particularly proud of any code that I&#x27;ve wrote (looking back, all of it sucks in one way or another), so I&#x27;d rather share a piece of code, that I&#x27;ve written most recently (and maybe receive some constructive criticism, hehe).<p>The piece of code that I&#x27;m speaking of is a variation of Bip Buffer, which itself is a special kind of ring buffer. Ring buffers, also called circular queues, are commonly used for producer-consumer scenarios. The main difference between a typical ring buffer and a Bip Buffer is that the former operates on distinct elements whereas the latter operates on contiguous data (e.g. binary streams).<p>--<p>In short, Bip Buffer is backed by an array, which it splits into two regions: A and B. At first, there&#x27;s only a single region A, and all incoming data is appended to it. Simultaneously, the data is consumed (stripped off) from the beginning of the array, probably by some other process. Region&#x27;s boundaries are updated as the data comes in and goes out (in FIFO fashion), so the region kind of floats from &quot;left&quot; to &quot;right&quot;, akin to a sliding window, but shrinking and growing in lockstep. As the array gets more than half full (region A&#x27;s right boundary crosses the halfway point of the array), it&#x27;s a good time to start thinking about wrapping around. Since now, on each change to region A&#x27;s boundaries the Bip Buffer compares free space on both sides of the region. As soon as there&#x27;s more space to the left than to the right, a second region B is created there (on the left, i.e. in the beginning of the array).<p>Now the newly arriving data is appended to B, but the old data is still consumed from A. This continues until one of two things happen:<p>1) B&#x27;s right boundary hits A&#x27;s left boundary. In this case no more data can be added to the buffer, so the producer has to wait until the consumer catches up.<p>2) All of A&#x27;s data is consumed. Now there&#x27;s only a single non-empty region left, namely B, so we may as well start calling it A and start over.<p>--<p>This aside, my use case is a bit more complicated than just reading a continuous stream of data. I have a messaging system, where nodes exchange large quantities of data with each other. All communication goes through a single TCP connection, and this includes both service messages (like, requests for blocks of data and announcements about having finished downloading certain blocks of data) and the actual data.<p>What I as a well-behaved peer want to be able to do is:<p>(a) Be able to process actual data asynchronously, as there are multiple incoming streams, disk I&#x2F;O, checksum verification, etc., and all of these contend with each other and generally take some time.<p>(b) Process service messages as soon as possible (e.g. in case some peer, that happens to be sending me data, in the midst of the process sends me a request for some other data). In technical terms, I want to minimize latency of my peer&#x27;s requests.<p>A simple way to accomplish this would be to copy blocks of data into memory and enqueue them for delayed, asynchronous processing. This works pretty well for small amounts of data (tens of MB&#x27;s per second), but has some limitations:<p>- Some runtimes incur overhead for each allocated object. E.g. in JVM each object is prepended with several bytes of metadata; references to the object are accounted for for purposes of garbage collection; if asynchronous processing of the object is taking too long, it may be promoted (copied) to a different, long-lived part of the memory (and, depending on the load, &quot;long&quot; may actually mean 50 ms more than usual).<p>- Copying takes time. Obviously, one copy from main memory to disk is absolutely necessary. NIC buffer can be mapped to main memory via DMA, if the OS supports it, or else it&#x27;s one more copy. Why add yet another copy to application process&#x27;s memory?<p>--<p>So my idea is to tweak the Bip Buffer in such a way, that it can hold the data, that&#x27;s going be processed asynchronously, while <i>at the same time</i> continuing to process next messages in the stream. Basically, it remembers, which areas of the buffer are &quot;reserved&quot; for delayed processing (and thus must not be overwritten), and goes on operating as usual and reading subsequent data, until it wraps around and hits the reserved data, and only then it stops. And if the data arrival rate is not too high, then chances are, the reserved data will already be processed and disposed of by the time the buffer wraps around.<p>Link to code: <a href="https:&#x2F;&#x2F;github.com&#x2F;atomashpolskiy&#x2F;bt&#x2F;blob&#x2F;d0f10b7a306c7595efb214dafd1ae52a51068d0d&#x2F;bt-core&#x2F;src&#x2F;main&#x2F;java&#x2F;bt&#x2F;net&#x2F;pipeline&#x2F;InboundMessageProcessor.java#L36" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;atomashpolskiy&#x2F;bt&#x2F;blob&#x2F;d0f10b7a306c7595ef...</a><p>Please keep in mind, that it&#x27;s a POC and was programmed in a single day, so there might be (must be?) some edge cases, that I did not think about, and the code style is a bit rushed (was too busy drawing pictures of this craziness and debugging :)