News for hackers. Stuff that matters.
Re:Nim Apocrypha, Vol. I by jstimpfle on Wednesday December 23, 2020 @10:18AM (#25518413)
> Undefined behavior

UB in C has some unfortunate historic problems, and is _probably_ way too complicated. But UB per se is needed for a number of really important optimizations - for example, for array indexing using 32-bit integers you need to be able to assume that index-scaling operations (implemented as multiplications/shifts) don't overflow.

> null terminated strings

null terminated strings are like 10-50% space savers compared to string + length tuples for realistic strings (which are small). They're also easy to pass around and require no bikeshedding what's the best string type.

You can easily make your own type if you want (struct String { const char *buffer; int length; }) or you can just use C++ and one of the more terrible full featured string types with all sorts of badly matching built-in memory management and what not.

(By the way, the issue with SetLength() that I described in my other comment applies to Pascal Strings, too)

(Oh, and Pascal/Delphi also wants you to choose between PChar, String, AnsiString, WideString and what not, and for every little thing you're doing you need to locate and use the proper API for whatever string type you've chosen. Or you need to do conversions before the calls, with all the necessary implicit memory allocations. Enjoy!)

(Oh, this is basically how I achieved a 100x-1000x speedup in a module of a business application in a short Delphi stint. I converted the string data to chunk-allocated bytes with manually computed indexing - instead of allocating thousands of little strings and throwing them back and forth. That reduced the total application startup time (including a lot of code that I never touched) from minutes (in some cases) to something bearable (1-5 secs).

> with the attendant buffer overflows are not fundamental constants of low level programming

Of course they are. If you're not controlling bytes directly, and deciding about data layout and tradeoffs, I wouldn't call that low-level programming. YMMV. But - while my code is never fuzzed - it's been years since I've been bitten by a string bug. The importance of this stuff is way overblown, and knowledge and experience how to program with simple data structures is only found in more obscure corners of the internet. Also, very little in systems programming is about strings.

Re:Nim Apocrypha, Vol. I by wffurr on Wednesday December 23, 2020 @10:23AM (#25518476)
Optimization war stories aside (which is cool, thank you!), this reads like the classic apologetics "C is fine if you just use it correctly". Whereas in practice large C and C++ projects and libraries regularly suffer from severe security vulnerabilities exploited in the wild specifically from buffer overflows and use-after-free bugs.
Re:Nim Apocrypha, Vol. I by jstimpfle on Wednesday December 23, 2020 @11:18AM (#25519142)
There is also "Rust is fine once you know how to not get burnt by the type system, and if you carefully choose your dependencies it will maybe still be able to start a build of your project a month from now, and if you're lucky it will build in less than a lifetime".

Or "Java is fine if your fingers aren't sore yet and if you don't mind the latency spikes for your interactive application or if you're basically programming like C with training wheels and less performance".

;-)

Re:Nim Apocrypha, Vol. I by earwaxauditor on Wednesday December 23, 2020 @12:03PM (#25519607)
At least “this code doesn’t compile” isn’t a potential security vulnerability.
Re:Nim Apocrypha, Vol. I by wffurr on Wednesday December 23, 2020 @03:38PM (#25521922)
That's exactly the point I was trying to make.
Re:Nim Apocrypha, Vol. I by jstimpfle on Wednesday December 23, 2020 @02:56PM (#25521476)
Sure, and projects that never ship aren't either.
Re:Nim Apocrypha, Vol. I by earwaxauditor on Wednesday December 23, 2020 @03:31PM (#25521837)
I think you should lighten up ;)
Re:Nim Apocrypha, Vol. I by kaba0 on Wednesday December 23, 2020 @01:54PM (#25520767)
As for the latter: Or you know, most applications don’t need manual memory management because more often than not, programmers WILL get it wrong. Unless you really need that level of control over the hardware, you ARE better off with a good runtime env
Re:Nim Apocrypha, Vol. I by jstimpfle on Wednesday December 23, 2020 @02:55PM (#25521466)
That may be, but personally I am interested in those applications - GUI work, signal processing, low-latency networking.
Re:Nim Apocrypha, Vol. I by elcritch on Wednesday December 23, 2020 @04:33PM (#25522472)
Nim, with the new ARC garbage collector works well for those situations now (well within a few percent usually). Especially with the `move` and `sink` semantics.

I'm using it in embedded devices and needed a few tight loops a week or two back. With a bit of work and a few odd contortions, I got it to compile to almost the exact type of C code I'd write by hand to run a fast inner loop using preallocated arrays, etc. It turns out that code wasn't noticeably faster than more idiomatic Nim code with a few tweaks to ensure move's were able to be used. So I ended up going back to the more readable/idiomatic version while still roughly doubling the overall speed.

The new `move` semantics with Nim's ARC system really do help make it easier to write fast code. Now I understand why the C++ ecosystem is so bullish on `move` in the C++ stdlib now. It really can take code which looks like straightforward C code but performs closer to painstakingly optimized C code. Mainly since it result in less copying of objects/structs and reducing the number of malloc's. Overall it means less effort to design a fast system.