News for hackers. Stuff that matters.
Re:MiniJinja: Learnings from Building a Template Engine in Rust by BonusPlay on Tuesday August 27, 2024 @01:19PM (#41370145)
I'm not a rust expert by any means, but I believe there's a problem with lifetimes. There are many ways to implement double-linked lists (think C++ smart pointers), but when you try to squeeze performance and use references, then it's a fight against borrow checker, which ends with the need to use unsafe rust.

smarter sources: https://rust-unofficial.github.io/too-many-lists/ https://softsilverwind.github.io/rust/2019/01/11/Rust-Linked...

Re:MiniJinja: Learnings from Building a Template Engine in Rust by oneshtein on Tuesday August 27, 2024 @02:01PM (#41370690)
Borrow checker is just a checker, which ensures that programmer doesn't write obviously wrong programs. It's obviously wrong to create cyclic references, even in a language with garbage collector, such as JavaScript. Why you need to fight with checker?
Re:MiniJinja: Learnings from Building a Template Engine in Rust by kbolino on Tuesday August 27, 2024 @02:49PM (#41371292)
Regarding the borrow checker, what you say is not correct. The borrow checker's rules are stricter than absolutely necessary. It is designed to reject all invalid programs, but it also rejects some valid programs.
Re:MiniJinja: Learnings from Building a Template Engine in Rust by oneshtein on Wednesday August 28, 2024 @01:09AM (#41376146)
Yes, rust compiler team improves borrow checker from time, to allow more valid programs to pass the checker. However, in practice, it's always possible to use unsafe code to do the job, and then build a safe façade for it.

In this particular case, borrow checker does it job as designed. So, why to fight it?

Re:MiniJinja: Learnings from Building a Template Engine in Rust by __s on Tuesday August 27, 2024 @03:49PM (#41372152)
garbage collectors detect cycles. you may be thinking of reference counting. CPython includes a gc for cycle detection
Re:MiniJinja: Learnings from Building a Template Engine in Rust by oneshtein on Wednesday August 28, 2024 @01:23AM (#41376218)
Some GC detects cycles, some not (reference counting is example of GC which does not), but even those which detects cycles, may not able to detect all of them or it can be expensive as, for example, in typical Mark&Sweep GC, because it may require to stop the world to perform GC, which is unacceptable for system programming languages, like C, C++, Rust.
Re:MiniJinja: Learnings from Building a Template Engine in Rust by pebal on Wednesday August 28, 2024 @04:36AM (#41377341)
You can have completely pauseless GC tracking, also in Rust.
Re:MiniJinja: Learnings from Building a Template Engine in Rust by LudwigNagasena on Tuesday August 27, 2024 @03:00PM (#41371454)
Why would it be obviously wrong?
Re:MiniJinja: Learnings from Building a Template Engine in Rust by oneshtein on Wednesday August 28, 2024 @01:45AM (#41376326)
When tree will go out of scope, their nodes memory will be reclaimed automatically ("dropping" in Rust terms) by automatic garbage collector built-in into compiler: `drop(root)->drop(leaf_a),drop(leaf_b)`. However, if leaf will have reference to its parent, then an infinite loop occurs: `drop(root)->drop(leaf_a)->drop(root)->drop(leaf_a)...`

The solution is to use an alternative automatic garbage collector instead of compiler built-in: arenas, RC with weak references, or Mark&Sweep GC. Arenas have better performance, because they drop all nodes at once. The easiest way to quickly implement an arena in safe Rust is to use vector (array) of nodes and used indices instead of direct references.

Re:MiniJinja: Learnings from Building a Template Engine in Rust by LudwigNagasena on Wednesday August 28, 2024 @08:33AM (#41378739)
You've said it would be obviously wrong "even in a language with garbage collector, such as JavaScript." But, obviously, most modern GC can handle cyclic references just fine.
Re:MiniJinja: Learnings from Building a Template Engine in Rust by oneshtein on Wednesday August 28, 2024 @10:24AM (#41379880)
But even languages with Tricolour Mark&Sweep GC, which handles cyclic references just fine, it's still possible to make memory leak via a dangling reference to a node in a complex cross-linked graph, because language and GC allows that.

Rust by default forbids cyclic references, by forcing to use trees, which completely avoids the problem.

Re:MiniJinja: Learnings from Building a Template Engine in Rust by LudwigNagasena on Wednesday August 28, 2024 @04:06PM (#41383670)
You can leak memory in any complex project, even if you only use safe Rust.

Linux kernel uses doubly linked lists, Redis uses doubly linked lists, V8 JS engine uses doubly linked lists. Have their authors chosen something obviously wrong?

Re:MiniJinja: Learnings from Building a Template Engine in Rust by oneshtein on Friday August 30, 2024 @03:12AM (#41398380)
Rust uses double linked lists, they are not harder to implement in Rust than in any other language. Moreover, built-in borrow checker will help to implement them properly, without memory leaks or use-after-free. What is your point?
Re:MiniJinja: Learnings from Building a Template Engine in Rust by LudwigNagasena on Friday August 30, 2024 @09:35AM (#41400584)
My point is that it's not obviously wrong to create cyclic references.
Re:MiniJinja: Learnings from Building a Template Engine in Rust by oneshtein on Saturday August 31, 2024 @01:21AM (#41406726)
OK, it's was obviously wrong to create cyclic references few years ago.
Re:MiniJinja: Learnings from Building a Template Engine in Rust by LudwigNagasena on Saturday August 31, 2024 @10:36AM (#41409189)
But doubly linked lists use cyclic references…
Re:MiniJinja: Learnings from Building a Template Engine in Rust by oneshtein on Thursday September 05, 2024 @01:07AM (#41453671)
Double linked lists use double links.