Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(258)

Issue 1076953002: Add move support for scoped_refptr. (Closed)

Created:
5 years, 8 months ago by Kibeom Kim (inactive)
Modified:
5 years, 8 months ago
Reviewers:
danakj, dcheng
CC:
Bernhard Bauer, chromium-reviews, danduong, erikwright+watch_chromium.org, gavinp+memory_chromium.org, Jeffrey Yasskin, Nico, trchen, Ryan Sleevi
Base URL:
https://chromium.googlesource.com/chromium/src.git@master
Target Ref:
refs/pending/heads/master
Project:
chromium
Visibility:
Public.

Description

Add move support for scoped_refptr. scoped_ptr has - move constructor. - move assignment operator. - .Pass(). This CL adds the same support for scoped_refptr. The main benefit is reducing unnecessary reference counter updates. // Example 1 class Foo { // Without .Pass(), a copy constructor would be called, which // increments and decrements the reference counter unnecessarily. // With .Pass(), a move constructor is called which doesn't update // the reference counter Foo(scoped_refptr<T> t) : t_(t.Pass()) {} const scoped_refptr<T> t_; }; // Example 2 scoped_refptr<Foo> func() { return scoped_refptr<Foo>(new Foo()); } main() { scoped_refptr<Foo> foo; // The following would be done by a copy assignment operator before, // but now a move assignment operator will be used instead. foo = func(); } // Example 3 class Foo : public base::RefCountedThreadSafe<Foo> { ... } void func(scoped_refptr<Foo> foo) { ... } main() { scoped_refptr<Foo> foo(...); // Because it's moved, the reference counter won't be updated. massage_loop->PostTask(FROM_HERE, base::Bind(&func, base::Passed(foo))); } Committed: https://crrev.com/aa3927982b7cb0032db3f06d89d1c0cfd6479a8b Cr-Commit-Position: refs/heads/master@{#324910}

Patch Set 1 #

Patch Set 2 : #

Total comments: 3

Patch Set 3 : Fixed self-move assignment and added more tests. #

Patch Set 4 : fix compiler error due to comparing different pointer types. #

Patch Set 5 : Used swap(...) to implement move assignment operator instead. #

Patch Set 6 : Update move assignment operator to match 20.7.2.2.3 #

Total comments: 4

Patch Set 7 : Added T != U tests and unrolled |scoped_refptr<T>(r.Pass()).swap(*this);| #

Patch Set 8 : add |override| for |virtual ~ScopedRefPtrCountDerived|. #

Total comments: 2

Patch Set 9 : reverted to |scoped_refptr<T>(r.Pass()).swap(*this);| and added a test case for ScopedRefPtrToSelf #

Total comments: 2

Patch Set 10 : add comment for check->self_ptr_ = scoped_refptr<ScopedRefPtrToSelf>(); test #

Unified diffs Side-by-side diffs Delta from patch set Stats (+377 lines, -5 lines) Patch
M base/memory/ref_counted.h View 1 2 3 4 5 6 7 8 5 chunks +20 lines, -0 lines 0 comments Download
M base/memory/ref_counted_unittest.cc View 1 2 3 4 5 6 7 8 9 3 chunks +352 lines, -5 lines 0 comments Download
M base/move.h View 1 chunk +5 lines, -0 lines 0 comments Download

Messages

