Skip to content

Commit b2871bb

Browse files
authored
ARROW-17823: [C++] Prefer std::make_shared/std::make_unique over constructor with new (#14216)
Advantage: readabilty, exception safety and efficiency(only for shared_ptr). Cases that don't apply: When calling a private/protected constructor within class member function, make_shared/unique can't work. Authored-by: Jin Shang <[email protected]> Signed-off-by: Sutou Kouhei <[email protected]>
1 parent 356e7f8 commit b2871bb

51 files changed

Lines changed: 165 additions & 186 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cpp/examples/arrow/compute_register_example.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class ExampleFunctionOptions : public cp::FunctionOptions {
5757

5858
std::unique_ptr<cp::FunctionOptions> ExampleFunctionOptionsType::Copy(
5959
const cp::FunctionOptions&) const {
60-
return std::unique_ptr<cp::FunctionOptions>(new ExampleFunctionOptions());
60+
return std::make_unique<ExampleFunctionOptions>();
6161
}
6262

6363
arrow::Status ExampleFunctionImpl(cp::KernelContext* ctx, const cp::ExecSpan& batch,

‎cpp/src/arrow/compute/exec.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ Result<std::unique_ptr<KernelExecutor>> MakeExecutor(ExecContext* ctx,
10971097
const FunctionOptions* options) {
10981098
DCHECK_EQ(ExecutorType::function_kind, func->kind());
10991099
auto typed_func = checked_cast<const FunctionType*>(func);
1100-
return std::unique_ptr<KernelExecutor>(new ExecutorType(ctx, typed_func, options));
1100+
return std::make_unique<ExecutorType>(ctx, typed_func, options);
11011101
}
11021102

11031103
} // namespace

‎cpp/src/arrow/compute/exec/options.cc‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ Result<std::shared_ptr<SourceNodeOptions>> SourceNodeOptions::FromTable(
5959
// Map the RecordBatchReader to a SourceNode
6060
ARROW_ASSIGN_OR_RAISE(auto batch_gen, MakeReaderGenerator(std::move(reader), exc));
6161

62-
return std::shared_ptr<SourceNodeOptions>(
63-
new SourceNodeOptions(table.schema(), batch_gen));
62+
return std::make_shared<SourceNodeOptions>(table.schema(), batch_gen);
6463
}
6564

6665
} // namespace compute

‎cpp/src/arrow/compute/exec/tpch_node.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3548,7 +3548,7 @@ Result<std::unique_ptr<TpchGen>> TpchGen::Make(ExecPlan* plan, double scale_fact
35483548
int64_t batch_size,
35493549
std::optional<int64_t> seed) {
35503550
if (!seed.has_value()) seed = GetRandomSeed();
3551-
return std::unique_ptr<TpchGen>(new TpchGenImpl(plan, scale_factor, batch_size, *seed));
3551+
return std::make_unique<TpchGenImpl>(plan, scale_factor, batch_size, *seed);
35523552
}
35533553

35543554
} // namespace internal

‎cpp/src/arrow/compute/exec_test.cc‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ TEST_F(TestExecSpanIterator, ChunkedArrays) {
765765
}
766766

