Generate less garbage in serialization.  #3127.

Usage of #compact means that we generate two arrays: one as the input,
one as the output.