News for hackers. Stuff that matters.
Re:ZPL: (Almost) C99 Powerkit by jart on Saturday March 20, 2021 @06:20PM (#26526475)
It looks like ZPL has its own memcpy() implementation which is a work in progress. https://github.com/zpl-c/zpl/blob/dfcb9538967f381363eaef10f9... That leads me to believe feedback is desired so assuming the author posted this as a Show HN kind of thread, here's some of what I've learned implementing that function.

It goes pretty quick on the machines I've used so far to have an indirect branch with overlapping moves. Here's what that looks like in "pure c" for gcc and clang. https://github.com/jart/cosmopolitan/blob/master/libc/str/me... memcpy is hard to implement in pure c99 because aliasing pointers to non-char types can trigger ubsan errors. With rep movsb it's a good idea to check cpuid for erms support. In that case it'll be fastest for bigger moves but is still slower for small moves. See the chart at https://justine.lol/cosmopolitan/index.html which compares various methods. Also take into consideration that if you call zpl_memcpy() the compiler doesn't know that's memcpy() so it can't perform some of its inlining magic at the call site. So if you want things like null propagation it might be better to do that as a macro.

Regarding array.c I implemented a bunch of similar routines myself and eventually chose to not worry about capacity and just call realloc every time. I'm not sure if the standard specifies this, but I'm pretty sure every realloc implementation in practice, under the hood, should track the capacity and grow it at two power sizes. So even if you're just constantly appending by one char the cost should amortize itself out to be cheap in the long run.

Re:ZPL: (Almost) C99 Powerkit by up2isomorphism on Saturday March 20, 2021 @07:29PM (#26526866)
It is very interesting to see your carefully choosing a specific layer of abstraction to somehow find the optimize both portability and performance. I agree that C99 might not always the right choice.

BTW, I am not the ZPL author, but I collect different C libraries as a personal stash of toolkits, and thought this might be interesting to other C programmers.

Re:ZPL: (Almost) C99 Powerkit by _dps on Saturday March 20, 2021 @09:52PM (#26527530)
> I collect different C libraries as a personal stash of toolkits

Might you have a write-up or link for these, and if so might you be willing to share it?

I have been using Sean Barrett's libraries [0], as well as his curated list of other people's single-header libraries [1], and, like you, I am always on the lookout for new things to add to the collection :)

[0] https://github.com/nothings/stb

[1] https://github.com/nothings/single_file_libs

Re:ZPL: (Almost) C99 Powerkit by up2isomorphism on Sunday March 21, 2021 @07:05PM (#26534978)
Yes, I probably will get some reviews on some my experiences with them later.

I also use stb.h, and very impressed that one can put so many things together in one file. Essentially I feel C is very good at doing what most other languages are struggling at - make code running 99% of the time fast, but when it comes to that rest 1%, it takes a lot of repetitive works. I think an "essential-collection" could greatly reduce the unnecessary effort.

Re:ZPL: (Almost) C99 Powerkit by jart on Sunday March 21, 2021 @03:05AM (#26528657)
I'm glad you brought up Sean Barrett's work since the ZPL codebase references it so STB was likely a source of influence for Dominik. It's one of those projects where I just see its impact everywhere I look and I believe the success is well-deserved.
Re:ZPL: (Almost) C99 Powerkit by _dps on Sunday March 21, 2021 @12:47PM (#26531752)
Oh hey, you made APE and redbean! Very cool projects, and something I always wondered if it was possible (I once tried a different strategy, failed, and then never tried again). I was very pleased to see someone had succeeded.
Re:ZPL: (Almost) C99 Powerkit by jart on Sunday March 21, 2021 @03:57PM (#26533397)
Thanks! I talked with one other person so far who discovered the idea before me. I'm glad I was able to contribute running the marathon it required. All we need to do now is make it simple and universally accessible to everyone who needs it.
Re:ZPL: (Almost) C99 Powerkit by zX41ZdbW on Saturday March 20, 2021 @06:49PM (#26526648)
Testing performance of memcpy should be more exhaustive than just running on single CPU: https://github.com/ClickHouse/ClickHouse/issues/18583#issuec...
Re:ZPL: (Almost) C99 Powerkit by jart on Saturday March 20, 2021 @07:18PM (#26526797)
I don't feel like you evaluated my work fairly because you used a modified version of Cosmopolitan memcpy() that had the CPUID checks removed, and then you judged it negatively for not having the CPUID checks.