Skip to content

Commit

Permalink
use ExternalMemoryAdjuster on zlib allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 13, 2024
1 parent 157fe35 commit d181608
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/workerd/api/node/zlib-util.c++
Original file line number Diff line number Diff line change
Expand Up @@ -802,9 +802,19 @@ void* ZlibUtil::Allocator::AllocForZlib(void* data, uInt items, uInt size) {

void* ZlibUtil::Allocator::AllocForBrotli(void* opaque, size_t size) {
auto* thisAllocator = static_cast<Allocator*>(opaque);
auto memory = kj::heapArray<uint8_t>(size);
auto begin = memory.begin();
thisAllocator->allocations.insert(begin, kj::mv(memory));
kj::Maybe<jsg::ExternalMemoryAdjustment> memory{};
auto isolate = v8::Isolate::GetCurrent();
if (isolate != nullptr) {
auto& js = jsg::Lock::from(isolate);
memory = js.getExternalMemoryAdjustment(size);
}
auto data = kj::heapArray<uint8_t>(size);
auto begin = data.begin();
thisAllocator->allocations.insert(begin,
{
.data = kj::mv(data),
.memory = kj::mv(memory),
});
return begin;
}

Expand Down
14 changes: 10 additions & 4 deletions src/workerd/api/node/zlib-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,23 @@ class ZlibUtil final: public jsg::Object {
ZlibUtil() = default;
ZlibUtil(jsg::Lock&, const jsg::Url&) {}

// A custom allocator to be used by the zlib and brotli libraries
// The current implementation stores allocations in a hash map.
// TODO: Use an arena allocator implementation instead of hashing pointers in order to improve performance
// A custom allocator to be used by the zlib and brotli libraries.
// The allocator should not and can not safely hold a reference to the jsg::Lock
// instance. Therefore, we lookup the current jsg::Lock instance from the
// isolate pointer and use that to get the external memory adjustment.
class Allocator final {
public:
static void* AllocForZlib(void* data, uInt items, uInt size);
static void* AllocForBrotli(void* data, size_t size);
static void FreeForZlib(void* data, void* pointer);

struct Allocation {
kj::Array<kj::byte> data;
kj::Maybe<jsg::ExternalMemoryAdjustment> memory;
};

private:
kj::HashMap<void*, kj::Array<kj::byte>> allocations;
kj::HashMap<void*, Allocation> allocations;
};

template <class CompressionContext>
Expand Down

0 comments on commit d181608

Please sign in to comment.