Skip to content

Commit

Permalink
refactor(container): prefer Vector over std::vector
Browse files Browse the repository at this point in the history
Reduce our use of std::vector to reduce binary bloat:
#689
  • Loading branch information
strager committed Nov 5, 2023
1 parent 127c598 commit c61fae6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/quick-lint-js/container/byte-buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ Byte_Buffer_Chunk make_chunk(const void* data, Byte_Buffer::Size_Type size) {

}

Byte_Buffer::Byte_Buffer() { this->add_new_chunk(this->default_chunk_size); }
Byte_Buffer::Byte_Buffer()
: chunks_("Byte_Buffer::chunks_", new_delete_resource()) {
this->add_new_chunk(this->default_chunk_size);
}

Byte_Buffer::Byte_Buffer(Byte_Buffer&&) = default;

Expand Down Expand Up @@ -107,7 +110,7 @@ void Byte_Buffer::prepend_copy(String8_View data) {
std::copy_n(data_bytes, data.size(), chunk_begin(prefix_chunk));
QLJS_ASSERT(end == chunk_end(prefix_chunk));

this->chunks_.insert(this->chunks_.begin(), std::move(prefix_chunk));
this->chunks_.push_front(std::move(prefix_chunk));
}

void Byte_Buffer::clear() {
Expand All @@ -123,7 +126,7 @@ void Byte_Buffer::clear() {

Byte_Buffer::Size_Type Byte_Buffer::size() const {
Size_Type total_size = 0;
for (std::size_t chunk_index = 0; chunk_index < this->chunks_.size() - 1;
for (Vector_Size chunk_index = 0; chunk_index < this->chunks_.size() - 1;
++chunk_index) {
const Byte_Buffer_Chunk& c = this->chunks_[chunk_index];
total_size += chunk_size(c);
Expand All @@ -136,7 +139,7 @@ bool Byte_Buffer::empty() const {
if (this->bytes_used_in_current_chunk() > 0) {
return false;
}
for (std::size_t chunk_index = 0; chunk_index < this->chunks_.size() - 1;
for (Vector_Size chunk_index = 0; chunk_index < this->chunks_.size() - 1;
++chunk_index) {
const Byte_Buffer_Chunk& c = this->chunks_[chunk_index];
if (chunk_size(c) > 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/quick-lint-js/container/byte-buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
#include <cstddef>
#include <memory>
#include <quick-lint-js/assert.h>
#include <quick-lint-js/container/vector.h>
#include <quick-lint-js/port/char8.h>
#include <quick-lint-js/port/have.h>
#include <quick-lint-js/util/cast.h>
#include <quick-lint-js/util/integer.h>
#include <utility>
#include <vector>

#if QLJS_HAVE_WRITEV
#include <sys/uio.h>
Expand Down Expand Up @@ -89,7 +89,7 @@ class Byte_Buffer {

template <class Func>
void enumerate_chunks(Func&& on_chunk) const {
for (std::size_t chunk_index = 0; chunk_index < this->chunks_.size();
for (Vector_Size chunk_index = 0; chunk_index < this->chunks_.size();
++chunk_index) {
const Byte_Buffer_Chunk* c = &this->chunks_[chunk_index];
const std::byte* c_begin =
Expand Down Expand Up @@ -128,7 +128,7 @@ class Byte_Buffer {
static Byte_Buffer_Chunk allocate_chunk(Size_Type size);
static void delete_chunk(Byte_Buffer_Chunk&&);

std::vector<Byte_Buffer_Chunk> chunks_;
Vector<Byte_Buffer_Chunk> chunks_;
std::byte* cursor_;
std::byte* current_chunk_end_;

Expand Down
4 changes: 4 additions & 0 deletions src/quick-lint-js/container/vector-profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ class Instrumented_Vector {
return this->data_[index];
}

QLJS_FORCE_INLINE const value_type &operator[](size_type index) const {
return this->data_[index];
}

QLJS_FORCE_INLINE value_type *begin() { return this->data(); }
QLJS_FORCE_INLINE value_type *end() { return this->begin() + this->size(); }

Expand Down
4 changes: 4 additions & 0 deletions src/quick-lint-js/container/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ class Raw_Vector {
QLJS_ASSERT(index < this->size());
return this->data_[index];
}
const T &operator[](size_type index) const {
QLJS_ASSERT(index < this->size());
return this->data_[index];
}

// Precondition: (none)
void reserve(size_type new_capacity) {
Expand Down

0 comments on commit c61fae6

Please sign in to comment.