Skip to content

Reduce the size of WrappedArray#8842

Merged
diesalbla merged 3 commits intoscala:2.12.xfrom
mkeskells:2.12.x_wrapped_array
Apr 11, 2020
Merged

Reduce the size of WrappedArray#8842
diesalbla merged 3 commits intoscala:2.12.xfrom
mkeskells:2.12.x_wrapped_array

Conversation

@mkeskells
Copy link
Contributor

WrappedArray is used in varargs, and IMO its rare to need the elemTag:ClassTag[T]

As its backed by a ClassValue cache anyway is seems unnecessary to cache it in array as well, and this reduces the size of the WrappedArray by 8 bytes, and removes and object ref from the GC traversal

@scala-jenkins scala-jenkins added this to the 2.12.12 milestone Mar 30, 2020
@mkeskells mkeskells requested a review from retronym March 30, 2020 08:08
@mkeskells mkeskells changed the title reduce the size of WrappedArray [after #8841] reduce the size of WrappedArray Mar 30, 2020
@mkeskells mkeskells force-pushed the 2.12.x_wrapped_array branch from 1cd2901 to b8226e1 Compare April 6, 2020 08:04
@SethTisue SethTisue added library:collections PRs involving changes to the standard collection library performance the need for speed. usually compiler performance, sometimes runtime performance. labels Apr 6, 2020
@retronym
Copy link
Member

retronym commented Apr 6, 2020

Aside from reducing the class footprint to that of Some, it will save allocation of the ClassTag or its lookup in the ClassTag.cache, so LGTM.

scala> val data = new Array[Object](0)
data: Array[Object] = Array()

scala> println(org.openjdk.jol.info.ClassLayout.parseInstance(Some("")).toPrintable)
scala.Some object internals:
 OFFSET  SIZE               TYPE DESCRIPTION                               VALUE
      0     4                    (object header)                           0d 00 00 00 (00001101 00000000 00000000 00000000) (13)
      4     4                    (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4                    (object header)                           42 04 01 f8 (01000010 00000100 00000001 11111000) (-134151102)
     12     4   java.lang.Object Some.value                                (object)
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total


scala> println(org.openjdk.jol.info.ClassLayout.parseInstance(data.toSeq).toPrintable)
scala.collection.mutable.WrappedArray$ofRef object internals:
 OFFSET  SIZE                     TYPE DESCRIPTION                               VALUE
      0     4                          (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
      4     4                          (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4                          (object header)                           43 58 01 f8 (01000011 01011000 00000001 11111000) (-134129597)
     12     1                  boolean ofRef.bitmap$0                            false
     13     3                          (alignment/padding gap)
     16     4   scala.reflect.ClassTag ofRef.elemTag                             null
     20     4       java.lang.Object[] ofRef.array                               []
Instance size: 24 bytes
Space losses: 3 bytes internal + 0 bytes external = 3 bytes total

@retronym retronym changed the title [after #8841] reduce the size of WrappedArray Reduce the size of WrappedArray Apr 7, 2020
@mkeskells mkeskells added the performance:do_not_allocate Changes to avoid object allocations label Apr 10, 2020
@diesalbla diesalbla merged commit 4f9b70d into scala:2.12.x Apr 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

library:collections PRs involving changes to the standard collection library performance:do_not_allocate Changes to avoid object allocations performance the need for speed. usually compiler performance, sometimes runtime performance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants