Skip to content

odb: Modernize dbOStream and dbIStream with C++20 concepts and 64KB buffering - #11190

Open
debayanbandyopadhyay wants to merge 2 commits into
The-OpenROAD-Project:masterfrom
debayanbandyopadhyay:modernize-dbstream
Open

odb: Modernize dbOStream and dbIStream with C++20 concepts and 64KB buffering#11190
debayanbandyopadhyay wants to merge 2 commits into
The-OpenROAD-Project:masterfrom
debayanbandyopadhyay:modernize-dbstream

Conversation

@debayanbandyopadhyay

Copy link
Copy Markdown
Contributor

Summary

This PR modernizes the database stream classes (dbOStream and dbIStream) in odb using modern C++20 features and introduces a 64KB internal buffer for dbOStream to improve serialization performance during write_db:

  • 64KB Internal Write Buffering: Groups scalar writes (arithmetic types, enums, PODs) in dbOStream into a 64KB buffer to minimize underlying std::ostream overhead, while allowing large byte payloads to bypass directly.
  • C++20 Concepts (requires): Replaces verbose SFINAE (std::enable_if_t) with explicit C++20 requires clauses for cleaner compile diagnostics.
  • Zero-Copy Streaming: Adopts std::span<const char> for raw byte writes and std::string_view for string serialization to avoid temporary std::string allocations.
  • Expanded Unit Testing: Adds TestDbStream.cpp with comprehensive test coverage for fundamental types and standard containers (std::vector, std::map, std::tuple, std::variant).

Type of Change

  • Refactoring

Impact

  • Performance: Accelerates write_db runtime by ~15% to 30% across flow checkpoints (synthesis, floorplan, placement).
  • Compatibility: 100% byte-for-byte binary and format compatible with existing ODB databases (zero behavioral or format regressions).

Verification

  • I have verified that the local build succeeds (./etc/Build.sh).
  • I have run the relevant tests and they pass (TestDbStream, TestObjectType, and ODB regressions).
  • My code follows the repository's formatting guidelines.
  • I have included tests to prevent regressions (src/odb/test/cpp/TestDbStream.cpp).
  • 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 modernizes the database streaming classes dbOStream and dbIStream by introducing buffering, using C++20 concepts and templates for arithmetic types, and utilizing std::string_view to avoid unnecessary allocations. It also adds a comprehensive suite of unit tests for these streams. The review feedback correctly identifies that the new template constraints on operator<< and operator>> exclude enums (such as dbObjectType), which will cause compilation failures, and suggests expanding the constraints to include std::is_enum_v<T>. Additionally, a syntax error was found in src/odb/test/cpp/CMakeLists.txt due to a stray line.

Comment thread src/odb/include/odb/dbStream.h
Comment thread src/odb/include/odb/dbStream.h
Comment thread src/odb/test/cpp/CMakeLists.txt
…uffering

- Add 64KB internal buffering in dbOStream for scalar types.
- Replace SFINAE with C++20 requires clauses (including enums).
- Use std::span and std::string_view for zero-copy streaming.
- Add TestDbStream unit test.

Signed-off-by: Debayan Bandyopadhyay <dbandyopadhyay@google.com>
@maliberty

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ad2a743606

ℹ️ 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".

Comment thread src/odb/include/odb/dbStream.h Outdated
using Position = std::ostream::pos_type;

dbOStream(_dbDatabase* db, std::ostream& f);
~dbOStream() { flush(); }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep exceptions out of the stream destructor

When the underlying stream throws on write, any final payload smaller than 64 KB is now written by ~dbOStream(). Destructors are implicitly noexcept, so this exception calls std::terminate instead of propagating; dbDatabase::writeEco enables failbit | badbit exceptions and relies on this destructor flush, making disk-full or other I/O failures an uncatchable process abort. Flush explicitly before destruction, as dbDatabase::write now does, and avoid throwing from the destructor.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Wrapped flush() in ~dbOStream() with a try-catch block to prevent exceptions from propagating and causing aborts. Added explicit stream.flush() calls in dbDatabase::write, dbDatabase::writeEco, and relevant test cases to ensure any I/O errors during write are still caught and handled when it is safe.

add_executable(TestSwapMasterUnusedPort TestSwapMasterUnusedPort.cpp)
add_executable(TestWriteReadDbHier TestWriteReadDbHier.cpp)
add_executable(TestObjectType TestObjectType.cpp)
add_executable(TestDbStream TestDbStream.cpp)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Add the stream test to the Bazel test suites

The new executable is declared only in CMake: a repo-wide search finds no TestDbStream target in src/odb/test/cpp/BUILD and no entry in src/odb/test/BUILD's cpp_tests suite. Consequently Bazel CI neither builds nor runs this regression; add the corresponding cc_test target and suite entry.

AGENTS.md reference: AGENTS.md:L28-L31

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Added the TestDbStream cc_test target definition to src/odb/test/cpp/BUILD.

@maliberty maliberty left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks this code is quite old.

Comment thread src/odb/include/odb/dbStream.h Outdated

dbOStream& operator<<(int64_t c)
void write_bytes(std::span<const char> bytes)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Match the surrounding naming style writeBytes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Renamed write_bytes to writeBytes in dbStream.h and updated all usages in tests.

Comment thread src/odb/include/odb/dbStream.h Outdated
}

dbOStream& operator<<(uint16_t c)
void flush() const

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

flush() is not const on an iostream and I don't think it should be there. I think that would allow buffer_pos_ to not require mutable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Made flush() and pos() non-const, and removed the mutable qualifier from buffer_pos_.

@maliberty maliberty Aug 20, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add the test to BUILD for bazel

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Added the test to the cpp_tests suite in src/odb/test/BUILD and defined the cc_test target in src/odb/test/cpp/BUILD.

@debayanbandyopadhyay
debayanbandyopadhyay force-pushed the modernize-dbstream branch 2 times, most recently from f70b561 to 3f9be03 Compare August 21, 2026 09:15
…rite_bytes

Signed-off-by: Debayan Bandyopadhyay <dbandyopadhyay@google.com>
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