Bug 1839083 - Avoid out-of-line call to EnsureCapacity for the common case where the capacity is already sufficient. r=nika

In my local macOS arm64 `--enable-release` non-PGO builds, this fix
speeds AppendElement up by a small amount.

In official Nightlies, PGO appears to inline the full EnsureCapacity method,
so this fix may not make a difference in practice.

I noticed this out-of-line call in profiles of querySelectorAll,
in the append loop in Gecko_ContentList_AppendAll.

Before: https://share.firefox.dev/444f581
After: https://share.firefox.dev/3NAvuf9

Differential Revision: https://phabricator.services.mozilla.com/D181350
This commit is contained in:
Markus Stange 2023-06-19 15:58:48 +00:00
parent c0bd0c45c9
commit eb66969fc6
2 changed files with 17 additions and 7 deletions

View File

@ -151,12 +151,10 @@ nsTArray_base<Alloc, RelocationStrategy>::ExtendCapacity(size_type aLength,
template <class Alloc, class RelocationStrategy>
template <typename ActualAlloc>
typename ActualAlloc::ResultTypeProxy
nsTArray_base<Alloc, RelocationStrategy>::EnsureCapacity(size_type aCapacity,
size_type aElemSize) {
// This should be the most common case so test this first
if (aCapacity <= mHdr->mCapacity) {
return ActualAlloc::SuccessResult();
}
nsTArray_base<Alloc, RelocationStrategy>::EnsureCapacityImpl(
size_type aCapacity, size_type aElemSize) {
MOZ_ASSERT(aCapacity > mHdr->mCapacity,
"Should have been checked by caller (EnsureCapacity)");
// If the requested memory allocation exceeds size_type(-1)/2, then
// our doubling algorithm may not be able to allocate it.

View File

@ -435,7 +435,19 @@ class nsTArray_base {
// @return False if insufficient memory is available; true otherwise.
template <typename ActualAlloc>
typename ActualAlloc::ResultTypeProxy EnsureCapacity(size_type aCapacity,
size_type aElemSize);
size_type aElemSize) {
// Do this check here so that our callers can inline it.
if (aCapacity <= mHdr->mCapacity) {
return ActualAlloc::SuccessResult();
}
return EnsureCapacityImpl<ActualAlloc>(aCapacity, aElemSize);
}
// The rest of EnsureCapacity. Should only be called if aCapacity >
// mHdr->mCapacity.
template <typename ActualAlloc>
typename ActualAlloc::ResultTypeProxy EnsureCapacityImpl(size_type aCapacity,
size_type aElemSize);
// Extend the storage to accommodate aCount extra elements.
// @param aLength The current size of the array.