Skip to content

Conversation

tongke6
Copy link
Member

@tongke6 tongke6 commented Sep 11, 2025

No description provided.

Copy link

@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.

Summary of Changes

Hello @tongke6, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request establishes a foundational interface for inter-party communication within the yacl::link module. By introducing the Mbox interface and an in-memory implementation, it provides a flexible and testable mechanism for exchanging messages between different participants. This enhancement is crucial for building and verifying distributed protocols, offering a clear abstraction layer for communication channels.

Highlights

  • New Mbox Interface: Introduced a new virtual interface, yacl::link::Mbox, for abstracting multi-party communication, defining Send and Recv methods.
  • In-Memory Mbox Implementation: Provided an InMemoryMbox class, which is a concrete implementation of the Mbox interface, designed for testing and local development scenarios.
  • Mbox Factory: Added a factory function, CreateInMemoryMboxs, to easily create multiple interconnected InMemoryMbox instances for a given world_size.
  • Unit Tests: Included comprehensive unit tests for InMemoryMbox covering basic send/receive, multiple messages, timeout handling, and concurrent operations to ensure correctness.
  • Bazel Build Updates: Updated the Bazel build configuration to include the new mbox and mbox_factory libraries and their corresponding tests, along with a minor update to MODULE.bazel.lock.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@tongke6 tongke6 requested a review from FollyCoolly September 11, 2025 08:25
Copy link

@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

This pull request introduces a new Mbox interface for multi-party communication, along with an in-memory implementation for testing purposes. The overall design is sound. My review focuses on improving performance, code hygiene, and style consistency. I've suggested using absl::StrCat for more efficient string construction, which requires a small build file modification. I've also pointed out some unused header includes and recommended adding missing newlines at the end of the new files for better style compliance.

Comment on lines 159 to 161
deps = [
":mbox",
],

Choose a reason for hiding this comment

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

medium

To support the suggested use of absl::StrCat for more efficient string building in mbox_factory.cc, the absl/strings dependency should be added here.

    deps = [
        ":mbox",
        "@abseil-cpp//absl/strings",
    ],

yacl/link/mbox.h Outdated
int64_t timeout_ms) = 0;
};

} // namespace yacl::link No newline at end of file

Choose a reason for hiding this comment

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

medium

It is a common convention and good practice to end files with a newline character. Some tools and compilers may issue a warning if it's missing.

Suggested change
} // namespace yacl::link
} // namespace yacl::link


#include <chrono>
#include <exception>
#include <future>

Choose a reason for hiding this comment

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

medium

This header is included but not used in this file. Removing it can improve compilation speed and reduce unnecessary dependencies.

#include <map>
#include <mutex>
#include <optional>
#include <shared_mutex>

Choose a reason for hiding this comment

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

medium

This header is included but not used in this file. Removing it can improve compilation speed and reduce unnecessary dependencies.

Comment on lines 42 to 43
std::string full_key =
"/frm/" + std::to_string(rank_) + "/msg/" + std::string(key);

Choose a reason for hiding this comment

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

medium

This string concatenation creates multiple temporary std::string objects, which is inefficient. Using absl::StrCat is a more performant way to concatenate strings as it avoids intermediate allocations.

To use absl::StrCat, you'll also need to:

  1. Add #include "absl/strings/str_cat.h" to this file.
  2. Add the @abseil-cpp//absl/strings dependency in yacl/link/BUILD.bazel (I've added a separate comment for this).
Suggested change
std::string full_key =
"/frm/" + std::to_string(rank_) + "/msg/" + std::string(key);
std::string full_key = absl::StrCat("/frm/", rank_, "/msg/", key);

Comment on lines 60 to 61
std::string full_key =
"/frm/" + std::to_string(src) + "/msg/" + std::string(key);

Choose a reason for hiding this comment

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

medium

Similar to the Send method, this string concatenation is inefficient. It's recommended to use absl::StrCat for better performance.

Suggested change
std::string full_key =
"/frm/" + std::to_string(src) + "/msg/" + std::string(key);
std::string full_key = absl::StrCat("/frm/", src, "/msg/", key);

return result;
}

} // namespace yacl::link No newline at end of file

Choose a reason for hiding this comment

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

medium

It is a common convention and good practice to end files with a newline character. Some tools and compilers may issue a warning if it's missing.

Suggested change
} // namespace yacl::link
} // namespace yacl::link

// from 0 to world_size - 1.
std::vector<std::shared_ptr<Mbox>> CreateInMemoryMboxs(size_t world_size);

} // namespace yacl::link No newline at end of file

Choose a reason for hiding this comment

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

medium

It is a common convention and good practice to end files with a newline character. Some tools and compilers may issue a warning if it's missing.

Suggested change
} // namespace yacl::link
} // namespace yacl::link

receiver.join();
}

} // namespace yacl::link No newline at end of file

Choose a reason for hiding this comment

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

medium

It is a common convention and good practice to end files with a newline character. Some tools and compilers may issue a warning if it's missing.

Suggested change
} // namespace yacl::link
} // namespace yacl::link


std::optional<std::vector<uint8_t>> result = std::nullopt;

auto stop_wait = [&] {

Choose a reason for hiding this comment

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

可以使用 extract:

auto stop_wait = [&] {
auto& mailbox = shared_state_->mailboxes[rank_];
if (auto node = mailbox.extract(full_key); !node.empty()) {
result = std::move(node.mapped());
return true;
}
return false;
};

EXPECT_FALSE(received.has_value());
}

TEST(InMemoryMbox, ConcurrentSendRecv) {

Choose a reason for hiding this comment

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

看起来Concurrent不是重点,BlockingRecvThenSend 如何?

std::optional<std::vector<uint8_t>> Recv(size_t src, std::string_view key,
int64_t timeout_ms) override;

public:
Copy link
Collaborator

Choose a reason for hiding this comment

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

multiple "public"s in a class

@tongke6 tongke6 marked this pull request as draft September 12, 2025 06:38
@tongke6 tongke6 changed the title Feat: Introduce Mbox interface for multiple parties communication Feat: Introduce Mbox C API for multiple parties communication Sep 15, 2025
@tongke6 tongke6 force-pushed the tk/mailbox branch 2 times, most recently from 5baf9df to 36ca71d Compare September 15, 2025 07:00
@tongke6 tongke6 marked this pull request as ready for review September 15, 2025 07:01
/// @param data_len Length of data in bytes.
///
/// @return MBOX_SUCCESS on success, appropriate error code on failure.
mbox_error_t mbox_send(mbox_t* mbox, size_t dst, const char* key,

Choose a reason for hiding this comment

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

这个API有个隐藏语义

send到底是放到 自己的mbox 就success了,还是到达对方的 mbox 才算success

理论上我理解放到 自己的mbox就算成功了,这样对异步比较友好,但也有两个问题

  1. sender 如何知道 mbox 之间的发送错误,需要新的接口吗?
  2. mobx 是否本身也要 flush 接口,Sender 异步send完毕后结束进程的问题

Copy link
Member Author

Choose a reason for hiding this comment

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

目前想的是放到自己的发送队列里就算成功。

flush 接口可以按需加。
mbox 后续会增加stats 接口,统计发送成功的,正在发送的,以及发送失败的等统计信息,以及接受信息。

Copy link

Stale pull request message. Please comment to remove stale tag. Otherwise this pr will be closed soon.

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.

4 participants