Skip to content

Conversation

@santigimeno
Copy link
Member

@santigimeno santigimeno commented May 29, 2025

Enable AsyncTSQueue to accept both single-element (T&&, const T&) and batch (std::vector&&, const std::vector&) callbacks, using SFINAE traits for compile-time detection and dispatch. Ensure correct argument forwarding and update tests to cover all supported callback signatures.

Summary by CodeRabbit

  • New Features
    • Enhanced queue processing to support both batch and single-item callbacks, automatically selecting the appropriate style.
  • Bug Fixes
    • Improved type safety and flexibility for callback handling in queue processing.
  • Tests
    • Added new tests to verify correct behavior for batch callbacks with various parameter types and additional arguments.

@santigimeno santigimeno self-assigned this May 29, 2025
@coderabbitai
Copy link

coderabbitai bot commented May 29, 2025

Walkthrough

The AsyncTSQueue class was refactored to support both batch and single-item processing callbacks using compile-time detection, replacing the previous single-item-only callback mechanism. The constructor and process method were updated accordingly, and new tests were added to verify batch callback behavior with various signatures and argument types.

Changes

File(s) Change Summary
src/nsolid/async_ts_queue.h Refactored AsyncTSQueue to unify and detect batch vs single-item callbacks at compile time.
test/cctest/test_nsolid_async_ts_queue.cc Added tests for batch callbacks with different vector parameter types and extra context args.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant AsyncTSQueue
    participant Callback

    User->>AsyncTSQueue: enqueue(items)
    User->>AsyncTSQueue: process()
    alt Batch callback detected
        AsyncTSQueue->>Callback: operator()(std::vector<T>&& batch[, extra_args])
    else Single-item callback detected
        loop for each item
            AsyncTSQueue->>Callback: operator()(T&& item[, extra_args])
        end
    end
Loading

Poem

In the queue where tasks abound,
Batch or single, callbacks found!
With compile-time smarts, we choose the way,
To process all, or one per play.
Tests now check both styles anew—
Hooray for queues that flex and do!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 06e86bf and 02c9003.