767767
TEST_F(TestExecSpanIterator, ZeroLengthInputs) {
768-
auto carr = std::shared_ptr<ChunkedArray>(new ChunkedArray({}, int32()));
768+
auto carr = std::make_shared<ChunkedArray>(ArrayVector{}, int32());
769769

770770
auto CheckArgs = [&](const ExecBatch& batch) {
771771
ExecSpanIterator iterator;
@@ -896,7 +896,7 @@ struct ExampleState : public KernelState {
896896
Result<std::unique_ptr<KernelState>> InitStateful(KernelContext*,
897897
const KernelInitArgs& args) {
898898
auto func_options = static_cast<const ExampleOptions*>(args.options);
899-
return std::unique_ptr<KernelState>(new ExampleState{func_options->value});
899+
return std::make_unique<ExampleState>(func_options->value);
900900
}
901901

902902
Status ExecStateful(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
@@ -1063,8 +1063,8 @@ TEST_F(TestCallScalarFunction, PreallocationCases) {
10631063

10641064
// Input is chunked, output has one big chunk
10651065
{
1066-
auto carr = std::shared_ptr<ChunkedArray>(
1067-
new ChunkedArray({arr->Slice(0, 10), arr->Slice(10)}));
1066+
auto carr =
1067+
std::make_shared<ChunkedArray>(ArrayVector{arr->Slice(0, 10), arr->Slice(10)});
10681068
std::vector<Datum> args = {Datum(carr)};
10691069
ASSERT_OK_AND_ASSIGN(Datum result, CallFunction(func_name, args, exec_ctx_.get()));
10701070
std::shared_ptr<ChunkedArray> actual = result.chunked_array();

‎cpp/src/arrow/compute/function_internal.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,13 +647,13 @@ const FunctionOptionsType* GetFunctionOptionsType(const Properties&... propertie
647647
}
648648
Result<std::unique_ptr<FunctionOptions>> FromStructScalar(
649649
const StructScalar& scalar) const override {
650-
auto options = std::unique_ptr<Options>(new Options());
650+
auto options = std::make_unique<Options>();
651651
RETURN_NOT_OK(
652652
FromStructScalarImpl<Options>(options.get(), scalar, properties_).status_);
653653
return std::move(options);
654654
}
655655
std::unique_ptr<FunctionOptions> Copy(const FunctionOptions& options) const override {
656-
auto out = std::unique_ptr<Options>(new Options());
656+
auto out = std::make_unique<Options>();
657657
CopyImpl<Options>(out.get(), checked_cast<const Options&>(options), properties_);
658658
return std::move(out);
659659
}

‎cpp/src/arrow/dataset/partition.cc‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -696,14 +696,12 @@ class FilenamePartitioningFactory : public KeyValuePartitioningFactory {
696696

697697
std::shared_ptr<PartitioningFactory> DirectoryPartitioning::MakeFactory(
698698
std::vector<std::string> field_names, PartitioningFactoryOptions options) {
699-
return std::shared_ptr<PartitioningFactory>(
700-
new DirectoryPartitioningFactory(std::move(field_names), options));
699+
return std::make_shared<DirectoryPartitioningFactory>(std::move(field_names), options);
701700
}
702701

703702
std::shared_ptr<PartitioningFactory> FilenamePartitioning::MakeFactory(
704703
std::vector<std::string> field_names, PartitioningFactoryOptions options) {
705-
return std::shared_ptr<PartitioningFactory>(
706-
new FilenamePartitioningFactory(std::move(field_names), options));
704+
return std::make_shared<FilenamePartitioningFactory>(std::move(field_names), options);
707705
}
708706

709707
bool FilenamePartitioning::Equals(const Partitioning& other) const {
@@ -851,7 +849,7 @@ class HivePartitioningFactory : public KeyValuePartitioningFactory {
851849

852850
std::shared_ptr<PartitioningFactory> HivePartitioning::MakeFactory(
853851
HivePartitioningFactoryOptions options) {
854-
return std::shared_ptr<PartitioningFactory>(new HivePartitioningFactory(options));
852+
return std::make_shared<HivePartitioningFactory>(options);
855853
}
856854

857855
std::string StripPrefix(const std::string& path, const std::string& prefix) {

‎cpp/src/arrow/filesystem/mockfs.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ class MockFileSystem::Impl {
431431
MockFileSystem::~MockFileSystem() = default;
432432

433433
MockFileSystem::MockFileSystem(TimePoint current_time, const io::IOContext& io_context) {
434-
impl_ = std::unique_ptr<Impl>(new Impl(current_time, io_context.pool()));
434+
impl_ = std::make_unique<Impl>(current_time, io_context.pool());
435435
}
436436

437437
bool MockFileSystem::Equals(const FileSystem& other) const { return this == &other; }

‎cpp/src/arrow/flight/client_cookie_middleware.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ClientCookieMiddlewareFactory : public ClientMiddlewareFactory {
2727
public:
2828
void StartCall(const CallInfo& info, std::unique_ptr<ClientMiddleware>* middleware) {
2929
ARROW_UNUSED(info);
30-
*middleware = std::unique_ptr<ClientMiddleware>(new ClientCookieMiddleware(*this));
30+
*middleware = std::make_unique<ClientCookieMiddleware>(*this);
3131
}
3232

3333
private:

‎cpp/src/arrow/flight/flight_internals_test.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ TEST(FlightTypes, RoundTripTypes) {
148148
std::vector<FlightEndpoint> endpoints{FlightEndpoint{ticket, {location1, location2}},
149149
FlightEndpoint{ticket, {location3}}};
150150
ASSERT_OK(MakeFlightInfo(*schema, desc, endpoints, -1, -1, &data));
151-
std::unique_ptr<FlightInfo> info = std::unique_ptr<FlightInfo>(new FlightInfo(data));
151+
auto info = std::make_unique<FlightInfo>(data);
152152
ASSERT_OK_AND_ASSIGN(std::string info_serialized, info->SerializeToString());
153153
ASSERT_OK_AND_ASSIGN(std::unique_ptr<FlightInfo> info_deserialized,
154154
FlightInfo::Deserialize(info_serialized));

0 commit comments

Comments
 (0)