New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request to expand MessageDereferencer implementation with hash computation of the message #2066
Comments
It's not a very good idea to use proto message as keys because they are not true value types. With the presence of unknown fields, two equal messages in one binary might be treated as different by another, and therefore two different binaries might get two different unordered_map<> even given the exactly same input data (if the hash function is implemented using MessageDifferencer). It's probably better to write your own hash function that has your desired behavior rather than replying on a generic one. |
@xfxyjwf One concern I have before implementing that About the feature, before implementing it myself, would you look through pull request with this feature if I provide one, or should I instead fork As for the feature's usage scenario consider this simplified subscription model below (I use similar one in my app). If you have any ideas how to effectively implement this model in different way (without
|
@slavanap Thanks for the detailed explanation. If the proto message is simple enough (doesn't involve unknown fields etc), a generic hash function should work fine. If you would like to contribute an implementation, can you first create one with only the public interface? We can start there and once we agree upon the public interface of this feature you can add the actual implementation. |
@xfxyjwf sorry for the delay, lots of work. After a more deep consideration I suggest the interface to look something like this. #include <google/protobuf/message.h>
#include <google/protobuf/stubs/hash.h>
GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_START
template<>
struct hash<::google::protobuf::Message> {
size_t operator()(const ::google::protobuf::Message& m) const;
};
GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_END This code may be placed either in In protobuf, you guys already have some hash implementations (here for example), so for hash computation we just need to iterate through message fields (probably only 1-2 highest levels) and call already implemented ADDEDInterestingly to notice, you already implemented the same functionality, but for C# here. |
@xfxyjwf I don't mind to implement this feature by myself, but my pull request code will need a review. |
Assume I have some generated protobuf message
class MyMessage
in C++.Assume I want to use
std::unordered_map<MyMessage, int> map;
I can not use it, because I need to provide
std::hash<MyMessage>
implementation.As for
std::equal_to<MyMessage>
, it can be easily supported viaMessageDereferencer
static methodBut new utility
MessageDereferencer
class lacks API for hash computation of the message. Thusstd::hash<google::protobuf::Message>
still has to use descriptor and reflection of the message to (recursively) compute hash of the message.Thus this issue is a request for such functionality that can be widely used across C++ or request for suggestions how to properly instantiate
unordered_set<MyMessage>
.The text was updated successfully, but these errors were encountered: