News for hackers. Stuff that matters.
Re:Zig self hosted compiler is now capable of building itself by lupire on Saturday April 16, 2022 @11:25AM (#31053052)
I think Zig prefers explicit memory management, because allocations may fail and should be handled explicitly, and because automatic deallocations lead to hard-to-predict lifetimes (excess memory usage, and bugs for resource handles that are destructed at hard to predict moments).

These are things that a "systems language" programmer should put in the work to do correctly/near-optimally, and not ask the compiler to just do something "good enough", like Python would.

Re:Zig self hosted compiler is now capable of building itself by coder543 on Saturday April 16, 2022 @12:08PM (#31053451)
> because automatic deallocations lead to hard-to-predict lifetimes (excess memory usage, and bugs for resource handles that are destructed at hard to predict moments).

I don't really feel this is the case in Rust.

Re:Zig self hosted compiler is now capable of building itself by varajelle on Saturday April 16, 2022 @02:43PM (#31054917)
But it is. If you have a String on the stack, it's memory is only reclaimed at the end of the scope, while it often could be free'd before. This is especially bad in async code around await point since it means the memory need to be kept alive more than needed.
Re:Zig self hosted compiler is now capable of building itself by coder543 on Saturday April 16, 2022 @03:12PM (#31055172)
I disagree. It is substantially and unequivocally better to hold onto memory until the end of scope than to leak memory by default. How can anyone argue that leaking memory is a better default? That’s a ticking time bomb. Maybe someone is so optimistic as to believe they’ll catch every leak before shipping new code?

You can easily add a manual “drop” call in Rust at any point if you want to force an allocation to be freed sooner, but I speak from years of experience using Rust for work when I say that Rust’s RAII model is not problematic in practice. I’m not simply speculating or theorizing, and I have professional experience with a variety of languages at all levels of the stack. I personally don’t mind garbage collectors most of the time, but Rust is great when you need more control.

In C++, RAII can absolutely be problematic because you are able to easily do things that cause undefined behavior by accident, which is arguably worse than either leaking by default or the mere act of holding onto memory for the duration of a scope.

If you can propose a system which cannot be contrived to have any downside, that would be fantastic! In the real world, Rust’s approach to memory management is extremely pragmatic and beneficial. I’m sure someone will eventually improve on Rust’s approach, but “leak by default” isn’t it.

I honestly do enjoy following Zig… it is a fascinating language taking a really interesting approach to solving many problems, but its memory safety story is not where I want it to be yet. Leaking memory by default is technically safe, but it's not exactly endearing.

Re:Zig self hosted compiler is now capable of building itself by guidoism on Saturday April 16, 2022 @06:50PM (#31056802)
I’ve recently been writing personal code that leaks like a sieve. It’s just not worth my time to find every leak when the lifetime of the process is finite and short and it will only ever run on a machine with gigs of memory. I haven’t thought through your question enough but maybe a situation where memory usage would be super high if waiting until the end of scope? I’m probably trying to hard to come up with a situation but I have a gut feeling that freeing mid scope is important under certain circumstances to keep the code simple and understandable.
Re:Zig self hosted compiler is now capable of building itself by coder543 on Saturday April 16, 2022 @07:08PM (#31056903)
> I’m probably trying to hard to come up with a situation but I have a gut feeling that freeing mid scope is important under certain circumstances to keep the code simple and understandable.

I explained in my previous comment that you can explicitly "drop" any value at any time in Rust, if you choose.[0] But if you don't, it will still be dropped at the end of the scope. The developer has control, but the language will watch your back.

[0]: https://doc.rust-lang.org/std/mem/fn.drop.html

Re:Zig self hosted compiler is now capable of building itself by StefanKarpinski on Saturday April 16, 2022 @07:54PM (#31057161)
Heck, this is the entire memory management model of PHP, which I found shocking when I learned it, but makes sense given that the language is intended to generate web pages: just allocate, never reclaim the memory, then kill the process when you’re done.
Re:Zig self hosted compiler is now capable of building itself by coder543 on Saturday April 16, 2022 @08:42PM (#31057405)
Do you have any sources? Maybe some truly ancient version? PHP has done garbage collection (seemingly mixed with some reference counting, like Python) for at least 10 or 15 years... I didn't bother to keep searching for even older information, but nothing I saw indicated that PHP only released memory when the process for a request exited.

I don't even think most mainstream uses of PHP have done the process-per-request model for decades, but I could be wrong.

Re:Zig self hosted compiler is now capable of building itself by tored on Sunday April 17, 2022 @08:34AM (#31060410)
Correct, garbage collector was added to PHP 5.3, prior to 5.3 PHP did only reference counting. PHP 5.3 was released in June of 2009.

Here is a series of articles that describes it in detail

https://www.php.net/manual/en/features.gc.php

Especially look at this example where the same script is executed with and without GC

https://www.php.net/manual/en/features.gc.performance-consid...

Re:Zig self hosted compiler is now capable of building itself by varajelle on Saturday April 16, 2022 @03:41PM (#31055438)
Of course you can call drop() manually, but almost nobody does or even think about it because that's not the way you program in a language with RAII.

Don't get me wrong. I do think that rust and c++ RAII is much more convenient and safe than the C or Zig way.

(I'd even prefer if you could annotate given struct in rust so the compiler could drop them as soon as it's no longer used, but that s not that simple)

Re:Zig self hosted compiler is now capable of building itself by coder543 on Saturday April 16, 2022 @03:50PM (#31055518)
I definitely wish that Non-Lexical Lifetimes would eagerly drop anything that doesn't implement Drop.

It would probably be a breaking change to automatically call an explicit Drop implementation anywhere other than the end of the current scope, so I think that would have to be left as-is. String doesn't implement Drop, so it could easily be dropped eagerly within the scope as soon as it won't be referenced again. Such a change would be roughly equivalent to any of the compiler optimizations that reorder statements in ways that should be unobservable.

Re:Zig self hosted compiler is now capable of building itself by ______-_-______ on Saturday April 16, 2022 @09:00PM (#31057494)
100%. I would actually go further and say every value should be dropped immediately after its last use, including temporaries in the middle of statements, whether or not it implements Drop. Reuse the same rules that NLL uses. Breaking change yes, so do it next edition. It would lower memory usage in general and solve so many little pain points, not least of which is holding strings across await points.
Re:Zig self hosted compiler is now capable of building itself by varajelle on Saturday April 16, 2022 @10:42PM (#31057987)

    fn foo(xx : &Mutex<String>) {
        let lock = xx.lock();
        let ptr = &mut *lock as *mut String;
        unsafe { use_from_c(ptr) }
    }
  
You get the idea: you don't want the lock or the string to be dropped before the unsafe code, even if the actual string is no longer used. That's the breaking change. It's hard to detect automatically, so hard to justify even in an edition.
Re:Zig self hosted compiler is now capable of building itself by coder543 on Sunday April 17, 2022 @11:53AM (#31061717)
I generally agree with you (which is why I made an exception for values with a manual Drop implementation), but to advocate on behalf of the idea… I don’t think it would be too crazy to make a rule that any function that invokes “unsafe” (or is itself defined as an unsafe function) would fall back to the old scope-based drop rules. Worst case scenario is that you’d be dealing with the current level of memory usage efficiency, and you might need to manually call drop if you want something dropped earlier, but in most cases things would be eagerly dropped even sooner.

Outside of uses of unsafe, are there any other serious problems with eagerly dropping values that manually implement Drop? Maybe this fallback mechanism would also have to be invoked simply for casting to a raw pointer anywhere in the function. Either way, a list of exceptions to eager drop would arguably be better than not having eager drop, as long as the list was sound.

It would still be a breaking change, and would definitely require at least being restricted to a new edition. Some people currently use Drop as a kind of scoped “defer”, especially for things like instrumentation, so maybe it would be time for the language to introduce a proper “defer” statement that exists for that purpose instead of making Drop so lazy for everyone.

Re:Zig self hosted compiler is now capable of building itself by vrfvr on Saturday April 16, 2022 @07:33PM (#31057024)
I still don't get why one should do that in the first place
Re:Zig self hosted compiler is now capable of building itself by pcwalton on Saturday April 16, 2022 @11:49PM (#31058309)
This doesn't match the experience of GC'd languages. I've never heard of problems in practice that arose from treating all values present on the stack/registers as GC roots, even if those values are dead in the data flow sense.
Re:Zig self hosted compiler is now capable of building itself by _3u10 on Saturday April 16, 2022 @09:53PM (#31057745)
Stacks are a fixed size. Unless you are running out of stack space there is no point in “freeing” memory from the stack. Where in the stack the stack pointer points has no bearing on stack size for any OS I’m aware of.

I’m assuming by string you mean a stack allocated array of char and not a std::string.

Re:Zig self hosted compiler is now capable of building itself by varajelle on Saturday April 16, 2022 @10:26PM (#31057919)
I meant a std::string on the stack, which is just a handle for more memory on the heap
Re:Zig self hosted compiler is now capable of building itself by kaba0 on Sunday April 17, 2022 @01:44PM (#31062656)
You can just put a single pair of bracket around it and that’s it.

For locks it is seriously the best way to manage them.

Re:Zig self hosted compiler is now capable of building itself by pjmlp on Saturday April 16, 2022 @01:49PM (#31054380)
And as we know from C, every developer is quite capable of taking care of use after free possible bugs.
Re:Zig self hosted compiler is now capable of building itself by Comevius on Saturday April 16, 2022 @02:50PM (#31054985)
The point of C and Zig is that you can solve any kind of problems with them. In particular there is much more to memory reclamation than garbage collectors or Rust's borrow checker. Some of the reclamation schemes offer wait-freedom, or efficiently support linearizable operations.
Re:Zig self hosted compiler is now capable of building itself by pjmlp on Saturday April 16, 2022 @03:02PM (#31055099)
Any kind of problem?

So how do you solve SIMD with C, without language extensions?

C is not a special snowflake.

Re:Zig self hosted compiler is now capable of building itself by Comevius on Saturday April 16, 2022 @04:01PM (#31055613)
SIMD is not a problem, it's hardware. Compilers tend to take advantage of the CPU-specific SIMD registers and instructions, or you can use them explicitly.

Writing generic SIMD code is more portable. C has libraries, and Zig also has vector primitives available through built-in functions.

Re:Zig self hosted compiler is now capable of building itself by pjmlp on Saturday April 16, 2022 @04:14PM (#31055716)
Any language can have libraries, C is not special here, nor is Zig.

In fact, a few GC enabled languages have explicit SIMD support.

Re:Zig self hosted compiler is now capable of building itself by turminal on Saturday April 16, 2022 @03:15PM (#31055193)
Are there languages that "solve" SIMD?
Re:Zig self hosted compiler is now capable of building itself by pjmlp on Saturday April 16, 2022 @04:13PM (#31055704)
Re:Zig self hosted compiler is now capable of building itself by sitkack on Sunday April 17, 2022 @06:06PM (#31064932)
Re:Zig self hosted compiler is now capable of building itself by maleldil on Saturday April 16, 2022 @04:29PM (#31055823)
What can C and Zig do that Rust can't?
Re:Zig self hosted compiler is now capable of building itself by Comevius on Saturday April 16, 2022 @04:49PM (#31055979)
Nothing if unsafe Rust is considered, though Zig does not require you to fight the language nearly as much. This is especially obvious with embedded. Zig's philosophy of no hidden control flow or allocation makes things simple. Simplicity is power.

For certain problems you would want a TLA+ specification for safety and especially liveness either way. It's not like Rust absolutely guarantees correctness in all cases.

Rust sits in a sweet spot between C/Zig and languages like Java, but it's not an appropriate replacement for either of them.

Re:Zig self hosted compiler is now capable of building itself by 3a2d29 on Saturday April 16, 2022 @11:24PM (#31058188)
Certainly Rust sits where C++ is. Zig and Rust are definitely best used for different things, but I think sweating rust off as in between C and Java is inaccurate.
Re:Zig self hosted compiler is now capable of building itself by politician on Saturday April 16, 2022 @05:53PM (#31056428)
This is kind of a trick question because they are all Turing Complete, but so is Assembly. The best way to interpret your question, then, is not “what is possible” but rather “what is simple to express correctly” and “what is simple to get correct eventually”. Those are questions about how tricky it is to get something to compile in the language and which tools exist to help determine whether a compiled program does what’s expected and does not do what’s not expected.
Re:Zig self hosted compiler is now capable of building itself by maleldil on Monday April 18, 2022 @02:10PM (#31074346)
It wasn't a trick question. I'm not asking it in the same tone as someone would ask 'What can C do that Java can't?'.

I believe that Rust can be as low level as you want. If you have to fight the language to accomplish something, it's because you're probably trying to do something unsafe, and Rust will let you do that if you pay the price.

I was looking for examples of real cases where Zig is a better option than Rust. And while Zig is a lovely language, its main points against Rust are that it lets you do unsafe things more easily, and it's easier to learn.

Re:Zig self hosted compiler is now capable of building itself by messe on Saturday April 16, 2022 @02:20PM (#31054703)
The general purpose allocator in the zig standard library has protections against use after free bugs.
Re:Zig self hosted compiler is now capable of building itself by pcwalton on Saturday April 16, 2022 @04:07PM (#31055660)
By quarantining all memory forever. This is not a scalable solution because keeping one allocation in a 4kB page alive will leak the whole rest of the page. And if you don't quarantine all memory forever then use after free comes back. If it were that easy to solve UAF then C++ would have solved it by now.

There is a scalable solution for UAF that doesn't involve introducing a lifetime/region system: garbage collection. Of course, that comes with its own set of tradeoffs.

Re:Zig self hosted compiler is now capable of building itself by AndyKelley on Saturday April 16, 2022 @04:41PM (#31055915)
The problem of a single allocation keeping a 4 KB page alive is something that you might commonly find with the C++ or Rust way of programming that encourages allocating many individual objects on the heap, but in Zig land this is pretty rare. In fact, compare a Zig implementation of a given application with an equivalent in any other language and you will find there is no contest with respect to the size of the memory footprint.

There are many cool possibilities that C++ has never explored and frankly I find your argument unimaginative.

Re:Zig self hosted compiler is now capable of building itself by pcwalton on Saturday April 16, 2022 @11:15PM (#31058149)
An allocator that can leak (4kB - epsilon) of memory for each allocation is broken. It's not a question of whether the language makes such allocations unusual: free() not actually allowing memory to be freed is a violation of the contract of a memory allocator.

From the man page (which I believe quotes ISO C): "The free() function shall cause the space pointed to by ptr to be deallocated; that is, made available for further allocation." If free doesn't do that because another allocation is pointing into that page, that's a violation of the contract. Standard quarantine doesn't violate the spec because the space will eventually become available no matter what, but perpetual quarantine does.

One way to fix the problem would be to round up all allocations to 4kB, but that's very wasteful and slow due to cache and page table traffic; you'd be better off from a performance point of view with a garbage collector. More promising is ARM MTE, though the tag is currently too small to really be called a solution to UAF as opposed to a mitigation. A 64-bit tag would be enough, but I'm not sure what the performance costs of that would be--I wouldn't be surprised if the increased memory traffic makes it slower than a GC.

Re:Zig self hosted compiler is now capable of building itself by gnuvince on Sunday April 17, 2022 @08:12AM (#31060320)
> way of programming that encourages *allocating many individual objects* on the heap

Quoting and bolding. I think this is the thing that I need to change most about my own style of programming.

Re:Zig self hosted compiler is now capable of building itself by pjmlp on Saturday April 16, 2022 @02:34PM (#31054834)
Just like C and C++ debugging allocators, so what is the improvement here?
Re:Zig self hosted compiler is now capable of building itself by charcircuit on Saturday April 16, 2022 @12:12PM (#31053481)
The lifetime of the memory a smart pointer manages is very predictable. It will have the same lifetime as the smart pointer itself (this is ignoring moves).
Re:Zig self hosted compiler is now capable of building itself by RustyConsul on Saturday April 16, 2022 @02:28PM (#31054777)
I think what he's saying is there is a way to carelessly use smart pointers in rust.

pub enum List { Empty, Elem(i32, Box<List>), }

instead of :

pub struct List { head: Link, }

enum Link { Empty, Some(Box<Node>), }

struct Node { elem: i32, next: Link, }

Re:Zig self hosted compiler is now capable of building itself by charcircuit on Saturday April 16, 2022 @10:07PM (#31057820)
Can you explain the difference between these approaches? Is it just that the first example allocates an extra u16 (tag of the tagged union) (ignoring any overhead)?
Re:Zig self hosted compiler is now capable of building itself by tialaramex on Sunday April 17, 2022 @05:25AM (#31059609)
Notice that if I make this alleged "List" with a single data item in it, my data lives in the List object I just made (probably on the stack), but an empty List gets allocated on the heap.

I thought Aria's "Entirely Too Many Lists" tutorial actually tries to build this, but it actually doesn't, she draws you the resulting "list" and then is like, OK, that's clearly not what we want, let's build an actual (bad, slow, wasteful) linked list as our first attempt.

Re:Zig self hosted compiler is now capable of building itself by kaba0 on Sunday April 17, 2022 @01:55PM (#31062747)
I don’t really get your example, or how does it have to do anything with rust? It’s just a bad linked list, isn’t it?
Re:Zig self hosted compiler is now capable of building itself by tialaramex on Sunday April 17, 2022 @02:22PM (#31062964)
Are you talking about the example by RustyConsul? Because I'm not RustyConsul so I can't tell you what specifically RustyConsul intended with this example. Yes this is a bad data structure in Rust, or C, or presumably Zig. In the context of this thread I'm sure Zig's proponents will say that you'd never make this mistake in Zig.
Re:Zig self hosted compiler is now capable of building itself by RustyConsul on Sunday April 17, 2022 @02:54PM (#31063233)
My original point was referencing the use of smart pointers. They are indeed very smart, but can be used stupidly as a bandaid kind of like .clone().

The difference really lies in the fact that i now have some data stored on the stack (The element) and some data stored on the heap because it's recursive. Was just a random example of where it's poor practice. As others have noted, linkedlists are a terrible data structure to begin with.