Skip to content

Commit

Permalink
remove path copies from SnapshotBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr committed Apr 19, 2024
1 parent c129c26 commit 3378cc6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
6 changes: 2 additions & 4 deletions silkworm/capi/silkworm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,10 @@ SILKWORM_EXPORT int silkworm_add_snapshot(SilkwormHandle handle, SilkwormChainSn
transactions_snapshot->reopen_index();

snapshots::SnapshotBundle bundle{
.headers_snapshot_path = *headers_segment_path,
.headers_snapshot = std::move(headers_snapshot),
.bodies_snapshot_path = *bodies_segment_path,
.bodies_snapshot = std::move(bodies_snapshot),
.tx_snapshot_path = *transactions_segment_path,
.tx_snapshot = std::move(transactions_snapshot)};
.tx_snapshot = std::move(transactions_snapshot),
};
handle->snapshot_repository->add_snapshot_bundle(std::move(bundle));
return SILKWORM_OK;
}
Expand Down
27 changes: 18 additions & 9 deletions silkworm/db/snapshots/repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,23 @@ SnapshotRepository::~SnapshotRepository() {
close();
}

void SnapshotRepository::add_snapshot_bundle(SnapshotBundle&& bundle) {
header_segments_[bundle.headers_snapshot_path.path()] = std::move(bundle.headers_snapshot);
body_segments_[bundle.bodies_snapshot_path.path()] = std::move(bundle.bodies_snapshot);
tx_segments_[bundle.tx_snapshot_path.path()] = std::move(bundle.tx_snapshot);
if (bundle.tx_snapshot_path.block_to() > segment_max_block_) {
segment_max_block_ = bundle.tx_snapshot_path.block_to() - 1;
void SnapshotRepository::add_snapshot_bundle(SnapshotBundle bundle) {
if (bundle.headers_snapshot && bundle.bodies_snapshot && bundle.tx_snapshot) {
// assume that all snapshot types end with the same block, and use one of them
BlockNum last_block = bundle.tx_snapshot->block_to() - 1;
segment_max_block_ = std::max(segment_max_block_, last_block);
}

if (bundle.headers_snapshot) {
header_segments_[bundle.headers_snapshot->fs_path()] = std::move(bundle.headers_snapshot);
}
if (bundle.bodies_snapshot) {
body_segments_[bundle.bodies_snapshot->fs_path()] = std::move(bundle.bodies_snapshot);
}
if (bundle.tx_snapshot) {
tx_segments_[bundle.tx_snapshot->fs_path()] = std::move(bundle.tx_snapshot);
}

idx_max_block_ = max_idx_available();
}

Expand Down Expand Up @@ -297,9 +307,8 @@ void SnapshotRepository::reopen_list(const SnapshotPathList& segment_files, bool
}
ensure(snapshot_valid, [&]() { return "invalid empty snapshot " + seg_file.filename(); });

if (seg_file.block_to() > segment_max_block) {
segment_max_block = seg_file.block_to() - 1;
}
BlockNum last_block = seg_file.block_to() - 1;
segment_max_block = std::max(segment_max_block, last_block);
} catch (const std::exception& exc) {
SILK_WARN << "Reopen failed for: " << seg_file.path() << " [" << exc.what() << "]";
if (!optimistic) throw;
Expand Down
5 changes: 1 addition & 4 deletions silkworm/db/snapshots/repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@ using BodySnapshotWalker = SnapshotWalker<BodySnapshot>;
using TransactionSnapshotWalker = SnapshotWalker<TransactionSnapshot>;

struct SnapshotBundle {
SnapshotPath headers_snapshot_path;
std::unique_ptr<HeaderSnapshot> headers_snapshot;
SnapshotPath bodies_snapshot_path;
std::unique_ptr<BodySnapshot> bodies_snapshot;
SnapshotPath tx_snapshot_path;
std::unique_ptr<TransactionSnapshot> tx_snapshot;
};

Expand All @@ -74,7 +71,7 @@ class SnapshotRepository {
return get_files(kSegmentExtension);
}

void add_snapshot_bundle(SnapshotBundle&& bundle);
void add_snapshot_bundle(SnapshotBundle bundle);

void reopen_list(const SnapshotPathList& segment_files, bool optimistic = false);
void reopen_file(const SnapshotPath& segment_path, bool optimistic = false);
Expand Down

0 comments on commit 3378cc6

Please sign in to comment.