Skip to content

Serialize auction in blocking task#4186

Merged
MartinquaXD merged 4 commits intomainfrom
serialize-auction-in-blocking-task
Feb 23, 2026
Merged

Serialize auction in blocking task#4186
MartinquaXD merged 4 commits intomainfrom
serialize-auction-in-blocking-task

Conversation

@MartinquaXD
Copy link
Contributor

@MartinquaXD MartinquaXD commented Feb 19, 2026

Description

The task that writes the current auction to the DB has 2 performance issues:

  • unnecessary conversions: instead of serializing the DTO to a JSON string we convert it to JsonValue and let sqlx do the conversion to string
  • conversions on wrong task: serializing data is very slow and blocks an entire runtime thread if we don't do the sync work in a blocking task (the entire process often takes more than 1s)

Changes

  • skip 1 conversion by directly serializing the DTO to string and writing a string to the DB (same pattern already applied by some other JSON upload)
  • serialize the DTO on a blocking task

How to test

adjusted existing e2e tests, they show that the serialized data can be read back into the exact JSON value we serialized in the first place

@MartinquaXD MartinquaXD requested a review from a team as a code owner February 19, 2026 21:18
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

No critical issues found. The PR correctly optimizes the auction serialization process by offloading it to a blocking task and removing an unnecessary intermediate conversion to JsonValue. These changes align with the performance goals stated in the description and follow Rust best practices for handling CPU-intensive tasks in an async environment.

@jmg-duarte
Copy link
Contributor

LGTM just fix the tests and should be G2G

@MartinquaXD MartinquaXD marked this pull request as draft February 19, 2026 21:23
@MartinquaXD MartinquaXD marked this pull request as draft February 19, 2026 21:23
@MartinquaXD MartinquaXD marked this pull request as ready for review February 19, 2026 21:29
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request optimizes the auction persistence process by serializing the auction DTO directly to a JSON string and offloading this operation to a blocking task. This change eliminates an unnecessary intermediate conversion to JsonValue and prevents the async runtime threads from being blocked by the potentially slow serialization process. No critical issues found.

.start_timer();

let data = serde_json::to_value(new_auction_data)?;
let data = tokio::task::spawn_blocking(move || serde_json::to_string(&new_auction_data))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any estimates regarding how "heavy" this operation is would be helpful in the pr description to justify the usage of a separate thread pool.

Copy link
Contributor

@jmg-duarte jmg-duarte Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this operation takes more than 100ms it's already more than eligible for the blocking thread pool

Martin mentions in the description that:

the entire process often takes more than 1s

So this sounds very reasonable

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, i missed that somehow. Anyway, my concern is explained here #4187 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to some tokio blog I currently fail to find anything over 100µm run time should be considered a blocking task. For many tasks sprinkling in tokio::task::yield should already be pretty good but for anything over 1ms using tokio::task::spawn_blocking seems reasonable.

Copy link
Contributor

@m-sz m-sz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MartinquaXD MartinquaXD added this pull request to the merge queue Feb 23, 2026
Merged via the queue into main with commit 250f603 Feb 23, 2026
19 checks passed
@MartinquaXD MartinquaXD deleted the serialize-auction-in-blocking-task branch February 23, 2026 13:01
@github-actions github-actions bot locked and limited conversation to collaborators Feb 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants