Live data from Hacker News

Cosmopolitan Libc: build-once run-anywhere C library

justine.lol

141–150 of 171 posts

Re: Cosmopolitan Libc: build-once run-anywhere C library

#141

Earlier quoted context omitted.

I have the same experience. Stack Overflow is not great when you try to achive something novel or do something new from scratch.

What is a way to fix this? Is this some part of an endless long-cycle of adoption for social networks over some kind of crossing-the-chasm style adoption curve? Or is there a structural way to fix it? Stackoverflow is better than its predecessors, but clearly is much less valuable now than for its first few 5 years or so.

For me SO could be more powerful if more answers would link or refer to documentation/github landing pages supporting/documenting their solution.

Often I see some weird way to implement something in the .NET world, especially Core/.NET 5, search Microsoft docs and find an 5-10-min read article updated last Wednesday that explains in-depth (relatively to a SO post) three different newer way of doing it.

A big weakness of SO, as I see it is aging. Most upvotes and marked-as-answer is most likely also the oldest.

Maybe the page could warn for old answers or highlight newer answers that are gaining traction.

Maybe questions and answers should be encouraged to specify version of tools. Asking identical questions for V1 and V2 of a tool should not be met by "POSSIBLE DUPLICATE" warning or get downvoted to oblivion. Posts shouldn't need "EDIT 1 updated for V1", "EDIT 2 updated for V2", as this is OK with 2 versions but what about 10?

Maybe points (or some score system) should decay over time? Idk. But I think something needs to be done.

Re: Cosmopolitan Libc: build-once run-anywhere C library

#142
post #92

Earlier quoted context omitted.

How is the libc polyglot? I mean don't system call vary widely between windows and Linux?

int ftruncate(int fd, int64_t length) { if (!IsWindows()) { return ftruncate$sysv(fd, length); } else { return ftruncate$nt(fd, length); } } You get the idea. The OS is detected at startup and then checked each time a function is invoked.

Would this not make the implementation inefficient?

Re: Cosmopolitan Libc: build-once run-anywhere C library

#143

This is what I've been working on with RISC-V, and you have no idea how much resistance I get when I try to fix the problems relating to calling conventions, inline assembly and system calls. I don't want to have to explain what I'm doing from the beginning every time I have a complex question about how to make a function call from inline assembly. It has really turned me off from stackoverflow as a whole. Sorry for…

I have the same experience. Stack Overflow is not great when you try to achive something novel or do something new from scratch.

I barely even use SO these days, it's usually more fruitful to engage with people on various official discord/slack/mailing lists.

SO has become a mound of garbage people dig through to find some piece of garbage solution to their garbage problems

Re: Cosmopolitan Libc: build-once run-anywhere C library

#144

Earlier quoted context omitted.

Is there an incentive from the real world to answer questions Apparently so - if you look at the “Customer Questions and Answers” section of Amazon product pages, there is a remarkably high proportion of answers like, “I don’t know, I haven’t opened it yet” and “I’m not sure, I bought it as a gift”.

No, that is sleazy as anything “growth hacking” from Amazon. If you bought product X and some months or years later “Sam” posts a question on Amazon that isn’t answered, Amazon will send you an email with a subject line that is a variation of “Sam has a question for you about Product X, can you help?” and the body would be something along the lines of “Dear foo, Sam wants to know if ? Click here to respond to Sam and…

Very interesting. I had not considered that possibility.

Re: Cosmopolitan Libc: build-once run-anywhere C library

#145

Earlier quoted context omitted.

Is there an incentive from the real world to answer questions Apparently so - if you look at the “Customer Questions and Answers” section of Amazon product pages, there is a remarkably high proportion of answers like, “I don’t know, I haven’t opened it yet” and “I’m not sure, I bought it as a gift”.

Occasionally AMZN will send out questions others have asked about products to you in the form of an email if you have previously purchased the same product. Some people mistake those for questions directly asked to them instead of realizing where their answer will be posted. StackOverflow doesn't do that AFAIK.

Oh. I didn’t think of that. I’m too naïve. Thanks!

Re: Cosmopolitan Libc: build-once run-anywhere C library

#146
post #34

Earlier quoted context omitted.

Author here. WebAssembly has played an important role protecting CI on the Internet and it's cool that it enables us to run LLVM in the browser. I was very surprised when I learned that some people were using it offline outside of browsers, since doing so requires a JVM-like runtime such as wasmtime. Cosmopolitan proves that it's possible to just fix C instead, which might fix all the stuff that's built on top of C t…

