Skip to content

Prefer lambda expressions over std::bind#17090

Merged
yashykt merged 2 commits intogrpc:masterfrom
yashykt:minor_cleanup1
Nov 5, 2018
Merged

Prefer lambda expressions over std::bind#17090
yashykt merged 2 commits intogrpc:masterfrom
yashykt:minor_cleanup1

Conversation

@yashykt
Copy link
Copy Markdown
Member

@yashykt yashykt commented Nov 2, 2018

Prefer lambda expressions over std::bind.
Fixes #17048

@yashykt yashykt requested a review from hcaseyal November 2, 2018 22:49
@yashykt yashykt added lang/c++ release notes: no Indicates if PR should not be in release notes labels Nov 2, 2018
@grpc-testing
Copy link
Copy Markdown

****************************************************************

libgrpc.so

     VM SIZE        FILE SIZE
 ++++++++++++++  ++++++++++++++

  [ = ]       0        0  [ = ]


****************************************************************

libgrpc++.so

     VM SIZE                                                                                            FILE SIZE
 ++++++++++++++ GROWING                                                                              ++++++++++++++
  +1.5%    +725 src/cpp/server/server_cc.cc                                                             +725  +1.5%
      +109%    +465 grpc::ServerAsyncReaderWriter<grpc::ByteBuffer, grpc::ByteBuffer>::WriteAndFinish       +465  +109%
       +40%    +452 grpc::Server::UnimplementedAsyncResponse::UnimplementedAsyncResponse                    +452   +40%
      [NEW]    +200 void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >    +200  [NEW]
       +17%    +198 grpc::Server::~Server                                                                   +198   +17%
      +8.5%    +153 grpc::Server::SyncRequestThreadManager::DoWork                                          +153  +8.5%
      [NEW]     +46 std::_Function_base::_Base_manager<grpc::ServerInterface::BaseAsyncRequest::Finalize     +46  [NEW]
      [NEW]     +46 std::_Function_base::_Base_manager<grpc::Server::SyncRequest::CallData::Run(std::sha     +46  [NEW]
      +1.8%     +12 [Unmapped]                                                                               +12  +1.8%
      [NEW]      +8 std::_Function_handler<void (), grpc::ServerInterface::BaseAsyncRequest::FinalizeRes      +8  [NEW]
      [NEW]      +8 std::_Function_handler<void (), grpc::Server::SyncRequest::CallData::Run(std::shared      +8  [NEW]
  +3.9%    +295 src/cpp/client/secure_credentials.cc                                                    +295  +3.9%
      [NEW]    +200 void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >    +200  [NEW]
       +22%    +138 grpc::MetadataCredentialsPluginWrapper::GetMetadata                                     +138   +22%
      [NEW]    +137 std::_Function_base::_Base_manager<grpc::MetadataCredentialsPluginWrapper::GetMetada    +137  [NEW]
      [NEW]    +137 std::_Function_base::_Base_manager<grpc::MetadataCredentialsPluginWrapper::GetMetada    +137  [NEW]
      [NEW]     +50 std::_Function_handler<void (), grpc::MetadataCredentialsPluginWrapper::GetMetadata(     +50  [NEW]
      [NEW]     +50 std::_Function_handler<void (), grpc::MetadataCredentialsPluginWrapper::GetMetadata(     +50  [NEW]
       +18%     +30 [Unmapped]                                                                               +30   +18%

 -------------- SHRINKING                                                                            --------------
  -2.1% -6.48Ki [None]                                                                                -138Ki  -1.7%
      -2.0% -5.60Ki [Unmapped]                                                                            -137Ki  -1.7%
      [DEL]    -169 typeinfo name for std::_Bind<void (grpc::MetadataCredentialsPluginWrapper::*(grpc::M    -169  [DEL]
      [DEL]    -164 typeinfo name for std::_Weak_result_type_impl<void (grpc::MetadataCredentialsPluginW    -164  [DEL]
      [DEL]    -159 typeinfo name for std::_Weak_result_type<void (grpc::MetadataCredentialsPluginWrappe    -159  [DEL]
      [DEL]    -151 typeinfo name for std::_Bind<void (grpc::AuthMetadataProcessorAyncWrapper::*(grpc::A    -151  [DEL]
      [DEL]    -150 typeinfo name for std::_Weak_result_type_impl<void (grpc::AuthMetadataProcessorAyncW    -150  [DEL]
      [DEL]    -145 typeinfo name for std::_Weak_result_type<void (grpc::AuthMetadataProcessorAyncWrappe    -145  [DEL]
      [DEL]     -88 [Other]                                                                                  -88  [DEL]
      [DEL]     -76 typeinfo name for std::_Weak_result_type_impl<void (grpc::ServerInterface::BaseAsync     -76  [DEL]
      [DEL]     -71 typeinfo name for std::_Weak_result_type<void (grpc::ServerInterface::BaseAsyncReque     -71  [DEL]
      [DEL]     -70 typeinfo name for std::_Weak_result_type_impl<void (grpc::Server::SyncRequest::CallD     -70  [DEL]
      [DEL]     -65 typeinfo name for std::_Weak_result_type<void (grpc::Server::SyncRequest::CallData::     -65  [DEL]
      [DEL]     -64 typeinfo name for std::_Bind<void (grpc::ServerInterface::BaseAsyncRequest::*(grpc::     -64  [DEL]
      [DEL]     -58 typeinfo name for std::_Bind<void (grpc::Server::SyncRequest::CallData::*(grpc::Serv     -58  [DEL]
      [DEL]     -24 typeinfo for std::_Weak_result_type<void (grpc::ServerInterface::BaseAsyncRequest::*     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Weak_result_type<void (grpc::AuthMetadataProcessorAyncWrapper::*)     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Weak_result_type<void (grpc::MetadataCredentialsPluginWrapper::*)     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Weak_result_type<void (grpc::Server::SyncRequest::CallData::*)()>     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Bind<void (grpc::ServerInterface::BaseAsyncRequest::*(grpc::Serve     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Bind<void (grpc::AuthMetadataProcessorAyncWrapper::*(grpc::AuthMe     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Bind<void (grpc::MetadataCredentialsPluginWrapper::*(grpc::Metada     -24  [DEL]
  -0.2%     -11 src/cpp/server/secure_server_credentials.cc                                              -11  -0.2%
      [DEL]    -137 std::_Function_base::_Base_manager<std::_Bind<void (grpc::AuthMetadataProcessorAyncW    -137  [DEL]
      [DEL]     -48 std::_Function_handler<void (), std::_Bind<void (grpc::AuthMetadataProcessorAyncWrap     -48  [DEL]
      -3.8%     -16 grpc::AuthMetadataProcessorAyncWrapper::Process                                          -16  -3.8%

  -1.0% -5.50Ki TOTAL                                                                                 -137Ki  -1.7%



@grpc-testing
Copy link
Copy Markdown

****************************************************************

libgrpc.so

     VM SIZE        FILE SIZE
 ++++++++++++++  ++++++++++++++

  [ = ]       0        0  [ = ]


****************************************************************

libgrpc++.so

     VM SIZE                                                                                            FILE SIZE
 ++++++++++++++ GROWING                                                                              ++++++++++++++
  +1.5%    +725 src/cpp/server/server_cc.cc                                                             +725  +1.5%
      +109%    +465 grpc::ServerAsyncReaderWriter<grpc::ByteBuffer, grpc::ByteBuffer>::WriteAndFinish       +465  +109%
       +40%    +452 grpc::Server::UnimplementedAsyncResponse::UnimplementedAsyncResponse                    +452   +40%
      [NEW]    +200 void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >    +200  [NEW]
       +17%    +198 grpc::Server::~Server                                                                   +198   +17%
      +8.5%    +153 grpc::Server::SyncRequestThreadManager::DoWork                                          +153  +8.5%
      [NEW]     +46 std::_Function_base::_Base_manager<grpc::ServerInterface::BaseAsyncRequest::Finalize     +46  [NEW]
      [NEW]     +46 std::_Function_base::_Base_manager<grpc::Server::SyncRequest::CallData::Run(std::sha     +46  [NEW]
      +1.8%     +12 [Unmapped]                                                                               +12  +1.8%
      [NEW]      +8 std::_Function_handler<void (), grpc::ServerInterface::BaseAsyncRequest::FinalizeRes      +8  [NEW]
      [NEW]      +8 std::_Function_handler<void (), grpc::Server::SyncRequest::CallData::Run(std::shared      +8  [NEW]

 -------------- SHRINKING                                                                            --------------
  -2.2% -6.81Ki [None]                                                                                -143Ki  -1.8%
      -2.1% -5.77Ki [Unmapped]                                                                            -142Ki  -1.8%
      [DEL]    -169 typeinfo name for std::_Bind<void (grpc::MetadataCredentialsPluginWrapper::*(grpc::M    -169  [DEL]
      [DEL]    -164 typeinfo name for std::_Weak_result_type_impl<void (grpc::MetadataCredentialsPluginW    -164  [DEL]
      [DEL]    -159 typeinfo name for std::_Weak_result_type<void (grpc::MetadataCredentialsPluginWrappe    -159  [DEL]
      [DEL]    -151 typeinfo name for std::_Bind<void (grpc::AuthMetadataProcessorAyncWrapper::*(grpc::A    -151  [DEL]
      [DEL]    -150 typeinfo name for std::_Weak_result_type_impl<void (grpc::AuthMetadataProcessorAyncW    -150  [DEL]
      [DEL]    -145 typeinfo name for std::_Weak_result_type<void (grpc::AuthMetadataProcessorAyncWrappe    -145  [DEL]
      [DEL]     -88 [Other]                                                                                  -88  [DEL]
      [DEL]     -76 typeinfo name for std::_Weak_result_type_impl<void (grpc::ServerInterface::BaseAsync     -76  [DEL]
      [DEL]     -71 typeinfo name for std::_Weak_result_type<void (grpc::ServerInterface::BaseAsyncReque     -71  [DEL]
      [DEL]     -70 typeinfo name for std::_Weak_result_type_impl<void (grpc::Server::SyncRequest::CallD     -70  [DEL]
      [DEL]     -65 typeinfo name for std::_Weak_result_type<void (grpc::Server::SyncRequest::CallData::     -65  [DEL]
      [DEL]     -64 typeinfo name for std::_Bind<void (grpc::ServerInterface::BaseAsyncRequest::*(grpc::     -64  [DEL]
      [DEL]     -58 typeinfo name for std::_Bind<void (grpc::Server::SyncRequest::CallData::*(grpc::Serv     -58  [DEL]
      [DEL]     -24 typeinfo for std::_Weak_result_type<void (grpc::ServerInterface::BaseAsyncRequest::*     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Weak_result_type<void (grpc::AuthMetadataProcessorAyncWrapper::*)     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Weak_result_type<void (grpc::MetadataCredentialsPluginWrapper::*)     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Weak_result_type<void (grpc::Server::SyncRequest::CallData::*)()>     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Bind<void (grpc::ServerInterface::BaseAsyncRequest::*(grpc::Serve     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Bind<void (grpc::AuthMetadataProcessorAyncWrapper::*(grpc::AuthMe     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Bind<void (grpc::MetadataCredentialsPluginWrapper::*(grpc::Metada     -24  [DEL]
  -1.2%     -89 src/cpp/client/secure_credentials.cc                                                     -89  -1.2%
      [DEL]    -200 void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >    -200  [DEL]
      [DEL]    -169 std::_Function_base::_Base_manager<std::_Bind<void (grpc::MetadataCredentialsPluginW    -169  [DEL]
      [DEL]     -78 std::_Function_handler<void (), std::_Bind<void (grpc::MetadataCredentialsPluginWrap     -78  [DEL]
      -7.6%     -48 grpc::MetadataCredentialsPluginWrapper::GetMetadata                                      -48  -7.6%
  -0.2%     -11 src/cpp/server/secure_server_credentials.cc                                              -11  -0.2%
      [DEL]    -137 std::_Function_base::_Base_manager<std::_Bind<void (grpc::AuthMetadataProcessorAyncW    -137  [DEL]
      [DEL]     -48 std::_Function_handler<void (), std::_Bind<void (grpc::AuthMetadataProcessorAyncWrap     -48  [DEL]
      -3.8%     -16 grpc::AuthMetadataProcessorAyncWrapper::Process                                          -16  -3.8%

  -1.2% -6.20Ki TOTAL                                                                                 -142Ki  -1.7%



@grpc-testing
Copy link
Copy Markdown

[trickle] No significant performance differences

1 similar comment
@grpc-testing
Copy link
Copy Markdown

[trickle] No significant performance differences

@grpc-testing
Copy link
Copy Markdown

Objective-C binary sizes
*****************STATIC******************
  New size                      Old size
 2,016,505      Total (=)      2,016,505

 No significant differences in binary sizes

***************FRAMEWORKS****************
  New size                      Old size
11,123,084      Total (<)     11,123,086

 No significant differences in binary sizes


@grpc-testing
Copy link
Copy Markdown

Objective-C binary sizes
*****************STATIC******************
  New size                      Old size
 2,016,505      Total (=)      2,016,505

 No significant differences in binary sizes

***************FRAMEWORKS****************
  New size                      Old size
11,123,081      Total (>)     11,123,078

 No significant differences in binary sizes


@grpc-testing
Copy link
Copy Markdown

Corrupt JSON data (indicates timeout or crash): 
    bm_call_create.BM_IsolatedFilter_ClientChannelFilter_NoOp_.counters.new: 10
    bm_call_create.BM_IsolatedFilter_ClientChannelFilter_NoOp_.counters.old: 10


[microbenchmarks] No significant performance differences

1 similar comment
@grpc-testing
Copy link
Copy Markdown

Corrupt JSON data (indicates timeout or crash): 
    bm_call_create.BM_IsolatedFilter_ClientChannelFilter_NoOp_.counters.new: 10
    bm_call_create.BM_IsolatedFilter_ClientChannelFilter_NoOp_.counters.old: 10


[microbenchmarks] No significant performance differences

Copy link
Copy Markdown
Contributor

@hcaseyal hcaseyal left a comment

Choose a reason for hiding this comment

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

LGTM

w->thread_pool_->Add(
std::bind(&MetadataCredentialsPluginWrapper::InvokePlugin, w, context,
cb, user_data, nullptr, nullptr, nullptr, nullptr));
w->thread_pool_->Add([=]() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The empty () are unnecessary and can be left out.

Suggested change
w->thread_pool_->Add([=]() {
w->thread_pool_->Add([=] {

Same thing below.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree with this comment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

But see my overall request about anonymous captures.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done!

Copy link
Copy Markdown
Contributor

@vjpai vjpai left a comment

Choose a reason for hiding this comment

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

IMMEDIATE LBTM.
We cannot use anonymous lambda captures.

@hcaseyal
Copy link
Copy Markdown
Contributor

hcaseyal commented Nov 5, 2018

@vjpai Why can't we use anonymous lambda captures?

@vjpai
Copy link
Copy Markdown
Contributor

vjpai commented Nov 5, 2018

Anonymous lambda captures are error-prone and imprecise; although people say that about auto, this is a worse situation since these can easily lead to dangling pointer bugs when some new variable comes along. I think we've allowed in maybe 1 or 2 into our whole codebase and that was where there really was no other option because of some other issue. The Bible no longer forbids them but also strongly doesn't prefer them.

@grpc-testing
Copy link
Copy Markdown

****************************************************************

libgrpc.so

     VM SIZE        FILE SIZE
 ++++++++++++++  ++++++++++++++

  [ = ]       0        0  [ = ]


****************************************************************

libgrpc++.so

     VM SIZE                                                                                            FILE SIZE
 ++++++++++++++ GROWING                                                                              ++++++++++++++
  +1.5%    +725 src/cpp/server/server_cc.cc                                                             +725  +1.5%
      +109%    +465 grpc::ServerAsyncReaderWriter<grpc::ByteBuffer, grpc::ByteBuffer>::WriteAndFinish       +465  +109%
       +40%    +452 grpc::Server::UnimplementedAsyncResponse::UnimplementedAsyncResponse                    +452   +40%
      [NEW]    +200 void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >    +200  [NEW]
       +17%    +198 grpc::Server::~Server                                                                   +198   +17%
      +8.5%    +153 grpc::Server::SyncRequestThreadManager::DoWork                                          +153  +8.5%
      [NEW]     +46 std::_Function_base::_Base_manager<grpc::ServerInterface::BaseAsyncRequest::Finalize     +46  [NEW]
      [NEW]     +46 std::_Function_base::_Base_manager<grpc::Server::SyncRequest::CallData::Run(std::sha     +46  [NEW]
      +1.8%     +12 [Unmapped]                                                                               +12  +1.8%
      [NEW]      +8 std::_Function_handler<void (), grpc::ServerInterface::BaseAsyncRequest::FinalizeRes      +8  [NEW]
      [NEW]      +8 std::_Function_handler<void (), grpc::Server::SyncRequest::CallData::Run(std::shared      +8  [NEW]

 -------------- SHRINKING                                                                            --------------
  -2.2% -6.81Ki [None]                                                                                -143Ki  -1.8%
      -2.1% -5.77Ki [Unmapped]                                                                            -142Ki  -1.8%
      [DEL]    -169 typeinfo name for std::_Bind<void (grpc::MetadataCredentialsPluginWrapper::*(grpc::M    -169  [DEL]
      [DEL]    -164 typeinfo name for std::_Weak_result_type_impl<void (grpc::MetadataCredentialsPluginW    -164  [DEL]
      [DEL]    -159 typeinfo name for std::_Weak_result_type<void (grpc::MetadataCredentialsPluginWrappe    -159  [DEL]
      [DEL]    -151 typeinfo name for std::_Bind<void (grpc::AuthMetadataProcessorAyncWrapper::*(grpc::A    -151  [DEL]
      [DEL]    -150 typeinfo name for std::_Weak_result_type_impl<void (grpc::AuthMetadataProcessorAyncW    -150  [DEL]
      [DEL]    -145 typeinfo name for std::_Weak_result_type<void (grpc::AuthMetadataProcessorAyncWrappe    -145  [DEL]
      [DEL]     -88 [Other]                                                                                  -88  [DEL]
      [DEL]     -76 typeinfo name for std::_Weak_result_type_impl<void (grpc::ServerInterface::BaseAsync     -76  [DEL]
      [DEL]     -71 typeinfo name for std::_Weak_result_type<void (grpc::ServerInterface::BaseAsyncReque     -71  [DEL]
      [DEL]     -70 typeinfo name for std::_Weak_result_type_impl<void (grpc::Server::SyncRequest::CallD     -70  [DEL]
      [DEL]     -65 typeinfo name for std::_Weak_result_type<void (grpc::Server::SyncRequest::CallData::     -65  [DEL]
      [DEL]     -64 typeinfo name for std::_Bind<void (grpc::ServerInterface::BaseAsyncRequest::*(grpc::     -64  [DEL]
      [DEL]     -58 typeinfo name for std::_Bind<void (grpc::Server::SyncRequest::CallData::*(grpc::Serv     -58  [DEL]
      [DEL]     -24 typeinfo for std::_Weak_result_type<void (grpc::ServerInterface::BaseAsyncRequest::*     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Weak_result_type<void (grpc::AuthMetadataProcessorAyncWrapper::*)     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Weak_result_type<void (grpc::MetadataCredentialsPluginWrapper::*)     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Weak_result_type<void (grpc::Server::SyncRequest::CallData::*)()>     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Bind<void (grpc::ServerInterface::BaseAsyncRequest::*(grpc::Serve     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Bind<void (grpc::AuthMetadataProcessorAyncWrapper::*(grpc::AuthMe     -24  [DEL]
      [DEL]     -24 typeinfo for std::_Bind<void (grpc::MetadataCredentialsPluginWrapper::*(grpc::Metada     -24  [DEL]
  -1.2%     -89 src/cpp/client/secure_credentials.cc                                                     -89  -1.2%
      [DEL]    -200 void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >    -200  [DEL]
      [DEL]    -169 std::_Function_base::_Base_manager<std::_Bind<void (grpc::MetadataCredentialsPluginW    -169  [DEL]
      [DEL]     -78 std::_Function_handler<void (), std::_Bind<void (grpc::MetadataCredentialsPluginWrap     -78  [DEL]
      -7.6%     -48 grpc::MetadataCredentialsPluginWrapper::GetMetadata                                      -48  -7.6%
  -0.2%     -11 src/cpp/server/secure_server_credentials.cc                                              -11  -0.2%
      [DEL]    -137 std::_Function_base::_Base_manager<std::_Bind<void (grpc::AuthMetadataProcessorAyncW    -137  [DEL]
      [DEL]     -48 std::_Function_handler<void (), std::_Bind<void (grpc::AuthMetadataProcessorAyncWrap     -48  [DEL]
      -3.8%     -16 grpc::AuthMetadataProcessorAyncWrapper::Process                                          -16  -3.8%

  -1.2% -6.20Ki TOTAL                                                                                 -142Ki  -1.7%



@grpc-testing
Copy link
Copy Markdown

[trickle] No significant performance differences

@grpc-testing
Copy link
Copy Markdown

Objective-C binary sizes
*****************STATIC******************
  New size                      Old size
 2,020,616      Total (=)      2,020,616

 No significant differences in binary sizes

***************FRAMEWORKS****************
  New size                      Old size
11,180,941      Total (>)     11,180,936

 No significant differences in binary sizes


@grpc-testing
Copy link
Copy Markdown

Corrupt JSON data (indicates timeout or crash): 
    bm_call_create.BM_IsolatedFilter_ClientChannelFilter_NoOp_.counters.new: 10
    bm_call_create.BM_IsolatedFilter_ClientChannelFilter_NoOp_.counters.old: 10


[microbenchmarks] No significant performance differences

@yashykt
Copy link
Copy Markdown
Member Author

yashykt commented Nov 5, 2018

All green! Thanks for reviewing!

@yashykt yashykt merged commit 673887d into grpc:master Nov 5, 2018
@hcaseyal
Copy link
Copy Markdown
Contributor

hcaseyal commented Nov 5, 2018

@vjpai Thanks for the explanation! Makes sense.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 3, 2019
@yashykt yashykt deleted the minor_cleanup1 branch May 18, 2023 20:33
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

lang/c++ release notes: no Indicates if PR should not be in release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants