Github user srowen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/4783#discussion_r26208393
  
    --- Diff: core/src/main/scala/org/apache/spark/util/SizeEstimator.scala ---
    @@ -257,21 +262,52 @@ private[spark] object SizeEstimator extends Logging {
         val parent = getClassInfo(cls.getSuperclass)
         var shellSize = parent.shellSize
         var pointerFields = parent.pointerFields
    +    val sizeCount = Array.fill(fieldSizes.max + 1)(0)
     
    +    // iterate through the fields of this class and gather information.
         for (field <- cls.getDeclaredFields) {
           if (!Modifier.isStatic(field.getModifiers)) {
             val fieldClass = field.getType
             if (fieldClass.isPrimitive) {
    -          shellSize += primitiveSize(fieldClass)
    +          sizeCount(primitiveSize(fieldClass)) += 1
             } else {
               field.setAccessible(true) // Enable future get()'s on this field
    -          shellSize += pointerSize
    +          sizeCount(pointerSize) += 1
               pointerFields = field :: pointerFields
             }
           }
         }
     
    -    shellSize = alignSize(shellSize)
    +    // Based on the simulated field layout code in Aleksey Shipilev's 
report:
    +    // 
http://cr.openjdk.java.net/~shade/papers/2013-shipilev-fieldlayout-latest.pdf
    +    // The code is in Figure 9.
    +    // The simplified idea of field layout consists of 4 parts (see more 
details in the report):
    +    //
    +    // 1. field alignment: HotSpot lays out the fields aligned by their 
size.
    +    // 2. object alignment: HotSpot rounds instance size up to 8 bytes
    +    // 3. consistent fields layouts throughout the hierarchy: This means 
we should layout
    +    // superclass first. And we can use superclass's shellSize as a 
starting point to layout the
    +    // other fields in this class.
    +    // 4. class alignment: HotSpot rounds field blocks up to to 
HeapOopSize not 4 bytes, confirmed
    +    // with Aleksey. see 
https://bugs.openjdk.java.net/browse/CODETOOLS-7901322
    +    //
    +    // The real world field layout is much more complicated. There are 
three kinds of fields
    +    // order in Java 8. And we don't consider the @contended annotation 
introduced by Java 8.
    +    // see the HotSpot classloader code, layout_fields method for more 
details.
    +    // 
hg.openjdk.java.net/jdk8/jdk8/hotspot/file/tip/src/share/vm/classfile/classFileParser.cpp
    +    var alignedSize = shellSize
    +    for (size <- fieldSizes if sizeCount(size) > 0) {
    +      val count = sizeCount(size)
    +      // If there are internal gaps, smaller field can fit in.
    +      alignedSize = math.max(alignedSize, alignSizeUp(shellSize, size) + 
size * count)
    +      shellSize += size * count
    +    }
    +
    +    // we should choose a larger size and clearly alignedSize >= shellSize.
    +    shellSize = alignedSize
    +
    +    // round up instance filed blocks
    +    shellSize = alignSizeUp(shellSize, pointerSize)
    --- End diff --
    
    Can the first arg just be `alignedSize`? I found it slightly funny to see 
the variable assigned twice.
    Otherwise looking good to me though I'm not so familiar with this change.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to