Overhaul of Compact Regions (#12455)
authorSimon Marlow <marlowsd@gmail.com>
Fri, 29 Jul 2016 13:11:03 +0000 (14:11 +0100)
committerSimon Marlow <marlowsd@gmail.com>
Wed, 7 Dec 2016 10:59:35 +0000 (10:59 +0000)
commit7036fde9df61b6eae9719c7f6c656778c756bec9
treea9d8eeaaf0d611dc7f29f2d5734b5be8218f32fc
parent4dd6b37fd540ad0243057f4aa29a93590d98de88
Overhaul of Compact Regions (#12455)

Summary:
This commit makes various improvements and addresses some issues with
Compact Regions (aka Compact Normal Forms).

This was the most important thing I wanted to fix.  Compaction
previously prevented GC from running until it was complete, which
would be a problem in a multicore setting.  Now, we compact using a
hand-written Cmm routine that can be interrupted at any point.  When a
GC is triggered during a sharing-enabled compaction, the GC has to
traverse and update the hash table, so this hash table is now stored
in the StgCompactNFData object.

Previously, compaction consisted of a deepseq using the NFData class,
followed by a traversal in C code to copy the data.  This is now done
in a single pass with hand-written Cmm (see rts/Compact.cmm). We no
longer use the NFData instances, instead the Cmm routine evaluates
components directly as it compacts.

The new compaction is about 50% faster than the old one with no
sharing, and a little faster on average with sharing (the cost of the
hash table dominates when we're doing sharing).

Static objects that don't (transitively) refer to any CAFs don't need
to be copied into the compact region.  In particular this means we
often avoid copying Char values and small Int values, because these
are static closures in the runtime.

Each Compact# object can support a single compactAdd# operation at any
given time, so the Data.Compact library now enforces mutual exclusion
using an MVar stored in the Compact object.

We now get exceptions rather than killing everything with a barf()
when we encounter an object that cannot be compacted (a function, or a
mutable object).  We now also detect pinned objects, which can't be
compacted either.

The Data.Compact API has been refactored and cleaned up.  A new
compactSize operation returns the size (in bytes) of the compact
object.

Most of the documentation is in the Haddock docs for the compact
library, which I've expanded and improved here.

Various comments in the code have been improved, especially the main
Note [Compact Normal Forms] in rts/sm/CNF.c.

I've added a few tests, and expanded a few of the tests that were
there.  We now also run the tests with GHCi, and in a new test way
that enables sanity checking (+RTS -DS).

There's a benchmark in libraries/compact/tests/compact_bench.hs for
measuring compaction speed and comparing sharing vs. no sharing.

The field totalDataW in StgCompactNFData was unnecessary.

Test Plan:
* new unit tests
* validate
* tested manually that we can compact Data.Aeson data

Reviewers: gcampax, bgamari, ezyang, austin, niteria, hvr, erikd

Subscribers: thomie, simonpj

Differential Revision: https://phabricator.haskell.org/D2751

GHC Trac Issues: #12455
63 files changed:
compiler/prelude/primops.txt.pp
docs/users_guide/sooner.rst
includes/rts/Flags.h
includes/rts/storage/ClosureMacros.h
includes/rts/storage/Closures.h
includes/stg/MiscClosures.h
libraries/base/Control/Exception.hs
libraries/base/Control/Exception/Base.hs
libraries/base/GHC/IO/Exception.hs
libraries/compact/Data/Compact.hs
libraries/compact/Data/Compact/Internal.hs
libraries/compact/Data/Compact/Serialized.hs
libraries/compact/tests/.gitignore
libraries/compact/tests/all.T
libraries/compact/tests/compact_append.hs
libraries/compact/tests/compact_autoexpand.hs
libraries/compact/tests/compact_bench.hs [new file with mode: 0644]
libraries/compact/tests/compact_bytestring.hs [new file with mode: 0644]
libraries/compact/tests/compact_cycle.hs [new file with mode: 0644]
libraries/compact/tests/compact_cycle.stdout [new file with mode: 0644]
libraries/compact/tests/compact_function.hs [new file with mode: 0644]
libraries/compact/tests/compact_function.stderr [new file with mode: 0644]
libraries/compact/tests/compact_gc.hs [new file with mode: 0644]
libraries/compact/tests/compact_gc.stdout [new file with mode: 0644]
libraries/compact/tests/compact_huge_array.hs [new file with mode: 0644]
libraries/compact/tests/compact_largemap.hs [new file with mode: 0644]
libraries/compact/tests/compact_largemap.stdout [new file with mode: 0644]
libraries/compact/tests/compact_loop.hs
libraries/compact/tests/compact_mutable.hs [new file with mode: 0644]
libraries/compact/tests/compact_mutable.stderr [new file with mode: 0644]
libraries/compact/tests/compact_pinned.hs [new file with mode: 0644]
libraries/compact/tests/compact_pinned.stderr [new file with mode: 0644]
libraries/compact/tests/compact_serialize.hs
libraries/compact/tests/compact_share.hs [new file with mode: 0644]
libraries/compact/tests/compact_share.stdout [new file with mode: 0644]
libraries/compact/tests/compact_simple.hs
libraries/compact/tests/compact_simple.stdout [new file with mode: 0644]
libraries/compact/tests/compact_simple_array.hs
libraries/compact/tests/compact_threads.hs [new file with mode: 0644]
libraries/compact/tests/compact_threads.stdout [new file with mode: 0644]
rts/Compact.cmm [new file with mode: 0644]
rts/Hash.c
rts/Hash.h
rts/Prelude.h
rts/PrimOps.cmm
rts/Printer.c
rts/RtsFlags.c
rts/RtsStartup.c
rts/RtsSymbols.c
rts/Stats.c
rts/StgMiscClosures.cmm
rts/Trace.h
rts/package.conf.in
rts/sm/CNF.c
rts/sm/CNF.h
rts/sm/Evac.c
rts/sm/Sanity.c
rts/sm/Scav.c
rts/sm/ShouldCompact.h [new file with mode: 0644]
rts/sm/Storage.c
rts/win32/libHSbase.def
testsuite/config/ghc
utils/deriveConstants/Main.hs