NewsCrawler.org

Switching to jemalloc cut serialization time in a Rust Tokio service by over half

Sequence diagram with three participants: client, Tokio server worker, and business logic worker pool. The client sends an Ion request to the Tokio server worker, which deserializes it and allocates the domain model. The worker dispatches the work to the business logic worker pool, which runs the business logic, drops the request, and allocates the response. The pool returns the response to the Tokio server worker, which serializes it, drops the response, and sends the Ion response to the client.
Image: dial9-rs.github.io

Matthew Pope describes a multi-threaded Rust service on Tokio whose serialization and deserialization ran many times slower in production than in local benchmarks. The service parses Amazon Ion requests with ion-rs, and in some production CPU profiles more than 40% of the work was tied to serialization and deserialization. The investigation pointed to contention in the global allocator[1].

In a CPU flame graph, about 63% of samples under ion_rs frames had a clone or drop on the stack and about 60% had malloc, free or realloc, while only about 22% were in the reader itself. The author explains that freeing memory on a different thread is costly, because the memory must return to the thread that owns it and cache lines move between cores, which can cause false sharing[2].

Replacing the global allocator with jemalloc[3] through tikv-jemallocator was a two-line change that cut serialization and deserialization time by more than half at the median and by about 40% at p99[4]. The author says this was not the only fix the team needed, but it was a simple change with a high payoff.

Repository jemalloc github.com

jemalloc is a general-purpose memory allocator that replaces the default malloc and is often used to reduce memory contention in multi-threaded programs.

Stars: 11,000Forks: 1,600License: NOASSERTIONUpdated:

Read the original dial9-rs.github.ioDiscussion

Discussion

Commenters mostly ask questions rather than answer them. One asks whether glibc malloc ever beats jemalloc and whether switching to an allocator such as mimalloc is always advisable, and no answers are given. Another says the article should have named glibc earlier, and that using "serde" for the team's own serialization code is confusing.

Sources

Published: First seen: Читать по-русски

Open as a page