Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Experimental] Abseil Containers #187

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ install(FILES ${libddwaf_SOURCE_DIR}/include/ddwaf.h DESTINATION ${CMAKE_INSTALL

# System dependencies
set(LIBDDWAF_INTERFACE_LIBRARIES "")
set(LIBDDWAF_PRIVATE_LIBRARIES lib_re2 lib_ac lib_injection lib_radix lib_utf8proc)
set(LIBDDWAF_PRIVATE_LIBRARIES lib_re2 lib_ac lib_injection lib_radix lib_utf8proc absl::flat_hash_map)

if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
# needed for glibc <2.17. We compile with --as-needed so the library won't
Expand Down
12 changes: 6 additions & 6 deletions perf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ file(GLOB_RECURSE LIBDDWAF_BENCHMARK_SOURCE *.cpp)
list(REMOVE_ITEM LIBDDWAF_BENCHMARK_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/benchcmp.cpp)
list(REMOVE_ITEM LIBDDWAF_BENCHMARK_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/benchmerge.cpp)

add_executable(benchmark ${LIBDDWAF_BENCHMARK_SOURCE})
target_link_libraries(benchmark PRIVATE libddwaf_objects lib_yamlcpp lib_rapidjson m)
target_include_directories(benchmark PRIVATE ${libddwaf_SOURCE_DIR}/src)
add_executable(waf_benchmark ${LIBDDWAF_BENCHMARK_SOURCE})
target_link_libraries(waf_benchmark PRIVATE libddwaf_objects lib_yamlcpp lib_rapidjson m)
target_include_directories(waf_benchmark PRIVATE ${libddwaf_SOURCE_DIR}/src)

add_executable(benchcmp benchcmp.cpp)
target_link_libraries(benchcmp PRIVATE lib_yamlcpp m)

add_executable(benchmerge benchmerge.cpp)
target_link_libraries(benchmerge PRIVATE lib_yamlcpp m)

set_target_properties(benchmark PROPERTIES
set_target_properties(waf_benchmark PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO)
Expand All @@ -33,7 +33,7 @@ ExternalProject_Get_property(proj_event_rules SOURCE_DIR)
set(EVENT_RULES_SOURCE_DIR ${SOURCE_DIR})

add_custom_target(run_benchmark
COMMAND ${CMAKE_BINARY_DIR}/perf/benchmark
COMMAND ${CMAKE_BINARY_DIR}/perf/waf_benchmark
--rule-repo=${EVENT_RULES_SOURCE_DIR}
--iterations=1000
--format=json
Expand All @@ -43,4 +43,4 @@ add_custom_target(run_benchmark
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)

add_dependencies(run_benchmark benchmark proj_event_rules)
add_dependencies(run_benchmark waf_benchmark proj_event_rules)
4 changes: 2 additions & 2 deletions src/collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ std::optional<event> match_rule(rule *rule, const object_store &store,
memory::unordered_map<ddwaf::rule *, rule::cache_type> &cache,
const memory::unordered_map<ddwaf::rule *, exclusion::filter_mode> &rules_to_exclude,
const memory::unordered_map<ddwaf::rule *, collection::object_set> &objects_to_exclude,
const std::unordered_map<std::string, rule_processor::base::ptr> &dynamic_processors,
const absl::flat_hash_map<std::string, rule_processor::base::ptr> &dynamic_processors,
ddwaf::timer &deadline)
{
const auto &id = rule->get_id();
Expand Down Expand Up @@ -79,7 +79,7 @@ void base_collection<Derived>::match(memory::vector<event> &events, const object
collection_cache &cache,
const memory::unordered_map<ddwaf::rule *, exclusion::filter_mode> &rules_to_exclude,
const memory::unordered_map<rule *, object_set> &objects_to_exclude,
const std::unordered_map<std::string, rule_processor::base::ptr> &dynamic_processors,
const absl::flat_hash_map<std::string, rule_processor::base::ptr> &dynamic_processors,
ddwaf::timer &deadline) const
{
if (cache.result >= Derived::type()) {
Expand Down
4 changes: 2 additions & 2 deletions src/collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct collection_cache {

template <typename Derived> class base_collection {
public:
using object_set = std::unordered_set<const ddwaf_object *>;
using object_set = absl::flat_hash_set<const ddwaf_object *>;
using cache_type = collection_cache;

base_collection() = default;
Expand All @@ -52,7 +52,7 @@ template <typename Derived> class base_collection {
collection_cache &cache,
const memory::unordered_map<ddwaf::rule *, exclusion::filter_mode> &rules_to_exclude,
const memory::unordered_map<ddwaf::rule *, object_set> &objects_to_exclude,
const std::unordered_map<std::string, rule_processor::base::ptr> &dynamic_processors,
const absl::flat_hash_map<std::string, rule_processor::base::ptr> &dynamic_processors,
ddwaf::timer &deadline) const;

protected:
Expand Down
6 changes: 3 additions & 3 deletions src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ std::optional<event::match> condition::match_target(T &it,
}

const rule_processor::base::ptr &condition::get_processor(
const std::unordered_map<std::string, rule_processor::base::ptr> &dynamic_processors) const
const absl::flat_hash_map<std::string, rule_processor::base::ptr> &dynamic_processors) const
{
if (processor_ || data_id_.empty()) {
return processor_;
Expand All @@ -79,8 +79,8 @@ const rule_processor::base::ptr &condition::get_processor(
}

std::optional<event::match> condition::match(const object_store &store,
const std::unordered_set<const ddwaf_object *> &objects_excluded, bool run_on_new,
const std::unordered_map<std::string, rule_processor::base::ptr> &dynamic_processors,
const absl::flat_hash_set<const ddwaf_object *> &objects_excluded, bool run_on_new,
const absl::flat_hash_map<std::string, rule_processor::base::ptr> &dynamic_processors,
ddwaf::timer &deadline) const
{
const auto &processor = get_processor(dynamic_processors);
Expand Down
6 changes: 3 additions & 3 deletions src/condition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class condition {
condition &operator=(const condition &) = delete;

std::optional<event::match> match(const object_store &store,
const std::unordered_set<const ddwaf_object *> &objects_excluded, bool run_on_new,
const std::unordered_map<std::string, rule_processor::base::ptr> &dynamic_processors,
const absl::flat_hash_set<const ddwaf_object *> &objects_excluded, bool run_on_new,
const absl::flat_hash_map<std::string, rule_processor::base::ptr> &dynamic_processors,
ddwaf::timer &deadline) const;

[[nodiscard]] const std::vector<condition::target_type> &get_targets() const
Expand All @@ -71,7 +71,7 @@ class condition {
const std::vector<transformer_id> &transformers, ddwaf::timer &deadline) const;

[[nodiscard]] const rule_processor::base::ptr &get_processor(
const std::unordered_map<std::string, rule_processor::base::ptr> &dynamic_processors) const;
const absl::flat_hash_map<std::string, rule_processor::base::ptr> &dynamic_processors) const;
std::vector<condition::target_type> targets_;
std::shared_ptr<rule_processor::base> processor_;
std::string data_id_;
Expand Down
2 changes: 1 addition & 1 deletion src/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ using filter_mode = exclusion::filter_mode;

class context {
public:
using object_set = std::unordered_set<const ddwaf_object *>;
using object_set = absl::flat_hash_set<const ddwaf_object *>;

explicit context(ruleset::ptr ruleset) : ruleset_(std::move(ruleset)), store_(ruleset_->free_fn)
{
Expand Down
6 changes: 4 additions & 2 deletions src/context_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <absl/container/flat_hash_map.h>
#include <absl/container/flat_hash_set.h>

namespace ddwaf::memory {
extern thread_local std::pmr::memory_resource *local_memory_resource;
Expand Down Expand Up @@ -99,9 +101,9 @@ template <typename T> using vector = std::vector<T, context_allocator<T>>;

template <class Key, class T, class Hash = std::hash<Key>, class Pred = std::equal_to<Key>>
using unordered_map =
std::unordered_map<Key, T, Hash, Pred, context_allocator<std::pair<const Key, T>>>;
absl::flat_hash_map<Key, T, Hash, Pred, context_allocator<std::pair<const Key, T>>>;

template <class T, class Hash = std::hash<T>, class Pred = std::equal_to<T>>
using unordered_set = std::unordered_set<T, Hash, Pred, context_allocator<T>>;
using unordered_set = absl::flat_hash_set<T, Hash, Pred, context_allocator<T>>;

} // namespace ddwaf::memory
2 changes: 1 addition & 1 deletion src/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void event_serializer::serialize(const memory::vector<event> &events, ddwaf_resu
ddwaf_object_array(&output.events);
ddwaf_object_array(&output.actions);

std::unordered_set<std::string_view> all_actions;
absl::flat_hash_set<std::string_view> all_actions;
for (const auto &event : events) {
ddwaf_object root_map;
ddwaf_object rule_map;
Expand Down
2 changes: 1 addition & 1 deletion src/exclusion/input_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class input_filter {

std::string_view get_id() { return id_; }

void get_addresses(std::unordered_set<std::string> &addresses) const
void get_addresses(absl::flat_hash_set<std::string> &addresses) const
{
for (const auto &cond : conditions_) {
for (const auto &target : cond->get_targets()) { addresses.emplace(target.name); }
Expand Down
10 changes: 5 additions & 5 deletions src/exclusion/object_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class path_trie {
// because trie_node is an incomplete type at this point
template <typename K, typename V> using MapType = std::map<K, V>;
#else
template <typename K, typename V> using MapType = std::unordered_map<K, V>;
template <typename K, typename V> using MapType = absl::flat_hash_map<K, V>;
#endif
MapType<std::string_view, trie_node> children_{};
};
Expand Down Expand Up @@ -237,7 +237,7 @@ inline std::ostream &operator<<(std::ostream &os, const path_trie::traverser::st

class object_filter {
public:
using cache_type = std::unordered_set<target_index>;
using cache_type = absl::flat_hash_set<target_index>;

explicit object_filter(const ddwaf::object_limits &limits = {}) : limits_(limits) {}

Expand All @@ -251,12 +251,12 @@ class object_filter {
memory::unordered_set<const ddwaf_object *> match(
const object_store &store, cache_type &cache, ddwaf::timer &deadline) const;

const std::unordered_map<std::string, target_index> &get_targets() const { return targets_; }
const absl::flat_hash_map<std::string, target_index> &get_targets() const { return targets_; }

protected:
object_limits limits_;
std::unordered_map<target_index, path_trie> target_paths_;
std::unordered_map<std::string, target_index> targets_;
absl::flat_hash_map<target_index, path_trie> target_paths_;
absl::flat_hash_map<std::string, target_index> targets_;
};

} // namespace ddwaf::exclusion
2 changes: 1 addition & 1 deletion src/exclusion/rule_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ rule_filter::rule_filter(std::string id, std::vector<condition::ptr> conditions,
}
}

optional_ref<const std::unordered_set<rule *>> rule_filter::match(
optional_ref<const absl::flat_hash_set<rule *>> rule_filter::match(
const object_store &store, cache_type &cache, ddwaf::timer &deadline) const
{
if (cache.result) {
Expand Down
6 changes: 3 additions & 3 deletions src/exclusion/rule_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ class rule_filter {
rule_filter(std::string id, std::vector<condition::ptr> conditions,
std::set<rule *> rule_targets, filter_mode mode = filter_mode::bypass);

optional_ref<const std::unordered_set<rule *>> match(
optional_ref<const absl::flat_hash_set<rule *>> match(
const object_store &store, cache_type &cache, ddwaf::timer &deadline) const;

std::string_view get_id() const { return id_; }
filter_mode get_mode() const { return mode_; }

void get_addresses(std::unordered_set<std::string> &addresses) const
void get_addresses(absl::flat_hash_set<std::string> &addresses) const
{
for (const auto &cond : conditions_) {
for (const auto &target : cond->get_targets()) { addresses.emplace(target.name); }
Expand All @@ -46,7 +46,7 @@ class rule_filter {
protected:
std::string id_;
std::vector<condition::ptr> conditions_;
std::unordered_set<rule *> rule_targets_;
absl::flat_hash_set<rule *> rule_targets_;
filter_mode mode_;
};

Expand Down
6 changes: 3 additions & 3 deletions src/iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace ddwaf::object {

template <typename T>
iterator_base<T>::iterator_base(
const std::unordered_set<const ddwaf_object *> &exclude, const object_limits &limits)
const absl::flat_hash_set<const ddwaf_object *> &exclude, const object_limits &limits)
: limits_(limits), excluded_(exclude)
{
stack_.reserve(initial_stack_size);
Expand Down Expand Up @@ -70,7 +70,7 @@ template <typename T> memory::vector<memory::string> iterator_base<T>::get_curre
}

value_iterator::value_iterator(const ddwaf_object *obj, const std::vector<std::string> &path,
const std::unordered_set<const ddwaf_object *> &exclude, const object_limits &limits)
const absl::flat_hash_set<const ddwaf_object *> &exclude, const object_limits &limits)
: iterator_base(exclude, limits)
{
initialise_cursor(obj, path);
Expand Down Expand Up @@ -217,7 +217,7 @@ void value_iterator::set_cursor_to_next_object()
}

key_iterator::key_iterator(const ddwaf_object *obj, const std::vector<std::string> &path,
const std::unordered_set<const ddwaf_object *> &exclude, const object_limits &limits)
const absl::flat_hash_set<const ddwaf_object *> &exclude, const object_limits &limits)
: iterator_base(exclude, limits)
{
initialise_cursor(obj, path);
Expand Down
8 changes: 4 additions & 4 deletions src/iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace ddwaf::object {

template <typename T> class iterator_base {
public:
explicit iterator_base(const std::unordered_set<const ddwaf_object *> &exclude,
explicit iterator_base(const absl::flat_hash_set<const ddwaf_object *> &exclude,
const object_limits &limits = object_limits());
~iterator_base() = default;

Expand Down Expand Up @@ -56,13 +56,13 @@ template <typename T> class iterator_base {
std::vector<std::pair<const ddwaf_object *, std::size_t>> stack_;
const ddwaf_object *current_{nullptr};

const std::unordered_set<const ddwaf_object *> &excluded_;
const absl::flat_hash_set<const ddwaf_object *> &excluded_;
};

class value_iterator : public iterator_base<value_iterator> {
public:
explicit value_iterator(const ddwaf_object *obj, const std::vector<std::string> &path,
const std::unordered_set<const ddwaf_object *> &exclude,
const absl::flat_hash_set<const ddwaf_object *> &exclude,
const object_limits &limits = object_limits());

~value_iterator() = default;
Expand Down Expand Up @@ -92,7 +92,7 @@ class value_iterator : public iterator_base<value_iterator> {
class key_iterator : public iterator_base<key_iterator> {
public:
explicit key_iterator(const ddwaf_object *obj, const std::vector<std::string> &path,
const std::unordered_set<const ddwaf_object *> &exclude,
const absl::flat_hash_set<const ddwaf_object *> &exclude,
const object_limits &limits = object_limits());

~key_iterator() = default;
Expand Down
2 changes: 1 addition & 1 deletion src/mkmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class multi_key_map {
[[nodiscard]] bool empty() const { return data_.empty(); }

protected:
std::unordered_map<Key, std::unordered_map<Key, std::set<T>>> data_;
absl::flat_hash_map<Key, absl::flat_hash_map<Key, std::set<T>>> data_;
};

} // namespace ddwaf
2 changes: 1 addition & 1 deletion src/parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ parameter::operator parameter::map() const
return parameter::map();
}

std::unordered_map<std::string_view, parameter> map;
absl::flat_hash_map<std::string_view, parameter> map;
map.reserve(nbEntries);
for (unsigned i = 0; i < nbEntries; i++) {
const parameter &kv = array[i];
Expand Down
7 changes: 5 additions & 2 deletions src/parameter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
#include <unordered_set>
#include <vector>

#include <absl/container/flat_hash_map.h>
#include <absl/container/flat_hash_set.h>

namespace ddwaf {

class parameter : public ddwaf_object {
public:
using map = std::unordered_map<std::string_view, parameter>;
using map = absl::flat_hash_map<std::string_view, parameter>;
using vector = std::vector<parameter>;
using string_set = std::unordered_set<std::string_view>;
using string_set = absl::flat_hash_set<std::string_view>;

parameter() = default;
parameter(const ddwaf_object &arg) : _ddwaf_object() { *((ddwaf_object *)this) = arg; }
Expand Down
4 changes: 2 additions & 2 deletions src/parser/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ void parse(

namespace v2 {
rule_spec_container parse_rules(parameter::vector &rule_array, base_section_info &info,
std::unordered_map<std::string, std::string> &rule_data_ids, const object_limits &limits,
absl::flat_hash_map<std::string, std::string> &rule_data_ids, const object_limits &limits,
rule::source_type source = rule::source_type::base);

rule_data_container parse_rule_data(parameter::vector &rule_data, base_section_info &info,
std::unordered_map<std::string, std::string> &rule_data_ids);
absl::flat_hash_map<std::string, std::string> &rule_data_ids);

override_spec_container parse_overrides(parameter::vector &override_array, base_section_info &info);

Expand Down
6 changes: 3 additions & 3 deletions src/parser/parser_v1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ condition::ptr parseCondition(
}

void parseRule(parameter::map &rule, base_section_info &info,
std::unordered_set<std::string_view> &rule_ids, ddwaf::ruleset &rs, ddwaf::object_limits limits)
absl::flat_hash_set<std::string_view> &rule_ids, ddwaf::ruleset &rs, ddwaf::object_limits limits)
{
auto id = at<std::string>(rule, "id");
if (rule_ids.find(id) != rule_ids.end()) {
Expand Down Expand Up @@ -137,7 +137,7 @@ void parseRule(parameter::map &rule, base_section_info &info,
conditions.push_back(parseCondition(cond, rule_transformers, limits));
}

std::unordered_map<std::string, std::string> tags;
absl::flat_hash_map<std::string, std::string> tags;
for (auto &[key, value] : at<parameter::map>(rule, "tags")) {
try {
tags.emplace(key, std::string(value));
Expand Down Expand Up @@ -172,7 +172,7 @@ void parse(

auto &section = info.add_section("rules");

std::unordered_set<std::string_view> rule_ids;
absl::flat_hash_set<std::string_view> rule_ids;
for (unsigned i = 0; i < rules_array.size(); ++i) {
const auto &rule_param = rules_array[i];
try {
Expand Down
Loading