Skip to content

Commit

Permalink
Improve protobuf domain mutation performance by caching recursion ana…
Browse files Browse the repository at this point in the history
…lysis results.

PiperOrigin-RevId: 714966960
  • Loading branch information
hadi88 authored and copybara-github committed Jan 13, 2025
1 parent 2dbe9e5 commit 87fffb7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions fuzztest/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ cc_library(
":status",
":type_support",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:no_destructor",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/random",
Expand Down
1 change: 1 addition & 0 deletions fuzztest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ fuzztest_cc_library(
fuzztest::status
fuzztest::type_support
absl::core_headers
absl::no_destructor
absl::flat_hash_map
absl::flat_hash_set
absl::random_random
Expand Down
15 changes: 13 additions & 2 deletions fuzztest/internal/domains/protobuf_domain_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <utility>
#include <vector>

#include "absl/base/no_destructor.h"
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
Expand Down Expand Up @@ -1668,9 +1669,19 @@ class ProtobufDomainUntypedImpl

bool IsFieldRecursive(const FieldDescriptor* field) {
if (!field->message_type()) return false;
static absl::NoDestructor<absl::flat_hash_map<const FieldDescriptor*, bool>>
cache;
auto it = cache->end();
if (IsCustomizedRecursivelyOnly()) {
it = cache->find(field);
if (it != cache->end()) return it->second;
}
absl::flat_hash_set<decltype(field->message_type())> parents;
return IsProtoRecursive(field->message_type(), parents,
/*consider_non_terminating_recursions=*/false);
bool result =
IsProtoRecursive(field->message_type(), parents,
/*consider_non_terminating_recursions=*/false);
if (IsCustomizedRecursivelyOnly()) cache->insert(it, {field, result});
return result;
}

bool IsOneofRecursive(const OneofDescriptor* oneof,
Expand Down

0 comments on commit 87fffb7

Please sign in to comment.