Total messages: 43 (11 generated)
Kibeom Kim (inactive)
5 years, 8 months ago (2015-04-09 23:37:25 UTC) #2
Ryan Sleevi
Drive by: Case 1 won't cause churn because of RVO Case 2 is working as ...
5 years, 8 months ago (2015-04-09 23:47:50 UTC) #4
dcheng
CCing some other people who might have thoughts. Also, we should see how far away ...
5 years, 8 months ago (2015-04-09 23:50:07 UTC) #6
Matt Giuca
Not a fan of this change. Having move semantics on a refcounted pointer doesn't make ...
5 years, 8 months ago (2015-04-10 00:02:10 UTC) #7
Kibeom Kim (inactive)
On 2015/04/10 00:02:10, Matt Giuca wrote: > Not a fan of this change. Having move ...
5 years, 8 months ago (2015-04-10 00:09:14 UTC) #8
Kibeom Kim (inactive)
btw, std::shared_ptr seems to have move constructor and move assignment operator. 20.7.2.2.1 template<class Y> shared_ptr(const ...
5 years, 8 months ago (2015-04-10 00:17:45 UTC) #10
Ryan Sleevi
On 2015/04/10 00:09:14, Kibeom Kim wrote: > I could be wrong, but I thought that ...
5 years, 8 months ago (2015-04-10 00:19:02 UTC) #11
dcheng
On 2015/04/10 at 00:02:10, mgiuca wrote: > Not a fan of this change. Having move ...
5 years, 8 months ago (2015-04-10 00:19:04 UTC) #12
jamesr
On 2015/04/10 00:19:04, dcheng wrote: > The flip side to this argument is Blink, which ...
5 years, 8 months ago (2015-04-10 00:22:34 UTC) #13
Matt Giuca
Yeah, I just ran an experiment with Clang 3.4 and GCC 4.8.2 (haven't looked at ...
5 years, 8 months ago (2015-04-10 00:29:21 UTC) #14
danakj
On Thu, Apr 9, 2015 at 5:02 PM, <mgiuca@chromium.org> wrote: > Not a fan of ...
5 years, 8 months ago (2015-04-10 00:44:16 UTC) #15
Kibeom Kim (inactive)
ptal. revised example 2. https://codereview.chromium.org/1076953002/diff/20001/base/memory/ref_counted.h File base/memory/ref_counted.h (right): https://codereview.chromium.org/1076953002/diff/20001/base/memory/ref_counted.h#newcode280 base/memory/ref_counted.h:280: scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) ...
5 years, 8 months ago (2015-04-10 02:42:07 UTC) #16
commit-bot: I haz the power
Dry run: CQ is trying da patch. Follow status at https://chromium-cq-status.appspot.com/patch-status/1076953002/40001
5 years, 8 months ago (2015-04-10 02:43:07 UTC) #18
commit-bot: I haz the power
Dry run: CQ is trying da patch. Follow status at https://chromium-cq-status.appspot.com/patch-status/1076953002/60001
5 years, 8 months ago (2015-04-10 02:56:57 UTC) #20
commit-bot: I haz the power
Try jobs failed on following builders: chromium_presubmit on tryserver.chromium.linux (JOB_FAILED, http://build.chromium.org/p/tryserver.chromium.linux/builders/chromium_presubmit/builds/55377)
5 years, 8 months ago (2015-04-10 03:15:34 UTC) #22
commit-bot: I haz the power
Dry run: CQ is trying da patch. Follow status at https://chromium-cq-status.appspot.com/patch-status/1076953002/80001
5 years, 8 months ago (2015-04-10 05:52:09 UTC) #24
commit-bot: I haz the power
Try jobs failed on following builders: chromium_presubmit on tryserver.chromium.linux (JOB_FAILED, http://build.chromium.org/p/tryserver.chromium.linux/builders/chromium_presubmit/builds/55399)
5 years, 8 months ago (2015-04-10 06:01:02 UTC) #26
danakj
You might also mention a third example class Foo { Foo(scoped_refptr<T> t) : t_(t.Pass()) {} ...
5 years, 8 months ago (2015-04-10 19:44:24 UTC) #27
danakj
https://codereview.chromium.org/1076953002/diff/100001/base/memory/ref_counted_unittest.cc File base/memory/ref_counted_unittest.cc (right): https://codereview.chromium.org/1076953002/diff/100001/base/memory/ref_counted_unittest.cc#newcode319 base/memory/ref_counted_unittest.cc:319: ScopedRefPtrCount *raw = new ScopedRefPtrCount(); Can you add some ...
5 years, 8 months ago (2015-04-10 20:14:32 UTC) #28
Kibeom Kim (inactive)
On 2015/04/10 19:44:24, danakj wrote: > You might also mention a third example > > ...
5 years, 8 months ago (2015-04-10 20:32:15 UTC) #29
danakj
On Fri, Apr 10, 2015 at 1:32 PM, <kkimlabs@chromium.org> wrote: > On 2015/04/10 19:44:24, danakj ...
5 years, 8 months ago (2015-04-10 20:34:03 UTC) #30
Kibeom Kim (inactive)
I noticed that I also need to take care of base/win/scoped_comptr.h too. I'll fix it ...
5 years, 8 months ago (2015-04-12 07:35:14 UTC) #31
dcheng
https://codereview.chromium.org/1076953002/diff/140001/base/memory/ref_counted.h File base/memory/ref_counted.h (right): https://codereview.chromium.org/1076953002/diff/140001/base/memory/ref_counted.h#newcode343 base/memory/ref_counted.h:343: ptr_ = p; I think this version is not ...
5 years, 8 months ago (2015-04-13 03:45:38 UTC) #32
Kibeom Kim (inactive)
https://codereview.chromium.org/1076953002/diff/140001/base/memory/ref_counted.h File base/memory/ref_counted.h (right): https://codereview.chromium.org/1076953002/diff/140001/base/memory/ref_counted.h#newcode343 base/memory/ref_counted.h:343: ptr_ = p; On 2015/04/13 03:45:38, dcheng wrote: > ...
5 years, 8 months ago (2015-04-13 09:57:53 UTC) #33
danakj
Thanks for the additional test kkimlabs, and thanks for pointing out the self-reference dcheng :) ...
5 years, 8 months ago (2015-04-13 16:13:23 UTC) #34
dcheng
Should we update the guidance on passing scoped_refptr<T>? When should one use const scoped_refptr<T>& vs ...
5 years, 8 months ago (2015-04-13 17:20:45 UTC) #35
danakj
I think we should send a PSA once this has landed and stuck, and introduce ...
5 years, 8 months ago (2015-04-13 17:34:19 UTC) #36
Kibeom Kim (inactive)
https://codereview.chromium.org/1076953002/diff/160001/base/memory/ref_counted_unittest.cc File base/memory/ref_counted_unittest.cc (right): https://codereview.chromium.org/1076953002/diff/160001/base/memory/ref_counted_unittest.cc#newcode135 base/memory/ref_counted_unittest.cc:135: check->self_ptr_ = scoped_refptr<ScopedRefPtrToSelf>(); On 2015/04/13 16:13:23, danakj (away until ...
5 years, 8 months ago (2015-04-13 18:37:59 UTC) #37
commit-bot: I haz the power
CQ is trying da patch. Follow status at https://chromium-cq-status.appspot.com/patch-status/1076953002/180001
5 years, 8 months ago (2015-04-13 18:39:20 UTC) #40
commit-bot: I haz the power
Committed patchset #10 (id:180001)
5 years, 8 months ago (2015-04-13 20:49:53 UTC) #41
commit-bot: I haz the power
Patchset 10 (id:??) landed as https://crrev.com/aa3927982b7cb0032db3f06d89d1c0cfd6479a8b Cr-Commit-Position: refs/heads/master@{#324910}
5 years, 8 months ago (2015-04-13 20:50:44 UTC) #42
Kibeom Kim (inactive)
5 years, 8 months ago (2015-04-14 01:37:36 UTC) #43
Message was sent while issue was closed.
On 2015/04/13 17:20:45, dcheng wrote:
> Should we update the guidance on passing scoped_refptr<T>? When should one use
> const scoped_refptr<T>& vs scoped_refptr<T> + Pass()?

I don't see any reason to copy when moving is an option. The moved
scoped_refptr<> will become nullptr, and I think one should always move when
that side effect is OK in that context.


On 2015/04/13 17:34:19, danakj (away until april 19) wrote:
> I think we should send a PSA once this has landed and stuck, and introduce
> people to the concept of using Pass() scoped_refptr (explaining its passing
> ownership of the single reference, and maybe give examples comparing it to
> PassRefPtr/RefPtr in blink).
> 
> We can let people know that const scoped_refptr<T>& still works for passing
> things around. But that passing scoped_refptr<T> by value and using Pass() can
> lead to better performance, or expression of intent.

Yeah I'll write a PSA shortly.

Powered by Google App Engine
This is Rietveld 408576698