[llvm] [ProfileData] Use std::vector for ValueData (NFC) (PR #95194)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 12 11:22:37 PDT 2024


================
@@ -847,19 +847,26 @@ void InstrProfValueSiteRecord::merge(InstrProfValueSiteRecord &Input,
   Input.sortByTargetValues();
   auto I = ValueData.begin();
   auto IE = ValueData.end();
+  std::vector<InstrProfValueData> Merged;
+  Merged.reserve(std::max(ValueData.size(), Input.ValueData.size()));
   for (const InstrProfValueData &J : Input.ValueData) {
-    while (I != IE && I->Value < J.Value)
+    while (I != IE && I->Value < J.Value) {
+      Merged.push_back(*I);
       ++I;
+    }
     if (I != IE && I->Value == J.Value) {
       bool Overflowed;
       I->Count = SaturatingMultiplyAdd(J.Count, Weight, I->Count, &Overflowed);
       if (Overflowed)
         Warn(instrprof_error::counter_overflow);
+      Merged.push_back(*I);
       ++I;
       continue;
     }
-    ValueData.insert(I, J);
+    Merged.push_back(J);
   }
+  Merged.insert(Merged.end(), I, IE);
----------------
kazutakahirata wrote:

> nit: I might slightly prefer something like
> 
> ```
> Iter1 = valueData.begin();
> Iter2 = Input.ValueData.begin();
> while(; Iter1 < valueData.end() && Iter2 < valueData.end();) {
>   if (Iter1.Value > Iter2.Value)
>     Merge.push_back(*Iter1);
>   else if (Iter2.Value > Iter1.Value)
>     Merge.push_back(*Iter2)
>   else
>     sum count and push
> }
> append the tail to Merged vector
> ```

Thank you for reviewing this patch!  Let me try to address this loop in a follow-up patch.  I do like the symmetric look, but your approach quietly fixes a possible existing bug.  In the original code, `J.Count` is multiplied by `Weight` only when the same value is present in `I`.  That is, if `J.Value` is not present in `I`, we don't multiply `J.Count` by `Weight`.

https://github.com/llvm/llvm-project/pull/95194


More information about the llvm-commits mailing list