Skip to content

Commit e0b1994

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 e0b1994

File tree

17 files changed

+2288
-799
lines changed

17 files changed

+2288
-799
lines changed

CHANGELOG

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ Unreleased:
44
SQL Standard library:
55
*
66
Trace Processor:
7-
*
7+
* Improved performance of parsing JSON by 7x. JSON traces should now load
8+
much faster in the Perfetto UI and the trace processor.
89
UI:
9-
*
10+
* Improved JSON loading performance. See `Trace Processor` section above.
1011
SDK:
1112
*
1213

src/trace_processor/importers/common/parser_types.h

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
#include <array>
2121
#include <cstdint>
2222
#include <functional>
23+
#include <limits>
24+
#include <memory>
2325
#include <optional>
24-
#include <string>
2526
#include <utility>
2627
#include <variant>
28+
#include <vector>
2729

2830
#include "perfetto/trace_processor/ref_counted.h"
2931
#include "perfetto/trace_processor/trace_blob_view.h"
32+
#include "src/trace_processor/containers/interval_tree.h"
3033
#include "src/trace_processor/containers/string_pool.h"
3134
#include "src/trace_processor/importers/proto/packet_sequence_state_generation.h"
3235

@@ -57,16 +60,74 @@ struct alignas(8) InlineSchedWaking {
5760
static_assert(sizeof(InlineSchedWaking) == 16);
5861

5962
struct alignas(8) JsonEvent {
60-
struct Begin {};
61-
struct End {};
62-
struct Scoped {
63-
int64_t dur;
63+
using StrOrUint64 = std::variant<std::monostate, StringPool::Id, uint64_t>;
64+
65+
int64_t dur = std::numeric_limits<int64_t>::max();
66+
67+
uint32_t pid = 0;
68+
uint32_t tid = 0;
69+
70+
StringPool::Id name = StringPool::Id::Null();
71+
StringPool::Id cat = StringPool::Id::Null();
72+
73+
union IdStrOrUint64 {
74+
StringPool::Id id_str;
75+
uint64_t id_uint64;
76+
};
77+
IdStrOrUint64 id;
78+
IdStrOrUint64 bind_id;
79+
80+
int64_t tts = std::numeric_limits<int64_t>::max();
81+
int64_t tdur = std::numeric_limits<int64_t>::max();
82+
int64_t async_cookie = std::numeric_limits<int64_t>::max();
83+
84+
std::unique_ptr<char[]> args;
85+
86+
char phase = '\0';
87+
88+
uint64_t flow_in : 1;
89+
uint64_t flow_out : 1;
90+
91+
uint64_t pid_is_string_id : 1;
92+
uint64_t tid_is_string_id : 1;
93+
94+
uint64_t bind_enclosing_slice : 1;
95+
96+
enum class IdType : uint64_t {
97+
kNone = 0,
98+
kString = 1,
99+
kUint64 = 2,
100+
};
101+
IdType id_type : 2;
102+
IdType bind_id_type : 2;
103+
104+
enum class Scope : uint64_t {
105+
kNone = 0,
106+
kGlobal = 1,
107+
kProcess = 2,
108+
kThread = 3,
64109
};
65-
struct Other {};
66-
using Type = std::variant<Begin, End, Scoped, Other>;
110+
Scope scope : 2;
67111

68-
std::string value;
69-
Type type;
112+
enum class AsyncCookieType : uint64_t {
113+
kId,
114+
kId2Local,
115+
kId2Global,
116+
};
117+
AsyncCookieType async_cookie_type : 2;
118+
119+
uint64_t args_size : 43;
120+
121+
JsonEvent()
122+
: flow_in(false),
123+
flow_out(false),
124+
pid_is_string_id(false),
125+
tid_is_string_id(false),
126+
bind_enclosing_slice(false),
127+
id_type(IdType::kNone),
128+
bind_id_type(IdType::kNone),
129+
scope(Scope::kNone),
130+
args_size(0) {}
70131
};
71132
static_assert(sizeof(JsonEvent) % 8 == 0);
72133

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)