Buffered Streams

I’ve been working on an application using GNAT.Sockets with calls to Receive_Socket and Send_Socket. I’ve been hesitant to use the Stream interface because I want writes coalesced into reasonably large (~64k) buffers for performance reasons.

I have my own buffered stream implemention right now, but I’m wondering if others have better solutions.

AFAIK recent GNAT has Ada.Streams.Storage.[Un]Bounded. Probably they were added to support custom 'Image attributes of Ada 2022 and are parts of standard now. Take a look.

3 Likes

and they are in GCC >= 11.

Don’t know how I missed that. Ada.Streams.Storage.Bounded was a nice easy replacement for my custom buffers with no change in performance and less code to maintain. Thanks!

One of the reasons for not using streams would be if you wanted a non-blocking API.
Basically, you “select” or “epoll” on the socket (and others) until it signals there is at least one byte to read, you read as much as is available into your local buffer, and go back to waiting if you do not have enough. This only works if you know how many bytes you actually expect, but this seems to often be the case. Having such a non-blocking API can result in significant performance improvement if your application in listening on several sockets, or can done something else in the meantime (more efficient than using dozens or hundreds of tasks).

I also did not know about Ada.Streams.Storage.*, so thanks for that

Yep, I’m aware of the tradeoffs of non-blocking calls. In this application, blocking is acceptable.