Pool Subzero constant data

Previously each constant would cause new memory to be allocated. This change makes us search through the existing constants to check if a suitable one already exists. Bug: b/178661423 Change-Id: I1315c80f019ff51e96245238100aa672d5e74930 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/52229 Kokoro-Result: kokoro <noreply+kokoro@google.com> Tested-by: 's avatarNicolas Capens <nicolascapens@google.com> Reviewed-by: 's avatarAntonio Maiorano <amaiorano@google.com> Commit-Queue: Nicolas Capens <nicolascapens@google.com>
parent 3d26cfc4
...@@ -833,6 +833,25 @@ public: ...@@ -833,6 +833,25 @@ public:
const void *addConstantData(const void *data, size_t size, size_t alignment = 1) const void *addConstantData(const void *data, size_t size, size_t alignment = 1)
{ {
// Check if we already have a suitable constant.
for(const auto &c : constantsPool)
{
void *ptr = c.data.get();
size_t space = c.space;
void *alignedPtr = std::align(alignment, size, ptr, space);
if(space < size)
{
continue;
}
if(memcmp(data, alignedPtr, size) == 0)
{
return alignedPtr;
}
}
// TODO(b/148086935): Replace with a buffer allocator. // TODO(b/148086935): Replace with a buffer allocator.
size_t space = size + alignment; size_t space = size + alignment;
auto buf = std::unique_ptr<uint8_t[]>(new uint8_t[space]); auto buf = std::unique_ptr<uint8_t[]>(new uint8_t[space]);
...@@ -840,15 +859,27 @@ public: ...@@ -840,15 +859,27 @@ public:
void *alignedPtr = std::align(alignment, size, ptr, space); void *alignedPtr = std::align(alignment, size, ptr, space);
ASSERT(alignedPtr); ASSERT(alignedPtr);
memcpy(alignedPtr, data, size); memcpy(alignedPtr, data, size);
constantData.emplace_back(std::move(buf)); constantsPool.emplace_back(std::move(buf), space);
return alignedPtr; return alignedPtr;
} }
private: private:
struct Constant
{
Constant(std::unique_ptr<uint8_t[]> data, size_t space)
: data(std::move(data))
, space(space)
{}
std::unique_ptr<uint8_t[]> data;
size_t space;
};
std::array<const void *, Nucleus::CoroutineEntryCount> funcs = {}; std::array<const void *, Nucleus::CoroutineEntryCount> funcs = {};
std::vector<uint8_t, ExecutableAllocator<uint8_t>> buffer; std::vector<uint8_t, ExecutableAllocator<uint8_t>> buffer;
std::size_t position; std::size_t position;
std::vector<std::unique_ptr<uint8_t[]>> constantData; std::vector<Constant> constantsPool;
}; };
#ifdef ENABLE_RR_PRINT #ifdef ENABLE_RR_PRINT
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment