RE: Potential improvement in compacting GC

2021-07-14 Thread Simon Peyton Jones via ghc-devs
Thanks Omer

I had an interesting conversation with Steve Blackburn, the brains behind
the MMTk memory management toolkit recently
https://www.mmtk.io/

MMTk is designed to be a re-usable, open-source garbage collector, specifically
designed to be usable with lots of languages. In principle this is a great
idea: GC is such a big field that no runtime (GHC's included) can ever devote
enough effort to GC to do a really state of the art job.  It makes sense for
one bunch of people to stellar GC and another bunch to simply reuse their
work.

Of course, the interface between the GC and the mutator, scheduler, etc
is particularly intimate.  Teasing them apart in GHC would be a significant
task, and success would not be guaranteed.

But Steve is interested in working on this, with help from our end, perhaps
initially with a student (or volunteer) project or two.

If it worked, it'd be cool.  

Here's a talk about MMTk: https://www.youtube.com/watch?v=3L6XEVaYAmU

Simon


|  -Original Message-
|  From: ghc-devs  On Behalf Of Ömer Sinan
|  Agacan
|  Sent: 14 July 2021 07:27
|  To: ghc-devs 
|  Subject: Re: Potential improvement in compacting GC
|  
|  Two other ideas that should improve GHC's compacting GC much more
|  significantly. I've implemented both of these in another project and
|  the results were great. In a GC benchmark (mutator does almost no work
|  other than allocating data using a bump allocator), first one reduced
|  Wasm instructions executed by 14%, second one 19.8%.
|  
|  Both of these ideas require pushing object headers to the mark stack
|  with the objects, which means larger mark stacks. This is the only
|  downside of these algorithms.
|  
|  - Instead of marking and then threading in the next pass, mark phase
|  threads
|all fields when pushing the fields to the mark stack. We still need
|  two other
|passes: one to unthread headers, another to move the objects. (we
|  can't do
|both in one pass, let me know if you're curious and I can elaborate)
|  
|This has the same number of passes as the current implementation,
|  but it only
|visits object fields once. Currently, we visit fields once when
|  marking, to
|mark fields, then again in `update_fwd`. This implementation does
|  both in one
|pass over the fields. `update_fwd` does not visit fields.
|  
|This reduced Wasm instructions executed by 14% in my benchmark.
|  
|  - Marking phase threads backwards pointers (ignores forwards
|  pointers). Then we
|do one pass instead of two: for a marked object, unthread it (update
|forwards pointers to the object's new location), move it to its new
|  location,
|then thread its forwards pointers. This completely eliminates one of
|  the 3
|passes, but fields need to be visited twice as before (unlike the
|  algorithm
|above).
|  
|I think this one is originally described in "An Efficient Garbage
|  Compaction
|Algorithm", but I found that paper to be difficult to follow. A
|  short
|description of the same algorithm exists in "High-Performance
|  Garbage
|Collection for Memory-Constrained Environments" in section 5.1.2.
|  
|This reduced Wasm instructions executed by 19.8% in my benchmark.
|  
|In this algorithm, fields that won't be moved can be threaded any
|  time before
|the second pass (pointed objects need to be marked and pushed to the
|  mark
|stack with headers before threading a field). For example, in GHC,
|  mut list
|entries can be threaded before or after marking (but before the
|  second pass)
|as IIRC mut lists are not moved. Same for fields of large objects.
|  
|  As far as I can see, mark-compact GC is still the default when max
|  heap size is specified and the oldest generation size is (by default)
|  more than 30% of the max heap size. I'm not sure if max heap size is
|  specified often (it's off by default), so not sure what would be the
|  impact of these improvements be, but if anyone would be interested in
|  funding me to implement these ideas (second algorithm above, and the
|  bitmap iteration in the previous email) I could try to allocate one or
|  two days a week to finish in a few months.
|  
|  Normally these are simple changes, but it's difficult to test and
|  debug GHC's RTS as we don't have a test suite readily available and
|  the code is not easily testable. In my previous implementations of
|  these algorithms I had unit tests for the GC where I could easily
|  generate arbitrary graphs (with cycles, backwards ptrs, forwards ptrs,
|  ptrs from/to roots etc.) and test GC in isolation. Implementation of
|  (2) took less than a day, and I didn't have to debug it more once the
|  tests passed. It's really unfortunate that GHC's RTS makes this kind
|  of thing difficult..
|  
|  Ömer
|  
|  Ömer Sinan Ağacan , 7 Oca 2021 Per, 20:42
|  tarihinde şunu yazdı:
|  >
|  > Hello,
|  >
|  > I recently implemented the algorithm used by GHC's compacting GC in
|  > another pr

Re: Potential improvement in compacting GC

2021-07-14 Thread YueCompl via ghc-devs
Greetings!

I'd like to take this opportunity to ask you experts about feasibility / 
technology-readiness of distributed GC, that I've recently been pondering with 
the idea to have a distributed GC managing shared heap across multiple server 
nodes, those inter-connected through fast ethernet. 

I'm implementing an array database system for internal use, each array is 
constrained to flat address space so no GC is required within one. But the 
number of arrays are unexpectedly large as my use cases becoming more apparent. 
Managing relations between those arrays (i.e. meta data) has appeared as far 
beyond the computational capacity of a single physical PC server (even powerful 
ones in today's market). We are working around this limitation by restricting 
meta data to be mappable to directory structure of underlying filesystem, but 
it's quite limiting from business perspective. 

We use FUSE filesystem driver to handle fine grained data coherence control 
over these many small arrays, to be accessed by many concurrent client 
machines,  by exposing a large virtual file for mmap on each client, to 
efficiently leverage os kernel pages as good shared cache storage, backing 
multiple processes running on each client os.

I'd imagine shared heap with https://hackage.haskell.org/package/compact 
 and a GC to have structural meta 
data managed likewise, so each client can mmap the entire shared heap, but only 
load relevant kernel pages on demand of the data nodes as it explores the 
information graph. This way the native programming language assumes the role of 
Data Manipulation Language as well, and Query Language can be simply some 
optimized data structures wrapped with accessing lib functions, more 
importantly the database system will have great horizontal scalability. And the 
whole architecture will have little technical debt as it seems so far.

So how feasible it is that you see? I'm aware that [compact] is still some 
experimental, and not even portable across GHC compilations yet, but it seems 
solvable with enough effort put in.

Thanks,
Compl


> On 2021-07-14, at 16:12, Simon Peyton Jones via ghc-devs 
>  wrote:
> 
> Thanks Omer
> 
> I had an interesting conversation with Steve Blackburn, the brains behind
> the MMTk memory management toolkit recently
> https://www.mmtk.io/
> 
> MMTk is designed to be a re-usable, open-source garbage collector, 
> specifically
> designed to be usable with lots of languages. In principle this is a great
> idea: GC is such a big field that no runtime (GHC's included) can ever devote
> enough effort to GC to do a really state of the art job.  It makes sense for
> one bunch of people to stellar GC and another bunch to simply reuse their
> work.
> 
> Of course, the interface between the GC and the mutator, scheduler, etc
> is particularly intimate.  Teasing them apart in GHC would be a significant
> task, and success would not be guaranteed.
> 
> But Steve is interested in working on this, with help from our end, perhaps
> initially with a student (or volunteer) project or two.
> 
> If it worked, it'd be cool.  
> 
> Here's a talk about MMTk: https://www.youtube.com/watch?v=3L6XEVaYAmU
> 
> Simon
> 
> 
> |  -Original Message-
> |  From: ghc-devs  On Behalf Of Ömer Sinan
> |  Agacan
> |  Sent: 14 July 2021 07:27
> |  To: ghc-devs 
> |  Subject: Re: Potential improvement in compacting GC
> |  
> |  Two other ideas that should improve GHC's compacting GC much more
> |  significantly. I've implemented both of these in another project and
> |  the results were great. In a GC benchmark (mutator does almost no work
> |  other than allocating data using a bump allocator), first one reduced
> |  Wasm instructions executed by 14%, second one 19.8%.
> |  
> |  Both of these ideas require pushing object headers to the mark stack
> |  with the objects, which means larger mark stacks. This is the only
> |  downside of these algorithms.
> |  
> |  - Instead of marking and then threading in the next pass, mark phase
> |  threads
> |all fields when pushing the fields to the mark stack. We still need
> |  two other
> |passes: one to unthread headers, another to move the objects. (we
> |  can't do
> |both in one pass, let me know if you're curious and I can elaborate)
> |  
> |This has the same number of passes as the current implementation,
> |  but it only
> |visits object fields once. Currently, we visit fields once when
> |  marking, to
> |mark fields, then again in `update_fwd`. This implementation does
> |  both in one
> |pass over the fields. `update_fwd` does not visit fields.
> |  
> |This reduced Wasm instructions executed by 14% in my benchmark.
> |  
> |  - Marking phase threads backwards pointers (ignores forwards
> |  pointers). Then we
> |do one pass instead of two: for a marked object, unthread it (update
> |forwards pointers to the object's new location), move it to its new