Skip to content

Refactor scheduler and base queue to use Abseil primitives#183

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/refactor-apollo-code-abseil-folly
Draft

Refactor scheduler and base queue to use Abseil primitives#183
Copilot wants to merge 2 commits intomainfrom
copilot/refactor-apollo-code-abseil-folly

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 26, 2026

Replaces STL synchronization and container primitives in the cyber scheduler and base queue with Abseil equivalents for improved cache efficiency, richer thread-safety annotations, and cleaner async patterns.

Changes

cyber/base/thread_safe_queue.h

  • std::mutex + std::condition_variableabsl::Mutex + absl::MutexLock + Await()
  • volatile bool break_all_wait_bool with ABSL_GUARDED_BY(mutex_) annotation
  • Private IsReadyForDequeue() condition predicate for Await()

cyber/scheduler/scheduler.h + policy headers

  • std::unordered_mapabsl::flat_hash_map (Swiss table; better cache locality)
  • std::mutex cr_wl_mtx_absl::Mutex

cyber/scheduler/scheduler*.cc

  • std::lock_guard<std::mutex> on cr_wl_mtx_absl::WriterMutexLock
  • String .append() chains → absl::StrAppend() / absl::StrCat()
  • Singleton guard in scheduler_factory.cc: std::mutexabsl::Mutex

Build

  • Added @com_google_absl//absl/container:flat_hash_map, absl/synchronization, and absl/strings deps to affected targets.

Example: WaitDequeue before/after

// Before
bool WaitDequeue(T* element) {
  std::unique_lock<std::mutex> lock(mutex_);
  cv_.wait(lock, [this]() { return break_all_wait_ || !queue_.empty(); });
  ...
}

// After
bool WaitDequeue(T* element) {
  absl::MutexLock lock(&mutex_);
  mutex_.Await(absl::Condition(this, &ThreadSafeQueue::IsReadyForDequeue));
  ...
}
Original prompt

This section details on the original issue you should resolve

<issue_title>[Feature]: Refactor the following Apollo C++ code to use Abseil and Folly for better performance.</issue_title>
<issue_description>### Use Case / Motivation

Improve the performance of the base library and ensure consistency in coding style.

Proposed Implementation

Category Legacy / STL Recommended Alternative Benefits / Optimization Points
Containers std::unordered_map absl::flat_hash_map Better memory usage and throughput; Abseil’s Swiss table is cache-friendly and often significantly faster than std::unordered_map.(Abseil)
Strings std::stringstream / + absl::StrCat / absl::StrJoin Reduces allocations by pre-computing lengths and avoids temporary string creation, leading to faster concatenation.(php.cn)
Synchronization Primitives std::mutex absl::Mutex Abseil’s Mutex can have better uncontended performance and richer features (e.g., deadlock detection and advanced waiting conditions).(AI Coding Community)
Async / Future std::future folly::Future / folly::SemiFuture Supports continuation chaining (.then()), executors, and more expressive async composition than the basic C++ std::future.(DeepWiki)
Memory / Large Strings std::vector (or naive buffers) absl::Cord Efficient handling of large/fragmented strings or data streams, minimizes copying, and can chain data segments.(AI Coding Community)
Error Handling bool / int status codes absl::StatusOr Strongly typed status with return value; forces error checking and makes failure handling safer.(AI Coding Community)
Concurrent Queue std::queue + std::mutex folly::MPMCQueue Lock-free or low-overhead multi-producer, multi-consumer queue; better throughput under high concurrency.(php.cn)

Alternatives Considered

No response</issue_description>

<agent_instructions>Task: Refactor the following Apollo C++ code to use Abseil and Folly for better performance.
Rules:

Replace std::unordered_map with absl::flat_hash_map.

Replace string concatenations with absl::StrCat.

Replace std::mutex with absl::Mutex and use absl::WriterMutexLock.

Ensure C++17 compatibility (e.g., use std::string_view where appropriate).

Avoid manual memory management; use absl::StatusOr for function returns that might fail.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

- Replace std::unordered_map with absl::flat_hash_map in scheduler headers
- Replace std::mutex with absl::Mutex in scheduler and thread_safe_queue
- Use absl::WriterMutexLock for exclusive lock guards
- Replace std::condition_variable with absl::Mutex::Await() in ThreadSafeQueue
- Replace string .append() chains with absl::StrAppend() in scheduler
- Use absl::StrCat for string construction in scheduler_factory
- Add ABSL_GUARDED_BY annotations for thread safety documentation
- Update BUILD files with absl dependencies
- Update scheduler test to use absl::flat_hash_map

Co-authored-by: daohu527 <10419854+daohu527@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor Apollo C++ code to use Abseil and Folly for performance Refactor scheduler and base queue to use Abseil primitives Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Refactor the following Apollo C++ code to use Abseil and Folly for better performance.

2 participants