Skip to content

Commit 8a63f12

Browse files
committed
Reuse buffers in pickler
Each `Pickle` object previously contained an `index` and `entries`, which are only used to populate the raw pickle bytes. Profiles show this as a significant source of allocations in some runs, and since the arrays backing these collections are held until jvm, the memory is likely to survive for several garbage collections and be tenured into an older space. Since the `bytes` array is the actual result of the pickling, this commit moves the other large (mutable!) collections to the pickler phase factory itself, and arranges that they be cleared before use and replaced at the end of each pickler phase (so as not to retain the memory for longer than needed). As a sanity check, each pickle has a field `active` which is used to assert that accesses to both mutable fields is safe -- it's set to false when they're cleared. The new override of `clear` in `AnyRefMap` is safe, since scala itself currently generates calls to `AnyRefMap.clear` when asked to: ``` [nix-shell:/code/scala/sandbox]$ scala -version Scala code runner version 2.12.9 -- Copyright 2002-2019, LAMP/EPFL and Lightbend, Inc. [nix-shell:/code/scala/sandbox]$ cat Test.scala object Test extends App { val map = new collection.mutable.AnyRefMap[String, Int]() map("a") = 0 map.clear() } [nix-shell:/code/scala/sandbox]$ scalac Test.scala -d . [nix-shell:/code/scala/sandbox]$ javap -c Test\$.class | grep '\.clear' 28: invokevirtual scala/scala#87 // Method scala/collection/mutable/AnyRefMap.clear:()V ```
1 parent 5d7e77d commit 8a63f12

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

library/src/scala/collection/mutable/AnyRefMap.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,15 @@ extends AbstractMap[K, V]
426426
this
427427
}
428428

429+
override def clear(): Unit = {
430+
import java.util.Arrays.fill
431+
fill(_keys, null)
432+
fill(_values, null)
433+
fill(_hashes, 0)
434+
_size = 0
435+
_vacant = 0
436+
}
437+
429438
}
430439

431440
object AnyRefMap {

0 commit comments

Comments
 (0)