Skip to content

Commit 9996748

Browse files
committed
feat: replace vector_byte_buffer with malloc_byte_buffer
The latter doesn't always explicitly initialize the allocated memory, which can help improve performance.
1 parent f6cf57b commit 9996748

23 files changed

+274
-82
lines changed

cmake/libdwarfs.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ add_library(
3434
src/history.cpp
3535
src/library_dependencies.cpp
3636
src/logger.cpp
37+
src/malloc_byte_buffer.cpp
3738
src/mapped_byte_buffer.cpp
3839
src/mmap.cpp
3940
src/option_map.cpp
@@ -44,15 +45,14 @@ add_library(
4445
src/thread_pool.cpp
4546
src/util.cpp
4647
src/varint.cpp
47-
src/vector_byte_buffer.cpp
48-
src/vector_byte_buffer_factory.cpp
4948
src/xattr.cpp
5049

5150
src/internal/features.cpp
5251
src/internal/file_status_conv.cpp
5352
src/internal/fs_section.cpp
5453
src/internal/fs_section_checker.cpp
5554
src/internal/glob_to_regex.cpp
55+
src/internal/malloc_buffer.cpp
5656
src/internal/metadata_utils.cpp
5757
src/internal/string_table.cpp
5858
src/internal/unicode_case_folding.cpp

include/dwarfs/byte_buffer.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ compare_spans(std::span<uint8_t const> lhs, std::span<uint8_t const> rhs);
4949

5050
} // namespace detail
5151

52+
namespace internal {
53+
54+
class malloc_buffer;
55+
56+
}
57+
5258
class byte_buffer_interface {
5359
public:
5460
virtual ~byte_buffer_interface() = default;
@@ -72,9 +78,7 @@ class mutable_byte_buffer_interface : public byte_buffer_interface {
7278
// that would reallocate the buffer will throw.
7379
virtual void freeze_location() = 0;
7480

75-
// TODO: See if we can do without this. This will *only* be implemented
76-
// in the vector_byte_buffer, other implementations will throw.
77-
virtual std::vector<uint8_t>& raw_vector() = 0;
81+
virtual internal::malloc_buffer& raw_buffer() = 0;
7882
};
7983

8084
class shared_byte_buffer {
@@ -164,7 +168,7 @@ class mutable_byte_buffer {
164168

165169
void freeze_location() { bb_->freeze_location(); }
166170

167-
std::vector<uint8_t>& raw_vector() { return bb_->raw_vector(); }
171+
internal::malloc_buffer& raw_buffer() { return bb_->raw_buffer(); }
168172

169173
void swap(mutable_byte_buffer& other) noexcept { std::swap(bb_, other.bb_); }
170174

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* vim:set ts=2 sw=2 sts=2 et: */
2+
/**
3+
* \author Marcus Holland-Moritz ([email protected])
4+
* \copyright Copyright (c) Marcus Holland-Moritz
5+
*
6+
* This file is part of dwarfs.
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the “Software”), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*
26+
* SPDX-License-Identifier: MIT
27+
*/
28+
29+
#pragma once
30+
31+
#include <cstdint>
32+
#include <cstring>
33+
#include <span>
34+
35+
namespace dwarfs::internal {
36+
37+
class malloc_buffer {
38+
public:
39+
using value_type = uint8_t;
40+
41+
malloc_buffer() = default;
42+
malloc_buffer(size_t size);
43+
malloc_buffer(void const* data, size_t size);
44+
malloc_buffer(std::span<value_type const> data);
45+
46+
~malloc_buffer();
47+
48+
malloc_buffer(malloc_buffer&&) = default;
49+
malloc_buffer& operator=(malloc_buffer&&) = default;
50+
51+
bool empty() const { return size_ == 0; }
52+
53+
size_t size() const { return size_; }
54+
size_t capacity() const { return capacity_; }
55+
56+
value_type const* data() const { return data_; }
57+
value_type* data() { return data_; }
58+
59+
void append(void const* data, size_t size) {
60+
reserve(size_ + size);
61+
copy(data_ + size_, data, size);
62+
size_ += size;
63+
}
64+
65+
void clear() { size_ = 0; }
66+
67+
void resize(size_t new_size) {
68+
reserve(new_size);
69+
size_ = new_size;
70+
}
71+
72+
void reserve(size_t new_capacity) {
73+
if (new_capacity > capacity_) {
74+
grow(new_capacity);
75+
}
76+
}
77+
78+
void shrink_to_fit();
79+
80+
private:
81+
void grow(size_t new_size);
82+
void resize_buffer(size_t new_size);
83+
static void copy(void* dest, void const* src, size_t size) {
84+
// TODO: try std::copy or even something custom
85+
std::memcpy(dest, src, size);
86+
}
87+
88+
value_type* data_{nullptr};
89+
size_t size_{0};
90+
size_t capacity_{0};
91+
};
92+
93+
} // namespace dwarfs::internal
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@
3434

3535
namespace dwarfs {
3636

37-
class vector_byte_buffer {
37+
namespace internal {
38+
class malloc_buffer;
39+
}
40+
41+
class malloc_byte_buffer {
3842
public:
3943
static mutable_byte_buffer create();
4044
static mutable_byte_buffer create(size_t size);
45+
static mutable_byte_buffer create_zeroed(size_t size);
4146
static mutable_byte_buffer create_reserve(size_t size);
4247
static mutable_byte_buffer create(std::string_view data);
4348
static mutable_byte_buffer create(std::span<uint8_t const> data);
44-
static mutable_byte_buffer create(std::vector<uint8_t>&& data);
49+
static mutable_byte_buffer create(internal::malloc_buffer&& data);
4550
};
4651

4752
} // namespace dwarfs

include/dwarfs/vector_byte_buffer_factory.h renamed to include/dwarfs/malloc_byte_buffer_factory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
namespace dwarfs {
3434

35-
class vector_byte_buffer_factory {
35+
class malloc_byte_buffer_factory {
3636
public:
3737
static byte_buffer_factory create();
3838
};

src/block_decompressor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <dwarfs/block_decompressor.h>
3030
#include <dwarfs/decompressor_registry.h>
3131
#include <dwarfs/fstypes.h>
32-
#include <dwarfs/vector_byte_buffer.h>
32+
#include <dwarfs/malloc_byte_buffer.h>
3333

3434
namespace dwarfs {
3535

@@ -42,7 +42,7 @@ shared_byte_buffer
4242
block_decompressor::decompress(compression_type type,
4343
std::span<uint8_t const> data) {
4444
block_decompressor bd(type, data);
45-
auto target = vector_byte_buffer::create_reserve(bd.uncompressed_size());
45+
auto target = malloc_byte_buffer::create_reserve(bd.uncompressed_size());
4646
bd.start_decompression(target);
4747
bd.decompress_frame(bd.uncompressed_size());
4848
return target.share();

src/compression/brotli.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
#include <dwarfs/decompressor_registry.h>
3838
#include <dwarfs/error.h>
3939
#include <dwarfs/fstypes.h>
40+
#include <dwarfs/malloc_byte_buffer.h>
4041
#include <dwarfs/option_map.h>
4142
#include <dwarfs/varint.h>
42-
#include <dwarfs/vector_byte_buffer.h>
4343

4444
#include "base.h"
4545

@@ -61,7 +61,7 @@ class brotli_block_compressor final : public block_compressor::impl {
6161

6262
shared_byte_buffer compress(shared_byte_buffer const& data,
6363
std::string const* /*metadata*/) const override {
64-
auto compressed = vector_byte_buffer::create(); // TODO: make configurable
64+
auto compressed = malloc_byte_buffer::create(); // TODO: make configurable
6565
compressed.resize(varint::max_size +
6666
::BrotliEncoderMaxCompressedSize(data.size()));
6767
size_t size_size = varint::encode(data.size(), compressed.data());

src/compression/flac.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@
4242
#include <dwarfs/compressor_registry.h>
4343
#include <dwarfs/decompressor_registry.h>
4444
#include <dwarfs/error.h>
45+
#include <dwarfs/malloc_byte_buffer.h>
4546
#include <dwarfs/option_map.h>
4647
#include <dwarfs/pcm_sample_transformer.h>
4748
#include <dwarfs/varint.h>
48-
#include <dwarfs/vector_byte_buffer.h>
4949

5050
#include <dwarfs/gen-cpp2/compression_types.h>
5151

@@ -281,7 +281,7 @@ class flac_block_compressor final : public block_compressor::impl {
281281
pcm_pad = pcm_sample_padding::Msb;
282282
}
283283

284-
auto compressed = vector_byte_buffer::create(); // TODO: make configurable
284+
auto compressed = malloc_byte_buffer::create(); // TODO: make configurable
285285

286286
{
287287
using namespace ::apache::thrift;

src/compression/lz4.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
#include <dwarfs/decompressor_registry.h>
3737
#include <dwarfs/error.h>
3838
#include <dwarfs/fstypes.h>
39+
#include <dwarfs/malloc_byte_buffer.h>
3940
#include <dwarfs/option_map.h>
40-
#include <dwarfs/vector_byte_buffer.h>
4141

4242
#include "base.h"
4343

@@ -82,7 +82,7 @@ class lz4_block_compressor final : public block_compressor::impl {
8282

8383
shared_byte_buffer compress(shared_byte_buffer const& data,
8484
std::string const* /*metadata*/) const override {
85-
auto compressed = vector_byte_buffer::create(); // TODO: make configurable
85+
auto compressed = malloc_byte_buffer::create(); // TODO: make configurable
8686
compressed.resize(sizeof(uint32_t) +
8787
LZ4_compressBound(to<int>(data.size())));
8888
// TODO: this should have been a varint; also, if we ever support

src/compression/lzma.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@
4242
#include <dwarfs/decompressor_registry.h>
4343
#include <dwarfs/error.h>
4444
#include <dwarfs/fstypes.h>
45+
#include <dwarfs/malloc_byte_buffer.h>
4546
#include <dwarfs/option_map.h>
4647
#include <dwarfs/sorted_array_map.h>
4748
#include <dwarfs/types.h>
48-
#include <dwarfs/vector_byte_buffer.h>
4949

5050
#include "base.h"
5151

@@ -221,7 +221,7 @@ lzma_block_compressor::compress(shared_byte_buffer const& data,
221221

222222
lzma_action action = LZMA_FINISH;
223223

224-
auto compressed = vector_byte_buffer::create(); // TODO: make configurable
224+
auto compressed = malloc_byte_buffer::create(); // TODO: make configurable
225225
compressed.resize(data.size() - 1);
226226

227227
s.next_in = data.data();

0 commit comments

Comments
 (0)