Repository: square/leakcanary
File: shark/src/main/java/shark/internal/PathFinder.kt (Kotlin)

Code before:

      val node = when (val referenceMatcher = fieldReferenceMatchers[field.name]) {
        null -> NormalNode(
            objectId = objectId,
            parent = parent,
            refFromParentType = INSTANCE_FIELD,
            refFromParentName = field.name
        )
        is LibraryLeakReferenceMatcher ->
          LibraryLeakChildNode(
              objectId = objectId,
              parent = parent,
              refFromParentType = INSTANCE_FIELD,
              refFromParentName = field.name,
              matcher = referenceMatcher
          )
        is IgnoredReferenceMatcher -> null
      }
      if (node != null) {
        enqueue(node)
      }
    }
  }

  private fun HeapInstance.readAllNonNullFieldsOfReferenceType(): MutableList<HeapField> {
    val hprofGraph = graph // DO NOT REMOVE! Getter performs typecast which is expensive!
    var fieldReader: FieldValuesReader? = null
    val result = mutableListOf<HeapField>()
    var skipBytesCount = 0
    for (heapClass in instanceClass.classHierarchy.toList()) {
      for (fieldRecord in heapClass.readRecord().fields) {
        if (fieldRecord.type != PrimitiveType.REFERENCE_HPROF_TYPE) {
          // Skip all fields that are not references. Track how many bytes to skip
          skipBytesCount += hprofGraph.getRecordSize(fieldRecord)
        } else {
          // Initialize field reader if it's not yet initialized. Replaces `lazy` without synchronization
          if (fieldReader == null) {
            fieldReader = hprofGraph.createFieldValuesReader(readRecord())
          }

          // Skip the accumulated bytes offset
          fieldReader.skipBytes(skipBytesCount)
          skipBytesCount = 0

          val fieldValue = ReferenceHolder(fieldReader.readId())
          if (!fieldValue.isNull) {
            val fieldName = hprofGraph.fieldName(heapClass.objectId, fieldRecord.nameStringId)
            result.add(HeapField(heapClass, fieldName, HeapValue(hprofGraph, fieldValue)))
          }
        }
      }

Review comment (suggestion):
- always put line related comments on the line before, never the end of a line, as those will eventually wrap and comment *after* the code.
- Remove `DO NOT REMOVE!`, avoid scary language that stops people from thinking. Explain instead:

```
// Assigning to local variable to avoid repeated lookup and cast (HeapInstance.graph casts HeapInstance.hprofGraph to HeapGraph in is getter)
val hprofGraph = graph
```

Code after:
        for ((fieldName, referenceMatcher) in referenceMatcherByField) {
          if (!fieldReferenceMatchers.containsKey(fieldName)) {
            fieldReferenceMatchers[fieldName] = referenceMatcher
          }
        }
      }
    }

    val fieldNamesAndValues = instance.readAllNonNullFieldsOfReferenceType()

    fieldNamesAndValues.sortBy { it.second }

    fieldNamesAndValues.forEach { pair ->
      val (objectId, name) = pair
      if (computeRetainedHeapSize) {
        updateDominatorWithSkips(parent.objectId, objectId)
      }

      val node = when (val referenceMatcher = fieldReferenceMatchers[name]) {
        null -> NormalNode(
            objectId = objectId,
            parent = parent,
            refFromParentType = INSTANCE_FIELD,
            refFromParentName = name
        )
        is LibraryLeakReferenceMatcher ->
          LibraryLeakChildNode(
              objectId = objectId,
              parent = parent,
              refFromParentType = INSTANCE_FIELD,
              refFromParentName = name,
              matcher = referenceMatcher
          )
        is IgnoredReferenceMatcher -> null
      }
      if (node != null) {
        enqueue(node)
      }
    }
  }

  private fun HeapInstance.readAllNonNullFieldsOfReferenceType(): MutableList<LongObjectPair<String>> {
    // Assigning to local variable to avoid repeated lookup and cast:
    // HeapInstance.graph casts HeapInstance.hprofGraph to HeapGraph in its getter
    val hprofGraph = graph
    var fieldReader: FieldIdReader? = null
    val result = mutableListOf<LongObjectPair<String>>()
    var skipBytesCount = 0

    for (heapClass in instanceClass.classHierarchyWithoutJavaLangObject()) {
      for (fieldRecord in heapClass.readRecord().fields) {