You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "hwse (via GitHub)" <gi...@apache.org> on 2024/07/25 13:25:33 UTC
[PR] AVRO-3984 [C++] Improve code generated for unions [avro]
hwse opened a new pull request, #3047:
URL: https://github.com/apache/avro/pull/3047
This merge request should improve the code generated by avrogencpp for union types. The improvements are:
## Getter return a reference
Previously the getters for a union branch returned a value:
std::map<std::string, float > get_map() const;
This lead to a complete copy of the map, which may be really expensive depending on the use case.
In this merge request the getter return a reference instead, which allows the user to choose if a copy should be done.
const std::map<std::string, float >& get_map() const;
TODO: push and describe remaining improvements
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@avro.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] AVRO-3984 [C++] Improve code generated for unions [avro]
Posted by "hwse (via GitHub)" <gi...@apache.org>.
hwse commented on code in PR #3047:
URL: https://github.com/apache/avro/pull/3047#discussion_r1693255581
##########
lang/c++/impl/avrogencpp.cc:
##########
@@ -376,8 +392,32 @@ string CodeGen::generateUnionType(const NodePtr &n) {
<< "private:\n"
<< " size_t idx_;\n"
<< " std::any value_;\n"
- << "public:\n"
- << " size_t idx() const { return idx_; }\n";
+ << "public:\n";
+
+ os_ << " /** enum representing union branches as returned by the idx() function */\n"
+ << " enum class Branch: size_t {\n";
+
+ // generate a enum that maps the branch name to the corresponding index (as returned by idx())
+ std::set<std::string> used_branch_names;
+ for (size_t i = 0; i < c; ++i) {
+ // escape reserved literals for c++
+ auto branch_name = decorate(names[i]);
+ // avoid rare collisions, e.g. somone might name their struct int_
+ if (used_branch_names.find(branch_name) != used_branch_names.end()) {
+ size_t postfix = 2;
+ std::string escaped_name = branch_name + "_" + std::to_string(postfix);
+ while (used_branch_names.find(escaped_name) != used_branch_names.end()) {
+ ++postfix;
+ escaped_name = branch_name + "_" + std::to_string(postfix);
+ }
+ branch_name = escaped_name;
+ }
+ os_ << " " << branch_name << " = " << i << ",\n";
+ used_branch_names.insert(branch_name);
+ }
+ os_ << " };\n";
+
+ os_ << " size_t idx() const { return idx_; }\n";
Review Comment:
Good point, i added the method. Thanks for the quick feedback!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] AVRO-3984 [C++] Improve code generated for unions [avro]
Posted by "Gerrit0 (via GitHub)" <gi...@apache.org>.
Gerrit0 commented on code in PR #3047:
URL: https://github.com/apache/avro/pull/3047#discussion_r1693073953
##########
lang/c++/impl/avrogencpp.cc:
##########
@@ -376,8 +392,32 @@ string CodeGen::generateUnionType(const NodePtr &n) {
<< "private:\n"
<< " size_t idx_;\n"
<< " std::any value_;\n"
- << "public:\n"
- << " size_t idx() const { return idx_; }\n";
+ << "public:\n";
+
+ os_ << " /** enum representing union branches as returned by the idx() function */\n"
+ << " enum class Branch: size_t {\n";
+
+ // generate a enum that maps the branch name to the corresponding index (as returned by idx())
+ std::set<std::string> used_branch_names;
+ for (size_t i = 0; i < c; ++i) {
+ // escape reserved literals for c++
+ auto branch_name = decorate(names[i]);
+ // avoid rare collisions, e.g. somone might name their struct int_
+ if (used_branch_names.find(branch_name) != used_branch_names.end()) {
+ size_t postfix = 2;
+ std::string escaped_name = branch_name + "_" + std::to_string(postfix);
+ while (used_branch_names.find(escaped_name) != used_branch_names.end()) {
+ ++postfix;
+ escaped_name = branch_name + "_" + std::to_string(postfix);
+ }
+ branch_name = escaped_name;
+ }
+ os_ << " " << branch_name << " = " << i << ",\n";
+ used_branch_names.insert(branch_name);
+ }
+ os_ << " };\n";
+
+ os_ << " size_t idx() const { return idx_; }\n";
Review Comment:
Do you think it is worth including a `branch` method which returns `static_cast<Branch>(idx_)`? In code I have that uses Avro I always end up doing this anyways to switch over the enum for branches.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] AVRO-3984 [C++] Improve code generated for unions [avro]
Posted by "hwse (via GitHub)" <gi...@apache.org>.
hwse commented on PR #3047:
URL: https://github.com/apache/avro/pull/3047#issuecomment-2298666360
@mkmkme @Gerrit0 There was no activity on this pull request lately, is there anything that i still need to do?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] AVRO-3984 [C++] Improve code generated for unions [avro]
Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on PR #3047:
URL: https://github.com/apache/avro/pull/3047#issuecomment-2301256691
Thank you, @hwse !
And @mkmkme & @Gerrit0 for the reviews!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] AVRO-3984 [C++] Improve code generated for unions [avro]
Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g merged PR #3047:
URL: https://github.com/apache/avro/pull/3047
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@avro.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] AVRO-3984 [C++] Improve code generated for unions [avro]
Posted by "Gerrit0 (via GitHub)" <gi...@apache.org>.
Gerrit0 commented on PR #3047:
URL: https://github.com/apache/avro/pull/3047#issuecomment-2252870876
> i would not consider the new enum a real feature
I would! It means I can get rid of the enums that are manually maintained alongside the avro file today. I think the release note is sufficient documentation though, anyone new to using avro will already end up looking at the generated header and see it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] AVRO-3984 [C++] Improve code generated for unions [avro]
Posted by "hwse (via GitHub)" <gi...@apache.org>.
hwse commented on code in PR #3047:
URL: https://github.com/apache/avro/pull/3047#discussion_r1693255581
##########
lang/c++/impl/avrogencpp.cc:
##########
@@ -376,8 +392,32 @@ string CodeGen::generateUnionType(const NodePtr &n) {
<< "private:\n"
<< " size_t idx_;\n"
<< " std::any value_;\n"
- << "public:\n"
- << " size_t idx() const { return idx_; }\n";
+ << "public:\n";
+
+ os_ << " /** enum representing union branches as returned by the idx() function */\n"
+ << " enum class Branch: size_t {\n";
+
+ // generate a enum that maps the branch name to the corresponding index (as returned by idx())
+ std::set<std::string> used_branch_names;
+ for (size_t i = 0; i < c; ++i) {
+ // escape reserved literals for c++
+ auto branch_name = decorate(names[i]);
+ // avoid rare collisions, e.g. somone might name their struct int_
+ if (used_branch_names.find(branch_name) != used_branch_names.end()) {
+ size_t postfix = 2;
+ std::string escaped_name = branch_name + "_" + std::to_string(postfix);
+ while (used_branch_names.find(escaped_name) != used_branch_names.end()) {
+ ++postfix;
+ escaped_name = branch_name + "_" + std::to_string(postfix);
+ }
+ branch_name = escaped_name;
+ }
+ os_ << " " << branch_name << " = " << i << ",\n";
+ used_branch_names.insert(branch_name);
+ }
+ os_ << " };\n";
+
+ os_ << " size_t idx() const { return idx_; }\n";
Review Comment:
Good point, i added the method.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] AVRO-3984 [C++] Improve code generated for unions [avro]
Posted by "mkmkme (via GitHub)" <gi...@apache.org>.
mkmkme commented on PR #3047:
URL: https://github.com/apache/avro/pull/3047#issuecomment-2252167056
> @mkmkme @Gerrit0
Sorry I'm on a holiday right now and will be available from Monday
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] AVRO-3984 [C++] Improve code generated for unions [avro]
Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on PR #3047:
URL: https://github.com/apache/avro/pull/3047#issuecomment-2252165578
@mkmkme @Gerrit0
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org