Skip to content

Commit d31096a

Browse files
committed
[DNS] tp: speed up JSON tokenization/parsing by 4.5x
Switch away from jsoncpp which has atrocious perf for parsing in favour of writing our own parser. Moreover, remove some uses of a poor quality json value fetch function in favour of our own. test Fix fix fix
1 parent 02240e4 commit d31096a

File tree

13 files changed

+1177
-539
lines changed

13 files changed

+1177
-539
lines changed

include/perfetto/ext/base/circular_queue.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "perfetto/base/logging.h"
2727
#include "perfetto/ext/base/utils.h"
28+
#include "perfetto/public/compiler.h"
2829

2930
namespace perfetto {
3031
namespace base {
@@ -308,7 +309,7 @@ class CircularQueue {
308309
entries_ = std::move(new_vec);
309310
}
310311

311-
inline T* Get(uint64_t pos) {
312+
PERFETTO_ALWAYS_INLINE inline T* Get(uint64_t pos) {
312313
PERFETTO_DCHECK(pos >= begin_ && pos < end_);
313314
PERFETTO_DCHECK((capacity_ & (capacity_ - 1)) == 0); // Must be a pow2.
314315
auto index = static_cast<size_t>(pos & (capacity_ - 1));

src/trace_processor/importers/common/parser_types.h

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
#include <array>
2121
#include <cstdint>
2222
#include <functional>
23+
#include <limits>
2324
#include <optional>
24-
#include <string>
2525
#include <utility>
2626
#include <variant>
2727

2828
#include "perfetto/trace_processor/ref_counted.h"
2929
#include "perfetto/trace_processor/trace_blob_view.h"
30+
#include "src/trace_processor/containers/interval_tree.h"
3031
#include "src/trace_processor/containers/string_pool.h"
3132
#include "src/trace_processor/importers/proto/packet_sequence_state_generation.h"
3233

@@ -57,16 +58,79 @@ struct alignas(8) InlineSchedWaking {
5758
static_assert(sizeof(InlineSchedWaking) == 16);
5859

5960
struct alignas(8) JsonEvent {
60-
struct Begin {};
61-
struct End {};
62-
struct Scoped {
63-
int64_t dur;
61+
using StrOrUint64 = std::variant<std::monostate, StringPool::Id, uint64_t>;
62+
63+
int64_t dur = std::numeric_limits<int64_t>::max();
64+
65+
uint32_t pid = 0;
66+
uint32_t tid = 0;
67+
68+
StringPool::Id name = StringPool::Id::Null();
69+
StringPool::Id cat = StringPool::Id::Null();
70+
71+
union IdStrOrUint64 {
72+
StringPool::Id id_str;
73+
uint64_t id_uint64;
6474
};
65-
struct Other {};
66-
using Type = std::variant<Begin, End, Scoped, Other>;
75+
IdStrOrUint64 id;
76+
IdStrOrUint64 bind_id;
77+
78+
int64_t tts = std::numeric_limits<int64_t>::max();
79+
int64_t tdur = std::numeric_limits<int64_t>::max();
80+
81+
char phase = '\0';
6782

68-
std::string value;
69-
Type type;
83+
uint64_t flow_in : 1;
84+
uint64_t flow_out : 1;
85+
86+
uint64_t pid_is_string_id : 1;
87+
uint64_t tid_is_string_id : 1;
88+
89+
uint64_t bind_enclosing_slice : 1;
90+
91+
enum class IdType : uint64_t {
92+
kNone = 0,
93+
kString = 1,
94+
kUint64 = 2,
95+
};
96+
IdType id_type : 2;
97+
IdType bind_id_type : 2;
98+
99+
enum class Scope : uint64_t {
100+
kNone = 0,
101+
kGlobal = 1,
102+
kProcess = 2,
103+
kThread = 3,
104+
};
105+
Scope scope : 2;
106+
107+
JsonEvent()
108+
: flow_in(false),
109+
flow_out(false),
110+
pid_is_string_id(false),
111+
tid_is_string_id(false),
112+
bind_enclosing_slice(false),
113+
id_type(IdType::kNone),
114+
bind_id_type(IdType::kNone),
115+
scope(Scope::kNone) {}
116+
117+
// json::JsonValue2 dur;
118+
// uint32_t pid;
119+
// uint32_t tid;
120+
// json::JsonValue2 id;
121+
// json::JsonValue2 cat;
122+
// json::JsonValue2 name;
123+
124+
// struct Begin {};
125+
// struct End {};
126+
// struct Scoped {
127+
// int64_t dur;
128+
// };
129+
// struct Other {};
130+
// using Type = std::variant<Begin, End, Scoped, Other>;
131+
132+
// std::string value;
133+
// Type type;
70134
};
71135
static_assert(sizeof(JsonEvent) % 8 == 0);
72136

src/trace_processor/importers/common/trace_parser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <cstdint>
2121
#include <string>
22+
#include "src/trace_processor/importers/common/parser_types.h"
2223

2324
namespace perfetto::trace_processor {
2425
namespace perf_importer {
@@ -63,7 +64,7 @@ class ProtoTraceParser {
6364
class JsonTraceParser {
6465
public:
6566
virtual ~JsonTraceParser();
66-
virtual void ParseJsonPacket(int64_t, std::string) = 0;
67+
virtual void ParseJsonPacket(int64_t, JsonEvent) = 0;
6768
virtual void ParseSystraceLine(int64_t, SystraceLine) = 0;
6869
virtual void ParseLegacyV8ProfileEvent(int64_t, LegacyV8CpuProfileEvent) = 0;
6970
};

0 commit comments

Comments
 (0)