Because x86 instruction encoding is a mess . Take a look at this[1] flowchart from the amd manual (vol. 3). That doesn't even tell the whole story; there's a ton of path dependence, and the operand format is quite complicated and has tons of special cases. 1. https://0x0.st/-rce.png

xoreaxeaxeax's videos about how to systematically parse the asm space of x86 really hit home to me how bad the encoding is.

https://www.youtube.com/watch?v=KrksBdWcZgQ

Re: Cosmopolitan Libc: build-once run-anywhere C library

#148
post #137

This is an interesting, and potentially compelling argument that WebAssembly binaries and runtime actually aren’t the future of compile-once-run-anywhere systems software: these support syscalls, sockets, and stdin/stdout —- all of which requires platform-specific glue code to achieve using WebAssembly and standard libc (or JS).

WebAsm is not performance competitive: "We find that the mean slowdown of WebAssembly vs. native across SPEC benchmarks is 1.55× for Chrome and 1.45× for Firefox, with peak slowdowns of 2.5× in Chrome and 2.08× in Firefox." https://arxiv.org/pdf/1901.09056.pdf

x86 isn’t performance competitive either if you’re targeting a CPU that doesn’t run it natively. In fact, it’s much worse than WebAssembly.

Even Apple Rosetta takes roughly a 1.25x-1.50x hit on SPEC compared to native [1], and that’s with ahead-of-time compilation and no software sandboxing, whereas WebAssembly VMs typically use JIT compilation and do provide software sandboxing, both of which come with overhead. In contrast, inNative [2] is a WebAssembly compiler that has ahead-of-time compilation and no software sandboxing, and it can supposedly reach 95% of native performance (no idea if the benchmarks are cherry-picked though). This makes sense: WebAssembly is higher-level, closer to a compiler IR, whereas x86 assembly has already decided on low-level details (like register allocation and calling conventions) that are suboptimal to emulate on another architecture.

And other x86 emulators will do worse than Rosetta. For one thing, Rosetta can take advantage of the Total Store Ordering mode added to M1 chips for the specific purpose of emulating x86 faster. An emulator running on pretty much any other CPU has to emulate a stronger memory model in software on top of a weaker one, which comes with a massive unavoidable penalty for multithreaded workloads. Also, most x86 emulators are just not as efficient as Rosetta, adding an additional penalty.

[1] https://www.anandtech.com/show/16252/mac-mini-apple-m1-teste... [2] https://innative.dev/news/

Re: Cosmopolitan Libc: build-once run-anywhere C library

#149
post #147

Any chance this could be made to work with tcc instead of gcc on Windows? I don't understand enough of how it is tied into gcc and linux to even take a shot at it.

Author here. tcc is tired and chibicc is wired. Check out https://github.com/jart/cosmopolitan/blob/master/third_party... where I've been working on building a 300kb actually portable executable c11 compiler + assembler + linker that supports all the best gnu extensions.

Re: Cosmopolitan Libc: build-once run-anywhere C library

#150
post #89

Earlier quoted context omitted.

I think the right way to add GUI support to this is to use the web. Simply embed an HTTP server in your binary and open the user's default web browser on launch. I have a framework for this here: https://github.com/jdarpinian/web-ui-skeleton

Author here. I agree. Check out redbean which is a single-file distributable web server, built with cosmopolitan: https://justine.lol/redbean/index.html redbean is forking web server (works great on windows) that can serve 1 million+ gzip encoded responses per second (only linux and freebsd are capable of that kind of performance) because APE binaries are isomorphic to the zip format which enables kernelspace copies.…

Yeah, very similar idea. The zip trick is awesome; especially letting the browser do the decompression is brilliant. My version has a couple features you might want: automatically opening the browser when launched and shutting down the server when the last tab is closed, and a debug mode where it serves directly from the filesystem to make editing easier. Man, I can think of so many cool features I'd want to add to this as well:

CGI support. Maybe also an option to embed node or deno, or even tcc for directly executing C source as CGI scripts.

Embedded writable SQLite DBs. Appending would clearly be tricky but probably doable.

PWA support, giving you native notifications, launcher icons, and top-level windows sans browser chrome just like a native GUI app.

Option to morph into various app formats (apk, appx, ipa) and instant install on a connected phone or upload to app stores. (Wait, actually all of these formats are based on zip! So maybe this wouldn't even need that much morphing? Just some extra cruft in the zip file, some code signing BS, and maybe an embedded copy of adb.)

With all these features it might not be tiny anymore but it could make a pretty complete cross platform native app dev environment contained in a single file. Still way smaller than Electron.

Post reply on HN