Skip to content

Commit

Permalink
Avoiding JSON event copies in get_values function, added get_predicat…
Browse files Browse the repository at this point in the history
…es function to grapher/utils/config.hpp
  • Loading branch information
JPenuchot committed Apr 5, 2022
1 parent f7126ca commit 0c3deb6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
4 changes: 4 additions & 0 deletions grapher/include/grapher/utils/config.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <grapher/predicates.hpp>

#include <vector>

#include <nlohmann/json.hpp>
Expand All @@ -18,6 +20,8 @@ struct group_descriptor_t {

group_descriptor_t get_default_group_descriptor();

std::vector<predicate_t> get_predicates(group_descriptor_t const &descriptor);

std::vector<nlohmann::json>
extract_group(group_descriptor_t const &descriptor,
std::vector<nlohmann::json> const &events);
Expand Down
12 changes: 9 additions & 3 deletions grapher/lib/grapher/utils/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <llvm/Support/raw_ostream.h>

#include <nlohmann/json.hpp>
#include <vector>

#include "grapher/predicates.hpp"
#include "grapher/utils/json.hpp"
Expand All @@ -23,14 +24,19 @@ group_descriptor_t get_default_group_descriptor() {
}})};
}

std::vector<nlohmann::json>
extract_group(group_descriptor_t const &descriptor,
std::vector<nlohmann::json> const &events) {
std::vector<predicate_t> get_predicates(group_descriptor_t const &descriptor) {
std::vector<predicate_t> predicates;

predicates.reserve(descriptor.predicates.size());
std::ranges::transform(descriptor.predicates, std::back_inserter(predicates),
get_predicate);
return predicates;
}

std::vector<nlohmann::json>
extract_group(group_descriptor_t const &descriptor,
std::vector<nlohmann::json> const &events) {
std::vector<predicate_t> predicates = get_predicates(descriptor);

std::vector<nlohmann::json> res;

Expand Down
23 changes: 12 additions & 11 deletions grapher/lib/grapher/utils/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,23 @@ std::vector<double> get_values(benchmark_iteration_t const &iteration,
repetition_ifstream >> j;
}

std::vector<nlohmann::json> events =
json_value<std::vector<nlohmann::json>>(j, "traceEvents");

// Filter events
std::vector<nlohmann::json> matching_events =
extract_group(descriptor, events);

// Check for data
if (matching_events.empty()) {
if (!j.contains("traceEvents") || !j["traceEvents"].is_array() ||
j["traceEvents"].empty()) {
return 0.;
}

nlohmann::json::array_t const &events =
j["traceEvents"].get_ref<nlohmann::json::array_t const &>();

std::vector<predicate_t> predicates = get_predicates(descriptor);

// Accumulate
double val = 0.;
for (nlohmann::json const &event : matching_events) {
val += json_value<double>(event, value_jptr);
for (nlohmann::json const &event : events) {
if (std::all_of(predicates.begin(), predicates.end(),
[&](predicate_t const &p) -> bool { return p(event); })) {
val += json_value<double>(event, value_jptr);
}
}
return val;
};
Expand Down

0 comments on commit 0c3deb6

Please sign in to comment.