Live data from Hacker News

sp.h: Fixing C by giving it a high quality, ultra portable standard library

spader.zone

101–110 of 222 posts

Re: sp.h: Fixing C by giving it a high quality, ultra portable standard library

#101
post #37

Earlier quoted context omitted.

It's fine as a serialization/deserialization primitive for on-disk files, as long as the NULL character is invalid. String tables in most object file formats work like that, a concatenated series of ASCIIZ strings. One byte of overhead (NUL), requires only an offset into one to address a string and you can share strings with common suffixes. It's a very compact layout.

Nothing prevents you from using a shared pool of strings that don't have null terminator. It can even be more efficient, since you don't have the null byte to handle at string end. Depending on the maximum string length you want to support, it doesn't even have to take more space.

How do you represent that pool of strings on-disk?

If we concatenate the raw strings together without the null terminator, either all string references will require a length on top of the offset (25% size penalty for a Elf32_Sym), or we'll need a separate descriptor table that stores string offsets and lengths to index into.

If we prepend strings with a length (let's say LEB128), we'll be at best tied with null-terminated strings because we'd have a byte for the length vs. a byte for the terminator. At worst, we'll have a longer string table because we'd need more than one byte to encode a long string length and we would lose the ability to share string suffixes.

Out of all the jank from a.out and COFF that was eliminated with ELF, that representation for the string table was kept (in fact, the only change was mandating a null byte at the beginning to have the offset 0 indicate a null string). It works fine since the 1970s and doesn't cause undue problems, as nothing prevents a parser to spit out std::string_view instead of const char* for the application code.

Re: sp.h: Fixing C by giving it a high quality, ultra portable standard library

#102
post #11

Earlier quoted context omitted.

Exactly. This shows that "extremely portable" is actually marketing for "It supports a number of platforms. In my opinion, this number is big".

> This shows that "extremely portable" is actually marketing for "It supports a number of platforms. In my opinion, this number is big". The number might just be zero - did anyone check if this compiles? I am trying to track down where the function `sp_mem_allocator_alloc_type` is defined (used in 3x places) but it doesn't appear in the GH search results. I'm not going to clone and build this (too dangerous).

> I'm not going to clone and build this (too dangerous).

Just create a disposable isolated environment, like VM or container, and do it inside? And, yes, does compile.

Re: sp.h: Fixing C by giving it a high quality, ultra portable standard library

#103
post #41

Earlier quoted context omitted.

You could, of course, spend 30 seconds look at the code on Github which you would have to do if you were interested in using it anyway? TRIPLES = \ x86_64-linux-none x86_64-linux-gnu x86_64-linux-musl \ aarch64-linux-none aarch64-linux-gnu aarch64-linux-musl \ aarch64-macos \ x86_64-windows-gnu \ wasm32-freestanding wasm32-wasi Or you could actually try the compliance suite on an architecture and report back to us if…

You've rejected a user. You can't complain that he has no interest in your project at that point. The bridge is burned.

I don't know how the author would feel. But, honestly, for a libc replacement, I'd personally be okay with that ...

If you can't be bothered to look at a Makefile (or ask an AI to look at the Makefile), you are almost certain to be more trouble than any possible benefit you will bring.

Especially in the realm of open source, I'm becoming increasingly comfortable with "If you can't be bothered to jump through even the most minimal of hoops, please get lost."

Re: sp.h: Fixing C by giving it a high quality, ultra portable standard library

#104
post #75

Earlier quoted context omitted.

> I am trying to track down where the function `sp_mem_allocator_alloc_type` is defined A quick glance at the source on github and here you go: https://github.com/tspader/sp/blob/e64697aa649907ce3357a7dd0... `sp_mem_allocator_alloc_type ` is going through a couple of macro resolutions which ends up at `sp_mem_allocator_alloc` > I'm not going to clone and build this (too dangerous). Your computer won't explode just fr…

It looks like I need to update my macOS machine! Thanks for the sanity, and thanks for reading.

Only if you want to move from MacOS Sequoia to Tahoe. Tahoe has the Liquid Glass stuff that people don't like as well as other UX changes that have been controversial.

Apple still do security updates on Sequoia.

Re: sp.h: Fixing C by giving it a high quality, ultra portable standard library

#105
post #56
post #7

Earlier quoted context omitted.

Looks like the default allocator uses mmap(2) for every single allocation , which is horribly inefficient - you map a whole PAGE_SIZE worth of memory for every tiny string. Aside from just wasting memory this will make the TLB very unhappy. It looks like sp_log's string formatting is entirely unbuffered which results in lots of tiny write syscalls.

The point of the library is that you do not call the low level allocation primitive to allocate a single string. Of course, in simple programs which exit immediately, there is no difference between using a page allocator and a heap allocator. In real programs, I use an appropriate allocator for the allocation rather than making arbitrary calls to malloc(). In the sp.h examples, I use the page allocator to keep freest…

q> sp_log() writes directly to an IO writer. An IO writer can be buffered or unbuffered, but is unbuffered by default. This is a feature, not a bug. Have a look through the IO code!

Why is the unbuffered default? Is there any thoughts on this?

Re: sp.h: Fixing C by giving it a high quality, ultra portable standard library

#106

Zig, one of the giants upon whose shoulders this library stands, coined a name for this almost-but-not-quite-UTF encoding: WTF-8 and WTF-16. These encodings mean, simply, the same as their UTF counterpart but allowing unpaired surrogates to pass through. To give credit where credit is due, both WTF-8 and WTF-16 were devised by Simon Sapin [1] and Zig simply picked them up. [1] https://wtf-8.codeberg.page/ sizeof((T){…

The WTF name really lies on the surface, there's no authoritative source of its origin.

I have a wtf.c from 10+ years ago when I was re-implementing Windows-style Unicode handling for some project. You keep running into various quirks, which accumulate and you inevitably arrive at your WTF moment. So WTF as name comes up naturally, no special wit required.

Re: sp.h: Fixing C by giving it a high quality, ultra portable standard library

#107

Just taking a quick look at the atomics section: First, (on unix) it's wrapping pthread mutex. That's part of libc! (Technically it might not be libc.so, but it's still the standard library.) Also, none of the atomics talk about the memory model. You don't _have_ to use the C11 memory model (Linux, for example, doesn't). But if you're not using the C11 memory model and letting the compiler insert fences for you, you…

> Oops... apparently this is vibecoded. Welp, I just wasted ten minutes of my life reviewing slop that I'm not going to get back.

you interested in project and spent some time researching it, but stop when understand that it is vibe-coded (be it or not)?

Why care if it is interesting to you?

Re: sp.h: Fixing C by giving it a high quality, ultra portable standard library

#108
post #106

Zig, one of the giants upon whose shoulders this library stands, coined a name for this almost-but-not-quite-UTF encoding: WTF-8 and WTF-16. These encodings mean, simply, the same as their UTF counterpart but allowing unpaired surrogates to pass through. To give credit where credit is due, both WTF-8 and WTF-16 were devised by Simon Sapin [1] and Zig simply picked them up. [1] https://wtf-8.codeberg.page/ sizeof((T){…

The WTF name really lies on the surface, there's no authoritative source of its origin. I have a wtf.c from 10+ years ago when I was re-implementing Windows-style Unicode handling for some project. You keep running into various quirks, which accumulate and you inevitably arrive at your WTF moment. So WTF as name comes up naturally, no special wit required.

That might be possible, but Simon Sapin was who tried to specify what exactly are WTF encodings so that should mark sort of milestone.

Re: sp.h: Fixing C by giving it a high quality, ultra portable standard library

#110
post #89

Earlier quoted context omitted.

> Your computer won't explode just from downloading and compiling some C code, don't worry ;) This is the first time I ever saw anyone dismissing the risk of downloading and running stuff off the internet. "Don't worry".

Compiling

"I have no idea how that rm -rf $HOME ended up in the Makefile"
Post reply on HN