-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Replace shared_ptr with unique_ptr for better performance #695
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
Conversation
|
@Snape3058, thanks for bringing it to our attention. The change looks good to me except using |
|
C++14 is removed, I split this patch into two commits, one for backporting and one for replacing. The replacing commit can also be applied to the cpp17 branch directly, as far as I am thinking. Maybe you want the backported |
|
@Snape3058, thanks for the change. I reviewed the code.
I would like to use
In the cpp17 branch, you don't have to do anything because it's just an experimental branch. I found two things when reviewing the code:
Lines 1286 to 1288 in fffbf1a
|
I prefer
Thank you for reminding me about the problem. The original shim was copied from gcc's libstdc++, which may probably conflict with your MIT Lisence. Therefore, I replace it with another implementation from stack overflow, which is a widely used implementation in a lot of open source projects.
According to my investigation, the macro |
|
Maybe testing the C++ standard is no longer required after changing The original purpose of testing C++ standard is to prevent the conflict of |
|
@Snape3058, looks very good! Thanks for your contribution! |
* Backport std::make_unique from C++14. * Replace shared_ptr with unique_ptr for better performance. Co-authored-by: Ella <maxutong16@otcaix.iscas.ac.cn>
According to the bug reports of my smart pointer analysis tool, you have a 'unique shared_ptr' problem. As the
shared_ptruses more resources than theunique_ptr, if the ownership is not semantically shared among multipleshared_ptrs, theunique_ptris preferred for better performance.Although my tool did not find all the problems, I tried my best to understand your code and fix all the appearances of the problem. This patch fixes the problem by replacing the related
shared_ptrwithunique_ptrand usingstd::make_uniqueandstd::moveto create and transfer theunique_ptrrespectively. As the functionstd::make_uniquewas introduced in C++14, I also changed the C++ standard to C++14 in the first commit.The problems are caused by
Result::res_andClient::cli_. As these two member variables only initialized by their corresponding constructors and they are not used as ashared_ptrin the class. Usingunique_ptrinstead ofshared_ptrfor managing these non-shared objects could be much better.I have no ideas about whether the objects are required to be accessed through a getter method in the future. But, you can simply add a getter method returning a raw pointer with the
unique_ptr::get()method, when the getters are needed and the ownership is not shared.Here are the three bug reports generated by my smart pointer analysis tool.
Sorry for renaming
httplib.hasanalyze.cppduring checking.