Hash :
df4d9667
Author :
Date :
2023-12-13T18:28:53
Revert "Optimize HandleAllocator for fast ID churning." This reverts commit b25ffe5a9775cc912a304c8552dd9c097a93420a. Reason for revert: b/316162914 Original change's description: > Optimize HandleAllocator for fast ID churning. > > Instead of calculating ranges of IDs and the overhead with updating > them every allocation/release, store a released ID list in a small > FastVector. > > Optimize the allocate path for the "good case" of no reserved IDs so > that it either pops the last released ID or incriments a next value and > returns it. Release has a similar cost of just a push_back when there > are no reserved IDs. > > This adds a small fixed memory cost due to the FastVector and a dynamic > memory cost of mReleasedList having up to N elements where N is the > maxmimum total handles allocated at one time. > > Bug: angleproject:8434 > Change-Id: I7c5aa126b5303c105cd2464d0d0933b922cc2b8f > Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5101509 > Reviewed-by: Charlie Lao <cclao@google.com> > Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Bug: angleproject:8434 Change-Id: Ide43d787b6942cc6b622e3b5d938bfbbbf3b3ebb No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5120277 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
//
// Copyright 2002 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// HandleAllocator.cpp: Implements the gl::HandleAllocator class, which is used
// to allocate GL handles.
#include "libANGLE/HandleAllocator.h"
#include <algorithm>
#include <functional>
#include <limits>
#include "common/debug.h"
namespace gl
{
struct HandleAllocator::HandleRangeComparator
{
bool operator()(const HandleRange &range, GLuint handle) const { return (range.end < handle); }
};
HandleAllocator::HandleAllocator()
: mBaseValue(1),
mNextValue(1),
mMaxValue(std::numeric_limits<GLuint>::max()),
mLoggingEnabled(false)
{
mUnallocatedList.push_back(HandleRange(1, mMaxValue));
}
HandleAllocator::HandleAllocator(GLuint maximumHandleValue)
: mBaseValue(1), mNextValue(1), mMaxValue(maximumHandleValue), mLoggingEnabled(false)
{
mUnallocatedList.push_back(HandleRange(1, mMaxValue));
}
HandleAllocator::~HandleAllocator() {}
void HandleAllocator::setBaseHandle(GLuint value)
{
ASSERT(mBaseValue == mNextValue);
mBaseValue = value;
mNextValue = value;
}
GLuint HandleAllocator::allocate()
{
ASSERT(!mUnallocatedList.empty() || !mReleasedList.empty());
// Allocate from released list, logarithmic time for pop_heap.
if (!mReleasedList.empty())
{
std::pop_heap(mReleasedList.begin(), mReleasedList.end(), std::greater<GLuint>());
GLuint reusedHandle = mReleasedList.back();
mReleasedList.pop_back();
if (mLoggingEnabled)
{
WARN() << "HandleAllocator::allocate reusing " << reusedHandle << std::endl;
}
return reusedHandle;
}
// Allocate from unallocated list, constant time.
auto listIt = mUnallocatedList.begin();
GLuint freeListHandle = listIt->begin;
ASSERT(freeListHandle > 0);
if (listIt->begin == listIt->end)
{
mUnallocatedList.erase(listIt);
}
else
{
listIt->begin++;
}
if (mLoggingEnabled)
{
WARN() << "HandleAllocator::allocate allocating " << freeListHandle << std::endl;
}
return freeListHandle;
}
void HandleAllocator::release(GLuint handle)
{
if (mLoggingEnabled)
{
WARN() << "HandleAllocator::release releasing " << handle << std::endl;
}
// Try consolidating the ranges first.
for (HandleRange &handleRange : mUnallocatedList)
{
if (handleRange.begin - 1 == handle)
{
handleRange.begin--;
return;
}
if (handleRange.end == handle - 1)
{
handleRange.end++;
return;
}
}
// Add to released list, logarithmic time for push_heap.
mReleasedList.push_back(handle);
std::push_heap(mReleasedList.begin(), mReleasedList.end(), std::greater<GLuint>());
}
void HandleAllocator::reserve(GLuint handle)
{
if (mLoggingEnabled)
{
WARN() << "HandleAllocator::reserve reserving " << handle << std::endl;
}
// Clear from released list -- might be a slow operation.
if (!mReleasedList.empty())
{
auto releasedIt = std::find(mReleasedList.begin(), mReleasedList.end(), handle);
if (releasedIt != mReleasedList.end())
{
mReleasedList.erase(releasedIt);
std::make_heap(mReleasedList.begin(), mReleasedList.end(), std::greater<GLuint>());
return;
}
}
// Not in released list, reserve in the unallocated list.
auto boundIt = std::lower_bound(mUnallocatedList.begin(), mUnallocatedList.end(), handle,
HandleRangeComparator());
ASSERT(boundIt != mUnallocatedList.end());
GLuint begin = boundIt->begin;
GLuint end = boundIt->end;
if (handle == begin || handle == end)
{
if (begin == end)
{
mUnallocatedList.erase(boundIt);
}
else if (handle == begin)
{
boundIt->begin++;
}
else
{
ASSERT(handle == end);
boundIt->end--;
}
return;
}
ASSERT(begin < handle && handle < end);
// need to split the range
auto placementIt = mUnallocatedList.erase(boundIt);
placementIt = mUnallocatedList.insert(placementIt, HandleRange(handle + 1, end));
mUnallocatedList.insert(placementIt, HandleRange(begin, handle - 1));
}
void HandleAllocator::reset()
{
mUnallocatedList.clear();
mUnallocatedList.push_back(HandleRange(1, mMaxValue));
mReleasedList.clear();
mBaseValue = 1;
mNextValue = 1;
}
bool HandleAllocator::anyHandleAvailableForAllocation() const
{
return !mUnallocatedList.empty() || !mReleasedList.empty();
}
void HandleAllocator::enableLogging(bool enabled)
{
mLoggingEnabled = enabled;
}
} // namespace gl