Move elements instead of copying in RepeatedField::ExtractSubrange.

Motivation: this matters when Element is Cord.
PiperOrigin-RevId: 654813293
pull/17532/head
Evan Brown 4 months ago committed by Copybara-Service
parent d29246f390
commit 6290dcd244
  1. 6
      src/google/protobuf/repeated_field.h

@ -766,15 +766,17 @@ void RepeatedField<Element>::ExtractSubrange(int start, int num,
ABSL_DCHECK_GE(num, 0);
const int old_size = size();
ABSL_DCHECK_LE(start + num, old_size);
Element* elem = unsafe_elements();
// Save the values of the removed elements if requested.
if (elements != nullptr) {
for (int i = 0; i < num; ++i) elements[i] = Get(i + start);
for (int i = 0; i < num; ++i) elements[i] = std::move(elem[i + start]);
}
// Slide remaining elements down to fill the gap.
if (num > 0) {
for (int i = start + num; i < old_size; ++i) Set(i - num, Get(i));
for (int i = start + num; i < old_size; ++i)
elem[i - num] = std::move(elem[i]);
Truncate(old_size - num);
}
}

Loading…
Cancel
Save