Github user shivaram commented on a diff in the pull request: https://github.com/apache/spark/pull/4783#discussion_r25969952 --- Diff: core/src/main/scala/org/apache/spark/util/SizeEstimator.scala --- @@ -257,21 +262,56 @@ private[spark] object SizeEstimator extends Logging { val parent = getClassInfo(cls.getSuperclass) var shellSize = parent.shellSize var pointerFields = parent.pointerFields + + // we have only four sizes: 1, 2, 4, 8. pointerSize can be 4 or 8. + // we can just use an Array.fill(8 + 1) to hold all the size counting, but we + // can also use a HashMap to store size counting; Here we uses Array. + 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. Aleksey Shiplev also leaders the development of JOL (Java Object + // Layout) tool. The simulated code is also in the JOL. The simplified idea of field layout + // consists of these parts (see more details in the report): + // 1. field alignment: HotSpot lay out the fields aligned by their size. + // 2. object alignment: HotSpot rounds instance size up to 8 bytes + // 3. consistent field layouts throughout the hierarchy: This means we should layout + // superclass first. And we use superclass's shellSize as a start point to layout the + // other fields in this class. + // 4. class alignment: HotSpot rounds field blocks upto 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 filed can fit in. + alignedSize = (alignSizeUp(shellSize, size) + size * count) max alignedSize --- End diff -- This is a code style comment -- In Spark we typically avoid infix operators (https://cwiki.apache.org/confluence/display/SPARK/Spark+Code+Style+Guide#SparkCodeStyleGuide-InfixMethods) You can make this `max(alignedSize, ...)`
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org