A space leak due to absName
#6080
Labels
performance
Slow type checking, interaction, compilation or execution of Agda programs
type: bug
Issues and pull requests about actual bugs
Milestone
I noticed that use of
absName
can lead to space leaks. Biographical and retainer profiling (for a certain kind of contrived test case) indicated thattelViewUpTo'
retained lots of data that was never used:agda/src/full/Agda/TypeChecking/Telescope.hs
Lines 374 to 381 in f787254
In the penultimate case
a
andb
are used after some potentially expensive computation. What doesabsV
do?agda/src/full/Agda/TypeChecking/Substitute.hs
Lines 1217 to 1218 in f787254
The argument
a
is returned, but not all ofb
, onlyabsName b
. I guessed that all ofb
is retained, and inspection of the Core fortelViewUpTo'
(for GHC 9.4.2 without-foptimise-heavily
) suggests that this is the case:(A specialised variant of the code is similar.) If we replace the code above with the following one, then it appears as if only the name is retained:
This change led to a speed-up of more than 50% for my test case, even though the slower variant of Agda was compiled using
-foptimise-heavily
, and the faster one wasn't.Here is the implementation of
Abs
:agda/src/full/Agda/Syntax/Internal.hs
Lines 241 to 245 in f787254
Note that
absName
is a field of two different constructors. I replaced this definition by the following one, and got a similar speed-up without forcing the name (after fixing one line of code that used the patternAbs{unAbs=ts}
):However, note that in this case the Core code still suggests that
b
is retained:Here is an excerpt of the specialised variant of the code:
I'm not sure exactly what is going on. Is there some later pass of GHC that optimises this code further?
What do you think? Should we use the bang pattern, or the new definition of
Abs
? The new definition ofAbs
might be helpful in other places, but I'm not sure exactly why it works.(Before we commit to any change I think it makes sense to also run the standard library benchmark.)
Edit: I renamed
Abs'
toAbsOrNoAbs
(The Haskell code was not consistent with the Core code.)The text was updated successfully, but these errors were encountered: