[llvm] [ProfData] Improve efficiency of reader (PR #169730)

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 26 14:00:10 PST 2025


================
@@ -401,12 +401,27 @@ std::error_code ProfileSymbolList::read(const uint8_t *Data,
   const char *ListStart = reinterpret_cast<const char *>(Data);
   uint64_t Size = 0;
   uint64_t StrNum = 0;
----------------
vitalybuka wrote:

Will this work?
```
std::error_code ProfileSymbolList::read(const uint8_t *Data,
                                        uint64_t ListSize) {
  auto ForEachStr = [&](function_ref<void(StringRef Str)> Fn) {
    const char *ListStart = reinterpret_cast<const char *>(Data);
    uint64_t Size = 0;
    uint64_t StrNum = 0;
    while (Size < ListSize && StrNum < ProfileSymbolListCutOff) {
      StringRef Str(ListStart + Size);
      Fn(Str);
      Size += Str.size() + 1;
      StrNum++;
    }
    return std::make_pair(Size, StrNum);
  };

  // Scan forward to see how many elements we expect.
  uint64_t ExpectedCount = ForEachStr([](StringRef Str) {}).second;
  reserve(ExpectedCount);

  uint64_t Size = ForEachStr([&](StringRef Str) { add(Str); }).first;

  assert(ExpectedCount == size());

  if (Size != ListSize && size() != ProfileSymbolListCutOff)
    return sampleprof_error::malformed;
  return sampleprof_error::success;
}
```

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


More information about the llvm-commits mailing list