Skip to content

Commit

Permalink
ref(replay): More efficient deserialization (#1782)
Browse files Browse the repository at this point in the history
After deploying #1678, we saw a
rise in memory consumption. We narrowed down the reason to
deserialization of replay recordings, so this PR attempts to replace
those deserializers with more efficient versions that do not parse an
entire `serde_json::Value` to get the tag (`type`, `source`) of the
enum.

A custom deserializer is necessary because serde does not support
[integer tags for internally tagged
enums](serde-rs/serde#745).

- [x] Custom deserializer for `NodeVariant`, based on serde's own
`derive(Deserialize)` of internally tagged enums.
- [x] Custom deserializer for `recording::Event`, based on serde's own
`derive(Deserialize)` of internally tagged enums.
- [x] Custom deserializer for `IncrementalSourceDataVariant`, based on
serde's own `derive(Deserialize)` of internally tagged enums.
- [x] Box all enum variants.

### Benchmark comparison

Ran a criterion benchmark on `rrweb.json`. It does not tell us anything
about memory consumption, but the reduced cpu usage points to simpler
deserialization:

#### Before

```
rrweb/1                 time:   [142.37 ms 148.17 ms 155.61 ms]
```

#### After

```
rrweb/1                 time:   [31.474 ms 31.801 ms 32.137 ms]
```

#skip-changelog

---------

Co-authored-by: Colton Allen <[email protected]>
Co-authored-by: Oleksandr <[email protected]>
  • Loading branch information
3 people authored Jan 27, 2023
1 parent 37e8464 commit 16a0d44
Show file tree
Hide file tree
Showing 9 changed files with 411 additions and 205 deletions.
131 changes: 81 additions & 50 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion relay-general/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ utf16string = "0.2.0"
uuid = { version = "0.8.1", features = ["v4", "serde"] }

[dev-dependencies]
criterion = "0.3"
criterion = "0.4"
insta = { version = "1.19.0", features = ["json", "redactions", "ron", "yaml"] }
pretty-hex = "0.2.0"
similar-asserts = "1.4.2"
Expand Down
2 changes: 1 addition & 1 deletion relay-metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ thiserror = "1.0.20"
tokio = { version = "1.0", features = ["macros", "time"] }

[dev-dependencies]
criterion = "0.3"
criterion = "0.4"
insta = "1.19.0"
relay-statsd = { path = "../relay-statsd", features = ["test"] }
relay-test = { path = "../relay-test" }
Expand Down
5 changes: 5 additions & 0 deletions relay-replays/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@ unicase = "2.6.0"
flate2 = "1.0.19"

[dev-dependencies]
criterion = "0.4"
insta = { version = "1.1.0", features = ["ron"] }
assert-json-diff = "2.0.2"

[[bench]]
name = "benchmarks"
harness = false
14 changes: 14 additions & 0 deletions relay-replays/benches/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};

use relay_replays::recording::_deserialize_event;

fn bench_recording(c: &mut Criterion) {
let payload = include_bytes!("../tests/fixtures/rrweb.json");

c.bench_with_input(BenchmarkId::new("rrweb", 1), &payload, |b, &_| {
b.iter(|| _deserialize_event(payload));
});
}

criterion_group!(benches, bench_recording);
criterion_main!(benches);
Loading

0 comments on commit 16a0d44

Please sign in to comment.