Live data from Hacker News

Single-file public-domain/open source C libraries with minimal dependencies

github.com

21–30 of 51 posts

Re: Single-file public-domain/open source C libraries with minimal dependencies

#21
post #20

Just proposed one of my projects for inclusion: https://github.com/djcapelis/atomic-ring Lock-free Single Producer, Single Consumer (SPSC) queue. The dependencies include C11 and that's it. No POSIX required, should work on any arch you can find a C11 compiler for.

If all you're doing is passing pointers from one thread to another, lock free is not a very good choice. Try a trivial spinlock based implementation. It will outperform it easily.

I strongly doubt you're going to build a faster thread-safe workqueue by adding spinlocks to it, but if you think what I wrote is too slow, feel free to build something faster and show the speed difference in a benchmark. You can use the code in test/ if you'd like.

The code I wrote does leave an optimization or two on the table. Apple's lockfree queue in GCD is faster than mine because of batching. It also has a ton more features, including being (IIRC) MPSC.

Re: Single-file public-domain/open source C libraries with minimal dependencies

#22
post #20

Earlier quoted context omitted.

If all you're doing is passing pointers from one thread to another, lock free is not a very good choice. Try a trivial spinlock based implementation. It will outperform it easily.

I strongly doubt you're going to build a faster thread-safe workqueue by adding spinlocks to it, but if you think what I wrote is too slow, feel free to build something faster and show the speed difference in a benchmark. You can use the code in test/ if you'd like. The code I wrote does leave an optimization or two on the table. Apple's lockfree queue in GCD is faster than mine because of batching. It also has a ton…

Locking is cheap, switching a pointer around is cheap. The only expensive thing is waking up a thread that is waiting on a lock. Spinlocks avoid that problem, which is why they outperform other implementations in terms of throughput (of course inversely proportional to the amount of work you do during a lock).

Atomic operations have a fixed cost. Spinlocks only really have a cost when contention is high. When you're just moving a pointer to another place, you're not likely to have that much contention to make atomic operations worth it.

Re: Single-file public-domain/open source C libraries with minimal dependencies

#23
post #18

Plug for a tiny header library I wrote https://github.com/rtaycher/debug_print_h It's a little thing for simple printf debugging. I got tired as hell of using formatting strings. It prints line, file, and function, expression and value. Since it stands out /greppable it's easier to delete then random printfs. It has support for different colors(to make things stand out) and uses c99 _Generic to support different type…

But... isn't _Generic part of C11, not C99? You actually require C11. Big difference.

Re: Single-file public-domain/open source C libraries with minimal dependencies

#24
post #18

Plug for a tiny header library I wrote https://github.com/rtaycher/debug_print_h It's a little thing for simple printf debugging. I got tired as hell of using formatting strings. It prints line, file, and function, expression and value. Since it stands out /greppable it's easier to delete then random printfs. It has support for different colors(to make things stand out) and uses c99 _Generic to support different type…

But... isn't _Generic part of C11, not C99? You actually require C11. Big difference.

Oops.

You're absolutely right it's C11.

Re: Single-file public-domain/open source C libraries with minimal dependencies

#25
post #22

Earlier quoted context omitted.

I strongly doubt you're going to build a faster thread-safe workqueue by adding spinlocks to it, but if you think what I wrote is too slow, feel free to build something faster and show the speed difference in a benchmark. You can use the code in test/ if you'd like. The code I wrote does leave an optimization or two on the table. Apple's lockfree queue in GCD is faster than mine because of batching. It also has a ton…

Locking is cheap, switching a pointer around is cheap. The only expensive thing is waking up a thread that is waiting on a lock. Spinlocks avoid that problem, which is why they outperform other implementations in terms of throughput (of course inversely proportional to the amount of work you do during a lock). Atomic operations have a fixed cost. Spinlocks only really have a cost when contention is high. When you're…

I don't think you've got this quite right. Spinlocks require memory barriers. C11 atomics are, among other things, a standardized way to implement cross platform memory barriers without dropping into platform specific assembly.

Re: Single-file public-domain/open source C libraries with minimal dependencies

#26
post #22

Earlier quoted context omitted.

Locking is cheap, switching a pointer around is cheap. The only expensive thing is waking up a thread that is waiting on a lock. Spinlocks avoid that problem, which is why they outperform other implementations in terms of throughput (of course inversely proportional to the amount of work you do during a lock). Atomic operations have a fixed cost. Spinlocks only really have a cost when contention is high. When you're…

I don't think you've got this quite right. Spinlocks require memory barriers. C11 atomics are, among other things, a standardized way to implement cross platform memory barriers without dropping into platform specific assembly.

The only way to test it out is with code. In my own tests spinlocks turned out to provide the highest throughput. I do not care about the problem enough anymore to go try out your implementation. If you implemented a lock free queue, you should be the one testing it out if it actually is better than a simple spinlock.

Re: Single-file public-domain/open source C libraries with minimal dependencies

#27
post #26

Earlier quoted context omitted.

I don't think you've got this quite right. Spinlocks require memory barriers. C11 atomics are, among other things, a standardized way to implement cross platform memory barriers without dropping into platform specific assembly.

The only way to test it out is with code. In my own tests spinlocks turned out to provide the highest throughput. I do not care about the problem enough anymore to go try out your implementation. If you implemented a lock free queue, you should be the one testing it out if it actually is better than a simple spinlock.

[deleted]

Re: Single-file public-domain/open source C libraries with minimal dependencies

#28
post #26

Earlier quoted context omitted.

I don't think you've got this quite right. Spinlocks require memory barriers. C11 atomics are, among other things, a standardized way to implement cross platform memory barriers without dropping into platform specific assembly.

The only way to test it out is with code. In my own tests spinlocks turned out to provide the highest throughput. I do not care about the problem enough anymore to go try out your implementation. If you implemented a lock free queue, you should be the one testing it out if it actually is better than a simple spinlock.

My code is on github with test harnesses for doing benchmarking on it. Yours is... some files on a hard drive that likely show something completely different than you think it does.

I'm sorry, but I don't have time to comment on this thread anymore.

Re: Single-file public-domain/open source C libraries with minimal dependencies

#29
post #24

Earlier quoted context omitted.

But... isn't _Generic part of C11, not C99? You actually require C11. Big difference.

Oops. You're absolutely right it's C11.

Yeah, I wondered about that because I thought VC's C99 implementation is _relatively_ useful but again C11 might never arrive there.

Re: Single-file public-domain/open source C libraries with minimal dependencies

#30
post #6

Uh, this stb library [1] seems to follow a horrible pattern: not only combining your code into a single .c file (which is fine, like the sqlite amalgamation), but putting the code into a single .h file. [2] I've never seen a C or C++ codebase that does this. Maybe you can rely on link-time deduplication, but it will still cause duplicate compilation, and thus increased compile times. I haven't thought it about it lat…

In general, I find the stb libraries practical and technically excellent. I can't see how they could be improved by arbitratily splitting it into a header and c-file.

Best practices are best practises within a specific usage domain. Sometimes the best solution to particular problem constraints is not the one that follows an orthodoxy.

"it will still cause duplicate compilation"

No, there is nothing in general that would cause this. The implementation is written behind a specific preprocessor condition which is not supposed to be a project wide definition.

For example user can write one stb-*-impl.c file that includes the header after the library specific preprocessor definiton.

Post reply on HN