Skip to content

Optimize dbBox serialization and deserialization - #11225

Open
debayanbandyopadhyay wants to merge 1 commit into
The-OpenROAD-Project:masterfrom
debayanbandyopadhyay:serialization-modernization-opt
Open

Optimize dbBox serialization and deserialization#11225
debayanbandyopadhyay wants to merge 1 commit into
The-OpenROAD-Project:masterfrom
debayanbandyopadhyay:serialization-modernization-opt

Conversation

@debayanbandyopadhyay

@debayanbandyopadhyay debayanbandyopadhyay commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR optimizes the serialization and deserialization of _dbBox in ODB to improve database write/read performance.

Key changes:

  • Optimized Writing (operator<<): Replaced field-by-field serialization of _dbBox flags, shapes (Rect/Oct), and metadata with flattened raw byte writes (stream.writeBytes) using robust sizeof formulas to calculate layout sizes safely without magic numbers.
  • Optimized Reading (operator>>): Refactored deserialization to read raw bytes into structured data blocks (OctData, RectData, etc.) and safely unpack them into _dbBox fields using a new unpack_box helper.
  • Strict-Aliasing & Compile-time Safety: Handled legacy flag unpacking safely using std::bit_cast and C++20 concepts (std::is_trivially_copyable). Added static_assert size checks for geometric shapes and serialization structs to guarantee layout consistency across platforms.
  • Regression Tests: Added a comprehensive DbBoxSerializationPublic unit test in TestDbStream.cpp that verifies serialization/deserialization correctness and schema compatibility for both Rect and Oct boxes. The test is implemented using only public ODB APIs to comply with Bazel build module/layering boundaries.

Type of Change

  • Refactoring

Impact

This change improves ODB database write performance. Internal Google3 benchmarks on design writes showed a 11% to 17% write-time speedup (average ~15.5% improvement) with no changes to the database schema or user-facing behavior.

Verification

  • I have verified that the local build succeeds (./etc/Build.sh).
  • I have run the relevant tests and they pass.
  • My code follows the repository's formatting guidelines.
  • I have included tests to prevent regressions.
  • I have signed my commits (DCO).

Related Issues

N/A

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the serialization and deserialization of _dbBox (both rectangular and octagonal shapes) to use raw byte streaming via std::span and std::bit_cast, improving performance and simplifying the code. It also adds a comprehensive unit test to verify the correctness of the serialization. The feedback suggests enhancing safety and robustness by adding compile-time size assertions for the serialization structures and replacing hardcoded magic numbers in the byte-writing operations with sizeof expressions.

Comment thread src/odb/src/db/dbBox.cpp
Comment thread src/odb/src/db/dbBox.cpp
@debayanbandyopadhyay
debayanbandyopadhyay force-pushed the serialization-modernization-opt branch from eaa5d2f to 641f596 Compare August 24, 2026 19:22
@github-actions github-actions Bot added size/XS and removed size/M labels Aug 24, 2026
@github-actions github-actions Bot added size/M and removed size/XS labels Aug 24, 2026
@debayanbandyopadhyay
debayanbandyopadhyay force-pushed the serialization-modernization-opt branch from 8c52e51 to 264b7de Compare August 24, 2026 19:37
@debayanbandyopadhyay
debayanbandyopadhyay marked this pull request as draft August 24, 2026 20:33
@debayanbandyopadhyay
debayanbandyopadhyay force-pushed the serialization-modernization-opt branch 3 times, most recently from 856bd52 to 9bb0892 Compare August 25, 2026 03:42
Signed-off-by: Debayan Bandyopadhyay <dbandyopadhyay@google.com>
@debayanbandyopadhyay
debayanbandyopadhyay force-pushed the serialization-modernization-opt branch from 9bb0892 to 47c66f6 Compare August 25, 2026 05:05
@debayanbandyopadhyay
debayanbandyopadhyay marked this pull request as ready for review August 25, 2026 05:18
@maliberty

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 47c66f625f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@maliberty

Copy link
Copy Markdown
Member

I see the gain is real but the cost is a lot of extraneous structs and code complexity. I explored some alternatives with Claude and I think this is a better approach

template <typename... Ts>
  requires(... && std::is_trivially_copyable_v<Ts>)
void writeValues(const Ts&... vals)
{
  constexpr size_t kTotal = (sizeof(Ts) + ...);
  static_assert(kTotal <= kBufferSize);
  if (buffer_pos_ + kTotal > kBufferSize) {
    flush();
  }
  char* p = buffer_.data() + buffer_pos_;
  ((std::memcpy(p, &vals, sizeof(Ts)), p += sizeof(Ts)), ...);
  buffer_pos_ += kTotal;
}`

The main savings you are getting is from reducing the number of calls to writeBytes and this achieves the effect in a simpler fashion. Then you can do

stream.writeValues(bits, r.xMin(), r.yMin(), r.xMax(), r.yMax(),
                   box.owner_, box.next_box_.id(),
                   box.design_rule_width_, box.min_spacing_);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants