feat(block_cache): use byte_buffer_factory for block allocation

This commit is contained in:
Marcus Holland-Moritz 2025-03-31 14:08:21 +02:00
parent 39950e085e
commit a8b5573840
9 changed files with 153 additions and 7 deletions

View File

@ -46,6 +46,7 @@ add_library(
src/util.cpp src/util.cpp
src/varint.cpp src/varint.cpp
src/vector_byte_buffer.cpp src/vector_byte_buffer.cpp
src/vector_byte_buffer_factory.cpp
src/xattr.cpp src/xattr.cpp
src/internal/features.cpp src/internal/features.cpp

View File

@ -36,6 +36,7 @@
#include <vector> #include <vector>
#include <dwarfs/byte_buffer.h> #include <dwarfs/byte_buffer.h>
#include <dwarfs/byte_buffer_factory.h>
#include <dwarfs/compression.h> #include <dwarfs/compression.h>
#include <dwarfs/compression_constraints.h> #include <dwarfs/compression_constraints.h>
@ -112,6 +113,7 @@ class block_decompressor {
block_decompressor(compression_type type, std::span<uint8_t const> data); block_decompressor(compression_type type, std::span<uint8_t const> data);
shared_byte_buffer start_decompression(mutable_byte_buffer target); shared_byte_buffer start_decompression(mutable_byte_buffer target);
shared_byte_buffer start_decompression(byte_buffer_factory const& bbf);
bool decompress_frame(size_t frame_size = BUFSIZ) { bool decompress_frame(size_t frame_size = BUFSIZ) {
return impl_->decompress_frame(frame_size); return impl_->decompress_frame(frame_size);

View File

@ -0,0 +1,54 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of dwarfs.
*
* dwarfs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dwarfs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <memory>
#include <dwarfs/byte_buffer.h>
namespace dwarfs {
class byte_buffer_factory_interface {
public:
virtual ~byte_buffer_factory_interface() = default;
virtual mutable_byte_buffer
create_mutable_fixed_reserve(size_t size) const = 0;
};
class byte_buffer_factory {
public:
byte_buffer_factory() = default;
explicit byte_buffer_factory(
std::shared_ptr<byte_buffer_factory_interface> impl)
: impl_{std::move(impl)} {}
mutable_byte_buffer create_mutable_fixed_reserve(size_t size) const {
return impl_->create_mutable_fixed_reserve(size);
}
private:
std::shared_ptr<byte_buffer_factory_interface> impl_;
};
} // namespace dwarfs

View File

@ -28,6 +28,7 @@
namespace dwarfs { namespace dwarfs {
class byte_buffer_factory;
class logger; class logger;
class mmif; class mmif;
@ -43,7 +44,8 @@ class cached_block {
public: public:
static std::unique_ptr<cached_block> static std::unique_ptr<cached_block>
create(logger& lgr, dwarfs::internal::fs_section const& b, create(logger& lgr, dwarfs::internal::fs_section const& b,
std::shared_ptr<mmif> mm, bool release, bool disable_integrity_check); std::shared_ptr<mmif> mm, byte_buffer_factory const& bbf, bool release,
bool disable_integrity_check);
virtual ~cached_block() = default; virtual ~cached_block() = default;

View File

@ -0,0 +1,34 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of dwarfs.
*
* dwarfs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dwarfs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <dwarfs/byte_buffer_factory.h>
#include <dwarfs/vector_byte_buffer.h>
namespace dwarfs {
class vector_byte_buffer_factory {
public:
static byte_buffer_factory create();
};
} // namespace dwarfs

View File

@ -63,6 +63,12 @@ block_decompressor::start_decompression(mutable_byte_buffer target) {
return target.share(); return target.share();
} }
shared_byte_buffer
block_decompressor::start_decompression(byte_buffer_factory const& bbf) {
auto target = bbf.create_mutable_fixed_reserve(impl_->uncompressed_size());
return start_decompression(target);
}
compression_registry& compression_registry::instance() { compression_registry& compression_registry::instance() {
static compression_registry the_instance; static compression_registry the_instance;
return the_instance; return the_instance;

View File

@ -48,6 +48,7 @@
#include <dwarfs/reader/cache_tidy_config.h> #include <dwarfs/reader/cache_tidy_config.h>
#include <dwarfs/scope_exit.h> #include <dwarfs/scope_exit.h>
#include <dwarfs/util.h> #include <dwarfs/util.h>
#include <dwarfs/vector_byte_buffer_factory.h>
#include <dwarfs/internal/fs_section.h> #include <dwarfs/internal/fs_section.h>
#include <dwarfs/internal/worker_group.h> #include <dwarfs/internal/worker_group.h>
@ -223,6 +224,7 @@ class block_cache_ final : public block_cache::impl {
[[maybe_unused]]) [[maybe_unused]])
: cache_(0) : cache_(0)
, mm_(std::move(mm)) , mm_(std::move(mm))
, buffer_factory_{vector_byte_buffer_factory::create()}
, LOG_PROXY_INIT(lgr) , LOG_PROXY_INIT(lgr)
// clang-format off // clang-format off
PERFMON_CLS_PROXY_INIT(perfmon, "block_cache") PERFMON_CLS_PROXY_INIT(perfmon, "block_cache")
@ -569,7 +571,8 @@ class block_cache_ final : public block_cache::impl {
try { try {
std::shared_ptr<cached_block> block = cached_block::create( std::shared_ptr<cached_block> block = cached_block::create(
LOG_GET_LOGGER, DWARFS_NOTHROW(block_.at(block_no)), mm_, LOG_GET_LOGGER, DWARFS_NOTHROW(block_.at(block_no)), mm_,
options_.mm_release, options_.disable_block_integrity_check); buffer_factory_, options_.mm_release,
options_.disable_block_integrity_check);
blocks_created_.fetch_add(1, std::memory_order_relaxed); blocks_created_.fetch_add(1, std::memory_order_relaxed);
// Make a new set for the block // Make a new set for the block
@ -804,6 +807,7 @@ class block_cache_ final : public block_cache::impl {
mutable worker_group wg_; mutable worker_group wg_;
std::vector<fs_section> block_; std::vector<fs_section> block_;
std::shared_ptr<mmif> mm_; std::shared_ptr<mmif> mm_;
byte_buffer_factory buffer_factory_;
LOG_PROXY_DECL(LoggerPolicy); LOG_PROXY_DECL(LoggerPolicy);
PERFMON_CLS_PROXY_DECL PERFMON_CLS_PROXY_DECL
PERFMON_CLS_TIMER_DECL(get) PERFMON_CLS_TIMER_DECL(get)

View File

@ -29,7 +29,6 @@
#include <dwarfs/error.h> #include <dwarfs/error.h>
#include <dwarfs/logger.h> #include <dwarfs/logger.h>
#include <dwarfs/mmif.h> #include <dwarfs/mmif.h>
#include <dwarfs/vector_byte_buffer.h>
#include <dwarfs/internal/fs_section.h> #include <dwarfs/internal/fs_section.h>
#include <dwarfs/reader/internal/cached_block.h> #include <dwarfs/reader/internal/cached_block.h>
@ -46,10 +45,11 @@ class cached_block_ final : public cached_block {
static inline std::atomic<size_t> instance_count_{0}; static inline std::atomic<size_t> instance_count_{0};
cached_block_(logger& lgr, fs_section const& b, std::shared_ptr<mmif> mm, cached_block_(logger& lgr, fs_section const& b, std::shared_ptr<mmif> mm,
bool release, bool disable_integrity_check) byte_buffer_factory const& buffer_factory, bool release,
bool disable_integrity_check)
: decompressor_{std::make_unique<block_decompressor>( : decompressor_{std::make_unique<block_decompressor>(
b.compression(), mm->span<uint8_t>(b.start(), b.length()))} b.compression(), mm->span<uint8_t>(b.start(), b.length()))}
, data_{decompressor_->start_decompression(vector_byte_buffer::create())} , data_{decompressor_->start_decompression(buffer_factory)}
, mm_(std::move(mm)) , mm_(std::move(mm))
, section_(b) , section_(b)
, LOG_PROXY_INIT(lgr) , LOG_PROXY_INIT(lgr)
@ -158,10 +158,11 @@ class cached_block_ final : public cached_block {
std::unique_ptr<cached_block> std::unique_ptr<cached_block>
cached_block::create(logger& lgr, fs_section const& b, std::shared_ptr<mmif> mm, cached_block::create(logger& lgr, fs_section const& b, std::shared_ptr<mmif> mm,
bool release, bool disable_integrity_check) { byte_buffer_factory const& bbf, bool release,
bool disable_integrity_check) {
return make_unique_logging_object<cached_block, cached_block_, return make_unique_logging_object<cached_block, cached_block_,
logger_policies>( logger_policies>(
lgr, b, std::move(mm), release, disable_integrity_check); lgr, b, std::move(mm), bbf, release, disable_integrity_check);
} }
} // namespace dwarfs::reader::internal } // namespace dwarfs::reader::internal

View File

@ -0,0 +1,42 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of dwarfs.
*
* dwarfs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dwarfs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/
#include <dwarfs/vector_byte_buffer_factory.h>
namespace dwarfs {
namespace {
class vector_byte_buffer_factory_impl : public byte_buffer_factory_interface {
public:
mutable_byte_buffer create_mutable_fixed_reserve(size_t size) const override {
return vector_byte_buffer::create_reserve(size);
}
};
} // namespace
byte_buffer_factory vector_byte_buffer_factory::create() {
return byte_buffer_factory{
std::make_shared<vector_byte_buffer_factory_impl>()};
}
} // namespace dwarfs