uWebSockets icon indicating copy to clipboard operation
uWebSockets copied to clipboard

Opaque C interface

Open ghost opened this issue 4 years ago • 4 comments

I want to implement an "official" C-API which is to be entirely opaque and minimal and with an LTO-optimized .a static library.

This would allow Swift, Rust and even PyPy to use the library. Sure, it won't be a perfect seamless integration in regards to the event-loop in every case but because uSockets can plug-in different event-loops it can easily be tweaked to allow perfect integration.

For instance I already have implemented seamless, perfect Swift integration with the Apple ecosystem so it can definitely be made for Rust as well.

And if you really want to you can then create wrappers in Rust, Swift to make it look more "normal". I have no interest in this.

Since we have LTO-optimization and LLVM as common base this can all be implemented without any overhead whatsoever and it can even make sense to use C instead of C++ since it is arguably simpler.

ghost avatar Feb 06 '21 19:02 ghost

It has to be uws_create_app(SSL, ...

and uws_app_listen(SSL, ...

And this switching needs to be constant-folded so the header has to define this switching in inlined functions going to proper symbol in the lib,

ghost avatar Feb 08 '21 23:02 ghost

Because uSockets already has GCD event-loop implemented and even has Swift examples working, making something for Swift could be a good first step. Maybe even go so far to create a Swift wrapper of the C wrapper so that it can look like something you would want to work with.

ghost avatar Feb 10 '21 23:02 ghost

I hope my pull request helps to get a step closer, i will use this in some personal projects too. @alexhultman will be nice to collaborate to optimize this capi, this is just 1 day of work, but is usable

cirospaciari avatar Jan 05 '22 00:01 cirospaciari

These are (C wrapper), (Rust using C wrapper) and (original C++) numbers in order:

root@5d057ec32e43:/uWebSockets.js/uWebSockets/uSockets# ./http_load_test 40 localhost 3000
Running benchmark now...
Req/sec: 211158.000000
Req/sec: 212730.000000
^C
root@5d057ec32e43:/uWebSockets.js/uWebSockets/uSockets# ./http_load_test 40 localhost 3000
Running benchmark now...
Req/sec: 211377.250000
Req/sec: 212990.750000
^C
root@5d057ec32e43:/uWebSockets.js/uWebSockets/uSockets# ./http_load_test 40 localhost 3000
Running benchmark now...
Req/sec: 208350.000000
Req/sec: 212954.250000
Req/sec: 212697.750000

There is no significant difference, extremely even performance. This is why zero cost languages kick ass. It's literally identical.

ghost avatar Jan 07 '22 05:01 ghost