Skip to content

Commit

Permalink
Let digesters support Reset() when this is more efficient than assi…
Browse files Browse the repository at this point in the history
…gning a

newly constructed instance.

PiperOrigin-RevId: 647650005
  • Loading branch information
QrczakMK committed Jun 28, 2024
1 parent 2d720ea commit 06ba234
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions riegeli/digests/highwayhash/highwayhash_digester.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ class HighwayHashDigester {
public:
explicit HighwayHashDigester(const HighwayHashKey& key) : cat_(key) {}

HighwayHashDigester(const HighwayHashDigester& that) = default;
HighwayHashDigester& operator=(const HighwayHashDigester& that) = default;

void Reset(const HighwayHashKey& key) {
cat_.Reset(key);
is_open_ = true;
}

void Write(absl::string_view chunk) {
cat_.Append(chunk.data(), chunk.size());
}
Expand Down
5 changes: 5 additions & 0 deletions riegeli/digests/openssl_digester.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class OpenSslDigester {
OpenSslDigester(const OpenSslDigester& that) = default;
OpenSslDigester& operator=(const OpenSslDigester& that) = default;

void Reset() {
init(&ctx_);
is_open_ = true;
}

void Write(absl::string_view src) { update(&ctx_, src.data(), src.size()); }

void Close() {
Expand Down
9 changes: 8 additions & 1 deletion riegeli/digests/wrapping_digester.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class WrappingDigester {
absl::negation<std::is_same<std::tuple<std::decay_t<Args>...>,
std::tuple<WrappingDigester>>>,
std::is_constructible<BaseDigester, Args&&...>>::value,
int> = true>
int> = 0>
explicit WrappingDigester(Args&&... args)
: base_(riegeli::Maker(std::forward<Args>(args)...)) {}

Expand All @@ -93,6 +93,13 @@ class WrappingDigester {
WrappingDigester(WrappingDigester&& that) = default;
WrappingDigester& operator=(WrappingDigester&& that) = default;

template <typename... Args,
std::enable_if_t<
std::is_constructible<BaseDigester, Args&&...>::value, int> = 0>
void Reset(Args&&... args) {
base_.Reset(riegeli::Maker(std::forward<Args>(args)...));
}

bool Write(absl::string_view src) { return base_.get().Write(src); }
bool Write(const Chain& src) { return base_.get().Write(src); }
bool Write(const absl::Cord& src) { return base_.get().Write(src); }
Expand Down

0 comments on commit 06ba234

Please sign in to comment.