Skip to content

Commit

Permalink
refactor(container): reduce template code bloat
Browse files Browse the repository at this point in the history
Only a tiny piece of try_grow_array_in_place depends on the template
parameter. Refactor this function template such that most of it is in
a non-template function. This should reduce binary size slightly.
  • Loading branch information
strager committed Nov 5, 2023
1 parent a420c88 commit 9bf7e0a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
20 changes: 20 additions & 0 deletions src/quick-lint-js/container/linked-bump-allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ void Linked_Bump_Allocator::do_deallocate(void* p, std::size_t bytes,
this->deallocate_bytes(p, bytes);
}

bool Linked_Bump_Allocator::try_grow_array_in_place_impl(
char* array, std::size_t old_byte_size, std::size_t new_byte_size) {
this->assert_not_disabled();
QLJS_ASSERT(new_byte_size > old_byte_size);
bool array_is_last_allocation =
array + old_byte_size == this->next_allocation_;
if (!array_is_last_allocation) {
// We can't grow because something else was already allocated.
return false;
}

std::size_t extra_bytes = new_byte_size - old_byte_size;
if (extra_bytes > this->remaining_bytes_in_current_chunk()) {
return false;
}
this->did_allocate_bytes(this->next_allocation_, extra_bytes);
this->next_allocation_ += extra_bytes;
return true;
}

[[nodiscard]] void* Linked_Bump_Allocator::allocate_bytes(std::size_t size,
std::size_t align) {
this->assert_not_disabled();
Expand Down
25 changes: 6 additions & 19 deletions src/quick-lint-js/container/linked-bump-allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <cstddef>
#include <memory>
#include <new>
#include <quick-lint-js/assert.h>
#include <quick-lint-js/container/flexible-array.h>
#include <quick-lint-js/port/memory-resource.h>
#include <quick-lint-js/port/span.h>
Expand Down Expand Up @@ -127,24 +126,9 @@ class Linked_Bump_Allocator final : public Memory_Resource {
template <class T>
bool try_grow_array_in_place(T* array, std::size_t old_size,
std::size_t new_size) {
this->assert_not_disabled();
QLJS_ASSERT(new_size > old_size);
std::size_t old_byte_size = old_size * sizeof(T);
bool array_is_last_allocation =
reinterpret_cast<char*>(array) + old_byte_size ==
this->next_allocation_;
if (!array_is_last_allocation) {
// We can't grow because something else was already allocated.
return false;
}

std::size_t extra_bytes = (new_size - old_size) * sizeof(T);
if (extra_bytes > this->remaining_bytes_in_current_chunk()) {
return false;
}
this->did_allocate_bytes(this->next_allocation_, extra_bytes);
this->next_allocation_ += extra_bytes;
return true;
return this->try_grow_array_in_place_impl(reinterpret_cast<char*>(array),
old_size * sizeof(T),
new_size * sizeof(T));
}

std::size_t remaining_bytes_in_current_chunk() const {
Expand Down Expand Up @@ -181,6 +165,9 @@ class Linked_Bump_Allocator final : public Memory_Resource {

static inline constexpr std::size_t default_chunk_size = 4096 - sizeof(Chunk);

bool try_grow_array_in_place_impl(char* array, std::size_t old_byte_size,
std::size_t new_byte_size);

[[nodiscard]] void* allocate_bytes(std::size_t size, std::size_t align);

void deallocate_bytes([[maybe_unused]] void* p,
Expand Down

0 comments on commit 9bf7e0a

Please sign in to comment.