3 ms·
It looks like ZPL has its own memcpy() implementation which is a work in progress. https://github.com/zpl-c/zpl/blob/dfcb9538967f381363eaef10f9f9c5875cd3b888/co
by jart 6y ago
It looks like ZPL has its own memcpy() implementation which is a work in progress. https://github.com/zpl-c/zpl/blob/dfcb9538967f381363eaef10f9f9c5875cd3b888/code/source/essentials/memory.c#L73 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/memmove-pure.c 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 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.
- zX41ZdbW 6y agoTesting performance of memcpy should be more exhaustive than just running on single CPU: https://github.com/ClickHouse/ClickHouse/issues/18583#issuecomment-793248521 https://github.com/ClickHouse/ClickHouse/issues/18583#issuec...
- jart 6y agoI 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.
- up2isomorphism 6y agoIt 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.
- _dps 6y ago> 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 https://github.com/nothings/stb [1] https://github.com/nothings/single_file_libs https://github.com/nothings/single_file_libs
- jart 6y agoI'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.
- _dps 6y agoOh 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.
- jart 6y agoThanks! 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.