by MaxBarraclough · 4y ago · 0 comments
> SectorLISP uses what we call an ABC garbage collector and it took only 40 bytes of assembly.
> [...]
> Fast immediate garbage collection with zero memory overhead and perfect heap defragmentation is as easy as ABC when your language guarantees data structures are acyclic.
Neat, but my understanding was that even pure functional languages can't generally make this guarantee, because things like letrec complicate matters. If SectorLISP can take this approach, why doesn't Haskell? (Or does it?)
(LETREC ((IS-ODD? (QUOTE (LAMBDA (X) (CAR X))))
(IS-EVEN? (QUOTE (LAMBDA (X) (NOT (IS-ODD X))))))
(IS-EVEN? (QUOTE (NIL T NIL T))))
The oldskool LISP way of writing that is: ((LAMBDA (IS-ODD? IS-EVEN?)
(IS-EVEN? (QUOTE (NIL T NIL T))))
(QUOTE (LAMBDA (X) (CAR X)))
(QUOTE (LAMBDA (X) (NOT (IS-ODD X)))))
As far as I can tell, letrec is just a nicer syntax for the same thing. So I don't know why it should require any mutability. It works probably because LISP is dynamically scoped. Most languages and modern LISP implementations prefer different scoping models to make the Assoc() equation faster. However that comes at a cost, since it can turn the difficulty of LISP up to 11. Next thing you know, you're discovering things like the Y combinator. So I have major respect for what languages like Haskell and Scheme are doing, because it's incredibly difficult. One of the reasons why I created SectorLISP is because I wanted to have a fun bastion of simplicity when I don't need the more mature tools.SectorLISP doesn't have any kind of internal mutability last time I checked. But I have experimented with it. For example, the Peel() optimization described in the blog post is how we solve the "make Assoc() faster problem". As it's written in the blog post, it's 100% pure. But if you introduce a tiny bit of mutability, it can go even faster on benchmarks even though it doesn't change the complexity class. See https://justine.lol/sectorlisp2/fast.js
I think you’re right about avoiding cycles. I think the parent was talking about a let rec construction for data, e.g. in Haskell:
let funky_list = 1:2:3:4:funky_list
(Note that : is the cons operator).If this is allowed then you may have issues. I think the problem is perhaps that the GC won’t work if you have pointers in the opposite of the normal direction. The fix though is relatively simple: add forwarding pointers (somehow), then after copying from the ‘B’ region to the ‘C’ region you leave behind a forwarding pointer and, if you see it again in your copying, you don’t make a duplicate copy of the data. Then I think you can proceed as before. But representing forwarding pointers efficiently is hard, I suspect.
Actually I think I don’t understand the GC sufficiently. Do you end up with a lot of memory usage after this or a little?
((LAMBDA (D Z) (D Z))
(QUOTE (LAMBDA (X)
(COND
(X ((LAMBDA (Y) (CONS Y Y)) (D (CDR X))))
((QUOTE T) (QUOTE T)))))
(QUOTE (A A A A A A A A)))
This should get you a binary tree about 8 levels deep with T on every leaf, and I think it should use linear memory (in the depth of the tree) but I don’t understand how the GC avoids ending up with an exponential amount of memory being used?As for your example, it uses exponential memory because your example produces exponential data. The only thing I'd need to show is that SectorLISP's ABC garbage collector doesn't grow at a large rate as the input size increases. According to the simulator's "heap" statistic, your example needs 449 doublewords of memory to compute a data structure that's 266 double words in size. That seems linear, but it's not, because if you add a few A's then the gap narrows.
The cool thing about your example is that even though it's generating exponential data, that data structure doesn't require an exponential amount of memory to be stored internally. Currently the SectorLISP simulator only does things like de-duplication for DEFINEs which the simulator reports as "code" size in doublewords. However that's a friendly branch feature and the rest of the time SectorLISP just does plain "what you see is what you get" evaluation. I'm sure much more advanced techniques have been discovered since McCarthy wrote his original paper.
You know more Haskell than I do ;-)
What I had in mind were recursive and mutually recursive functions, defined with letrec, as examples of reference-cycles in functional programming.
Is there actually any Lisp dialect where letrec works for data? I'm not aware of such, at least where it comes to major ones (CL and common implementations of Scheme).
(list-length '#1=(1 2 3 4 . #1#))
But I think that isn’t what you mean.Of course dynamic scoping will bite you earlier or later because of the upward and downward FUNARG problems. See my book (http://t3x.org/lfn/) for lots of entertaining details.
In languages with no mutability at all (not even internal mutability like with these two features), you can't create cycles and refcounting becomes possible, but I'm not aware of non-academic general purpose languages that actually fall in this category.
Esoteric functional languages like Unlambda do make that guarantee, and implementations can use a ref counter as their GC.
I've written more about this here: https://terbium.io/2019/09/cyclic-immutable/
Erlang?
...the terms are immutable and thus there can be no pointers modified on the old heap to point to the young heap
so old objects cannot refer to young objects. This means it doesn't have to deal with write barriers, etc.
This doesn't happen when you've got mutation or laziness.
One language that I'm aware which makes this tradeoff is Erlang.
The linked paper at the bottom of the article explains how things work and have worked for a long time now. The source of cyclic references, and even references from old to new generations, comes from "thunks" (unevaluated lazy computations). Haskell guarantees that a thunk only ever gets evaluated once, and the way this is implemented is that the thunk is mutated with a pointer to the evaluation result.
So the garbage collector needs to deal with mutations, cyclic references, and references from old to new generations. (All that in a language with supposedly immutable data!)
But yes, GCs have evolved in the past decade, so these things are subject to change