📒 Files selected for processing (1)
  • src/nsolid/async_ts_queue.h (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/nsolid/async_ts_queue.h
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: test-linux (ubuntu-24.04)
  • GitHub Check: test-linux (ubuntu-24.04-arm)
  • GitHub Check: lint-js-and-md
  • GitHub Check: coverage-linux
  • GitHub Check: coverage-linux-without-intl
  • GitHub Check: build-tarball
  • GitHub Check: test-macOS
  • GitHub Check: coverage-windows
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@santigimeno santigimeno force-pushed the santi/improve_async_ts_queue branch from 9455b8d to 7ca4eed Compare May 29, 2025 14:14
@santigimeno santigimeno changed the title src: add batched support callbeck in AsyncTSQueue src: add batched support callback in AsyncTSQueue May 29, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
src/nsolid/async_ts_queue.h (1)

87-99: Trait set misses const std::vector<T>&& and const T&& cases

Although unlikely in practice, users could legitimately provide callbacks that take const std::vector<T>&& or const T&&. Those signatures currently fail SFINAE detection and trigger the static-assert even though they are perfectly sound.

-      std::is_invocable<Cb, std::vector<T>&&, Extra...>,
-      std::is_invocable<Cb, const std::vector<T>&, Extra...>
+      std::is_invocable<Cb, std::vector<T>&&, Extra...>,
+      std::is_invocable<Cb, const std::vector<T>&, Extra...>,
+      std::is_invocable<Cb, const std::vector<T>&&, Extra...>

and likewise for the is_single_callback branch.

Adding the missing qualifiers costs virtually nothing and avoids surprising compile-time errors for users that choose to keep immutability even when moving.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d4cd851 and 89c979e.

📒 Files selected for processing (2)
  • src/nsolid/async_ts_queue.h (3 hunks)
  • test/cctest/test_nsolid_async_ts_queue.cc (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
test/cctest/test_nsolid_async_ts_queue.cc (1)
src/nsolid/async_ts_queue.h (6)
  • create (39-44)
  • create (39-39)
  • item (59-63)
  • item (59-59)
  • item (71-75)
  • item (71-71)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: test-macOS
  • GitHub Check: coverage-linux
  • GitHub Check: test-linux (ubuntu-24.04)
  • GitHub Check: test-linux (ubuntu-24.04-arm)
  • GitHub Check: coverage-linux-without-intl
  • GitHub Check: build-tarball
  • GitHub Check: coverage-windows
  • GitHub Check: lint-js-and-md
  • GitHub Check: build-docs
🔇 Additional comments (3)
test/cctest/test_nsolid_async_ts_queue.cc (3)

237-256: Great coverage for std::vector<T>&& batch path

The test validates rvalue batch handling and ensures the callback is invoked only once—exactly the edge that could regress.


258-276: 👍 Extra-arg batch test adds real-world scenario

Very useful to demonstrate that forwarding of additional bound parameters still works with batch mode.


279-295: Const-reference batch variant covered

The const-ref path is less common but important for read-only consumers. Test looks solid.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
src/nsolid/async_ts_queue.h (2)

109-112: Perfect-forward the extra arguments inside the bound lambda

args are captured by value; when they are later passed to std::invoke they are always forwarded as l-values, which can:

  • Prevent move-only parameters from being moved into the callback.
  • Produce unnecessary copies for heavy objects.

Forward them again to preserve their value category:

-      std::invoke(cb, std::forward<decltype(first)>(first), args...);
+      std::invoke(cb,
+                  std::forward<decltype(first)>(first),
+                  std::forward<Args>(args)...);

(You’ll need to add Args to the lambda’s template parameter list, e.g. turn it into a generic lambda with template<typename... Args2> or wrap the extras in a tuple and std::apply.)


115-125: Reserve vector capacity to avoid repeated reallocations

When batching, the vector grows one element at a time. If thousands of items are pending, that leads to O(n) reallocations.

After the first successful dequeue you know the queue’s approximate size; pre-allocate to at least that:

-          std::vector<T> batch;
-          batch.push_back(std::move(item));
+          std::vector<T> batch;
+          batch.reserve(queue_.approx_size() + 1);  // or a sensible heuristic
+          batch.push_back(std::move(item));

(Assumes TSQueue exposes an approx_size()/size() getter; if not, consider adding one.)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 89c979e and 06e86bf.

📒 Files selected for processing (1)
  • src/nsolid/async_ts_queue.h (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: lint-js-and-md
  • GitHub Check: coverage-linux
  • GitHub Check: build-docs
  • GitHub Check: test-linux (ubuntu-24.04-arm)
  • GitHub Check: test-linux (ubuntu-24.04)
  • GitHub Check: coverage-windows
  • GitHub Check: test-macOS
  • GitHub Check: coverage-linux-without-intl
  • GitHub Check: build-tarball

Copy link
Contributor

@juanarbol juanarbol left a comment

Choose a reason for hiding this comment

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

This is very nice!

Enable AsyncTSQueue to accept both single-element (T&&, const T&) and
batch (std::vector<T>&&, const std::vector<T>&) callbacks, using
if constexpr with type traits for compile-time dispatch. Ensure correct
argument forwarding and update tests to cover all supported callback
signatures.
@santigimeno santigimeno force-pushed the santi/improve_async_ts_queue branch from 02c9003 to 9cc7a5d Compare June 5, 2025 17:47
santigimeno added a commit that referenced this pull request Jun 5, 2025
Enable AsyncTSQueue to accept both single-element (T&&, const T&) and
batch (std::vector<T>&&, const std::vector<T>&) callbacks, using
if constexpr with type traits for compile-time dispatch. Ensure correct
argument forwarding and update tests to cover all supported callback
signatures.

PR-URL: #312
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
@santigimeno
Copy link
Member Author

Landed in 4ba885f

@santigimeno santigimeno closed this Jun 5, 2025
@santigimeno santigimeno deleted the santi/improve_async_ts_queue branch June 5, 2025 21:28
santigimeno added a commit that referenced this pull request Aug 25, 2025
Enable AsyncTSQueue to accept both single-element (T&&, const T&) and
batch (std::vector<T>&&, const std::vector<T>&) callbacks, using
if constexpr with type traits for compile-time dispatch. Ensure correct
argument forwarding and update tests to cover all supported callback
signatures.

PR-URL: #312
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
santigimeno added a commit that referenced this pull request Aug 26, 2025
Enable AsyncTSQueue to accept both single-element (T&&, const T&) and
batch (std::vector<T>&&, const std::vector<T>&) callbacks, using
if constexpr with type traits for compile-time dispatch. Ensure correct
argument forwarding and update tests to cover all supported callback
signatures.

PR-URL: #312
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
PR-URL: #359
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
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.

3 participants