[vm, gc] Update old allocation stats when scanning instead of pushing.

Avoids the incremental barrier needing to deal with allocation stats, and avoids a repeated size calculation.

Bug: https://github.com/dart-lang/sdk/issues/34002
Change-Id: I9104ffca30174a3e659c93d20b4888678e8e759a
Reviewed-on: https://dart-review.googlesource.com/68221
Reviewed-by: Zach Anderson <zra@google.com>
diff --git a/runtime/vm/heap/marker.cc b/runtime/vm/heap/marker.cc
index 3d0004d..77e14da 100644
--- a/runtime/vm/heap/marker.cc
+++ b/runtime/vm/heap/marker.cc
@@ -219,13 +219,18 @@
         // First drain the marking stacks.
         VisitingOldObject(raw_obj);
         const intptr_t class_id = raw_obj->GetClassId();
+
+        intptr_t size;
         if (class_id != kWeakPropertyCid) {
-          marked_bytes_ += raw_obj->VisitPointersNonvirtual(this);
+          size = raw_obj->VisitPointersNonvirtual(this);
         } else {
           RawWeakProperty* raw_weak =
               reinterpret_cast<RawWeakProperty*>(raw_obj);
-          marked_bytes_ += ProcessWeakProperty(raw_weak);
+          size = ProcessWeakProperty(raw_weak);
         }
+        marked_bytes_ += size;
+        NOT_IN_PRODUCT(UpdateLiveOld(class_id, size));
+
         raw_obj = work_list_.Pop();
       } while (raw_obj != NULL);
 
@@ -354,14 +359,6 @@
       return;
     }
 
-#ifndef PRODUCT
-    if (RawObject::IsVariableSizeClassId(raw_obj->GetClassId())) {
-      UpdateLiveOld(raw_obj->GetClassId(), raw_obj->Size());
-    } else {
-      UpdateLiveOld(raw_obj->GetClassId(), 0);
-    }
-#endif  // !PRODUCT
-
     PushMarked(raw_obj);
   }
 
diff --git a/runtime/vm/heap/marker.h b/runtime/vm/heap/marker.h
index 6926468..274e99f 100644
--- a/runtime/vm/heap/marker.h
+++ b/runtime/vm/heap/marker.h
@@ -49,7 +49,6 @@
   Heap* heap_;
 
   Mutex stats_mutex_;
-  // TODO(koda): Remove after verifying it's redundant w.r.t. ClassHeapStats.
   uintptr_t marked_bytes_;
 
   friend class MarkTask;