bkietz commented on code in PR #36915:
URL: https://github.com/apache/arrow/pull/36915#discussion_r1287323046


##########
cpp/src/arrow/type.cc:
##########
@@ -2090,17 +2090,39 @@ Status SchemaBuilder::AreCompatible(const 
std::vector<std::shared_ptr<Schema>>&
   return Merge(schemas, policy).status();
 }
 
+std::vector<std::shared_ptr<Field>> make_fields(
+    std::initializer_list<std::pair<std::string, std::shared_ptr<DataType>>> 
init_list) {
+  std::vector<std::shared_ptr<Field>> fields;
+  fields.reserve(init_list.size());
+  for (const auto& [name, type] : init_list) {
+    fields.push_back(field(name, type));
+  }

Review Comment:
   The elements of an initializer list are always const, so calling move() on 
them won't accomplish anything. We could elide the copy by making the 
initializer lists accept string_view instead of string, though: 
   ```
   std::vector<std::shared_ptr<Field>> MakeFields(
       std::initializer_list<std::pair<std::string_view, 
std::shared_ptr<DataType>>> init_list) {
     for (const auto& [name, type] : init_list) {
       fields.push_back(field(std::string{name}, type));
     }
   }
   ```
   
   However I've seldom seen a field name longer than 22 chars, so they'd be 
inlined in most STL impls.
   
   From a perf golf standpoint, the larger annoyance is the type, which must be 
copied (since we don't have a nice implicitly constructible non-owning DataType 
holder) and which hits atomics when copied. If you wanted to get around *that* 
you'd need something like:
   ```
   struct DataTypeView {
     // NOLINTNEXTLINE
     DataTypeView(const std::shared_ptr<DataType>& type) : type{type} {}
     // NOLINTNEXTLINE
     operator std::shared_ptr<DataType>() const { return type; }
     const std::shared_ptr<DataType>& type;
   };
   std::vector<std::shared_ptr<Field>> MakeFields(
       std::initializer_list<std::pair<std::string_view, DataTypeView>> 
init_list) {
     for (const auto& [name, type] : init_list) {
       fields.push_back(field(std::string{name}, type));
     }
   }
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to