Live data from Hacker News

Why does C have the best file API

maurycyz.com

1–10 of 169 posts

Re: Why does C have the best file API

#6
> However, in other most languages, you have to read() in tiny chunks, parse, process, serialize and finally write() back to the disk. This works, but is verbose and needlessly limited

C has those too and am glad that they do. This is what allows one to do other things while the buffer gets filled, without the need for multithreading.

Yes easier standardized portable async interfaces would have been nice, not sure how well supported they are.

Re: Why does C have the best file API

#7
post #6

> However, in other most languages, you have to read() in tiny chunks, parse, process, serialize and finally write() back to the disk. This works, but is verbose and needlessly limited C has those too and am glad that they do. This is what allows one to do other things while the buffer gets filled, without the need for multithreading. Yes easier standardized portable async interfaces would have been nice, not sure ho…

Wouldn’t we need to implement all of that extra stuff if we really wanted to work with text from files? I have a use case where I do need extra fast text input/output from files. If anyone has thoughts on this, I’d love it.

Re: Why does C have the best file API

#8
I think C# standard library is better. You can do same unsafe code as in C, SafeBuffer.AcquirePointer method then directly access the memory. Or you can do safer and slightly slower by calling Read or Write methods of MemoryMappedViewAccessor.

All these methods are in the standard library, i.e. they work on all platforms. The C code is specific to POSIX; Windows supports memory mapped files too but the APIs are quite different.

Re: Why does C have the best file API

#10
post #6

> However, in other most languages, you have to read() in tiny chunks, parse, process, serialize and finally write() back to the disk. This works, but is verbose and needlessly limited C has those too and am glad that they do. This is what allows one to do other things while the buffer gets filled, without the need for multithreading. Yes easier standardized portable async interfaces would have been nice, not sure ho…

Wouldn’t we need to implement all of that extra stuff if we really wanted to work with text from files? I have a use case where I do need extra fast text input/output from files. If anyone has thoughts on this, I’d love it.

The standard way is to use libraries like libevent, libuv that wraps system calls such as epoll, kqueue etc.

The other palatable way is to register consumer coroutines on a system provided event-loop. In C one does so with macro magic, or using stack switching with the help of tiny bit of insight inline assembly.

Take a look at Simon Tatham's page on coroutines in C.

To get really fast you may need to bypass the kernel. Or have more control on the event loop / scheduler. Database implementations would be the place to look.

Post reply on HN