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

Issue 6094005: Create "Prebind" a wrapper to tr1::bind. (Closed)

Created:
9 years, 12 months ago by awong
Modified:
9 years, 7 months ago
CC:
chromium-reviews, brettw-cc_chromium.org, cbentzel+watch_chromium.org, Paweł Hajdan Jr., darin-cc_chromium.org, rdsmith+dwatch_chromium.org
Visibility:
Public.

Description

Create "Prebind" a wrapper to tr1::bind. BUG= TEST=

Patch Set 1 #

Patch Set 2 : Closure example ported to Prebinds #

Total comments: 10

Patch Set 3 : Remove closure.h and ThunkState #

Total comments: 2
Unified diffs Side-by-side diffs Delta from patch set Stats (+548 lines, -52 lines) Patch
M base/base.gypi View 1 2 chunks +2 lines, -0 lines 0 comments Download
M base/message_loop.h View 1 2 2 chunks +10 lines, -0 lines 0 comments Download
M base/message_loop.cc View 1 2 2 chunks +33 lines, -0 lines 0 comments Download
A base/prebind.h View 1 2 1 chunk +402 lines, -0 lines 2 comments Download
M base/task.cc View 1 1 chunk +6 lines, -0 lines 0 comments Download
M base/worker_pool.h View 1 2 2 chunks +26 lines, -0 lines 0 comments Download
M chrome/browser/download/download_manager_unittest.cc View 1 4 chunks +9 lines, -8 lines 0 comments Download
M net/base/host_resolver_impl.cc View 1 2 5 chunks +12 lines, -13 lines 0 comments Download
M net/socket/client_socket_pool_base.h View 1 10 chunks +30 lines, -11 lines 0 comments Download
M net/socket/client_socket_pool_base.cc View 1 2 8 chunks +18 lines, -20 lines 0 comments Download

Messages

Total messages: 8 (0 generated)
awong
Start of an attempt to wrap bind.
9 years, 12 months ago (2010-12-30 03:07:12 UTC) #1
awong
I finished out wrapping tr1::function/bind with base::Thunk/Prebind. Then I threw it on top of the ...
9 years, 12 months ago (2010-12-30 09:46:07 UTC) #2
willchan no longer on Chromium
I haven't reviewed things in detail, but I think we can be considerably more efficient ...
9 years, 11 months ago (2011-01-05 00:30:16 UTC) #3
willchan no longer on Chromium
I need to review this some more, but I have to head out. Here are ...
9 years, 11 months ago (2011-01-05 00:59:01 UTC) #4
awong
I've removed ThunkState in favor of using a higher-order function + a scoped_refptr to handle ...
9 years, 11 months ago (2011-01-05 03:17:41 UTC) #5
willchan no longer on Chromium
I'm primarily concerned with the extra AddRef()/Release() pairs. http://codereview.chromium.org/6094005/diff/3001/base/closure.h File base/closure.h (right): http://codereview.chromium.org/6094005/diff/3001/base/closure.h#newcode99 base/closure.h:99: static ...
9 years, 11 months ago (2011-01-05 19:44:09 UTC) #6
awong
On 2011/01/05 19:44:09, willchan wrote: > I'm primarily concerned with the extra AddRef()/Release() pairs. > ...
9 years, 11 months ago (2011-01-06 03:37:03 UTC) #7
awong
9 years, 11 months ago (2011-01-06 03:38:26 UTC) #8
On 2011/01/05 19:44:09, willchan wrote:
> I'm primarily concerned with the extra AddRef()/Release() pairs.
> 
> http://codereview.chromium.org/6094005/diff/3001/base/closure.h
> File base/closure.h (right):
> 
> http://codereview.chromium.org/6094005/diff/3001/base/closure.h#newcode99
> base/closure.h:99: static ::std::tr1::function<void()> RefThunk(O* o) {
> On 2011/01/05 03:17:42, awong wrote:
> > On 2011/01/05 00:30:17, willchan wrote:
> > > Do we really need to use Thunks here?  Is the flexibility worth the extra
> heap
> > > allocation here?  Can't we have a bool parameter in the Closure object
that
> > > indicates whether or not to AddRef()/Release()?  Note that to do this,
we'd
> > have
> > > to define a helper function with a boolean template parameter.  Specialize
> one
> > > boolean value to call AddRef()/Release() and the other to be a no-op. 
This
> is
> > > necessary since otherwise the code may not compile since O may not define
> > > AddRef()/Release().
> > 
> > Looking at this more, I don't think it's quite that simple.  We probably
don't
> > need a fully generalized thunk.  However, if we specialize the Thunk object
> > based on a boolean refcount, then the caller will have to always declare
that
> in
> > the type.  So, the API would look like:
> 
> I never said template parameter.  I said parameter.  A template parameter
would
> cause the bad API as you pointed out.
> 
> > 
> >   Thunk<void(void), true> t = base::Prebind(&O::blah, &o);
> > 
> > Also, if t held the reference, to o, then all copies of t would ref/deref o.

> > We'd be trading a heap allocation at creation for an extra ref/deref per
copy.
> 
> Yes, that'd be bad, and that's not what I'm suggesting :)
> 
> > 
> > I think a nicer solution would be to bind a scoped_refptr into the argument
> list
> > when we're refcounting.  See the "Invoker" think I just added.
> 
> This approach adds unnecessary AddRef()/Release() pairs.  I'll point them out
in
> the new code.
> 
> http://codereview.chromium.org/6094005/diff/3001/base/closure.h#newcode268
> base/closure.h:268: func_ = ::std::tr1::bind(&DoNothing);
> On 2011/01/05 03:17:42, awong wrote:
> > On 2011/01/05 00:30:17, willchan wrote:
> > > can't this just be std::tr1::function()?
> > 
> > That actually asserted.
> 
> Oh really?  Ok, whatever.
> 
> > 
> > However, I can remove the bind call and just do func_ = &DoNothing. 
> 
> Sounds good.
> 
> http://codereview.chromium.org/6094005/diff/11001/base/prebind.h
> File base/prebind.h (right):
> 
> http://codereview.chromium.org/6094005/diff/11001/base/prebind.h#newcode233
> base/prebind.h:233: typename RetainTraits<T>::type*
> object_(RetainTraits<T>::unwrap(t));
> Here you take T, which may be scoped_refptr<O>, and withdraw the raw pointer. 
> In the next line, you use MaybeScopedRefptr to wrap it within a
scoped_refptr<O>
> again.  This then gets copied by value as a parameter to bind().  bind() then
> will copy construct it into its member variable to store the parameter.  Then
> when the Thunk is returned, it will be copy constructed to the local variable.

> Every time the Thunk is copied, the scoped_refptr will be copied.  So each
time
> we pass around the Thunk, we get extra AddRef()/Release() pairs.  This leads
to
> a lot of unnecessary refcounting.
> 
> Note that this is not a problem for C++0x, because of move semantics with
rvalue
> references.  But this implementation as is will multiply the number of
> AddRef()/Release() pairs in Chromium for all Callbacks/Tasks (which would now
be
> Thunks).
> 
> Note that the current implementation avoids this problem because Task objects
> are heap allocated and generally not copied, whereas Thunk is intended to be
> copied on the stack.
> 
> http://codereview.chromium.org/6094005/diff/11001/base/prebind.h#newcode239
> base/prebind.h:239: RetainTraits<T>::MaybeScopedRefptr(object_)));
> MaybeScopedRefptr() basically always forces using a scoped_refptr for a raw
> pointer, unless Unretained() is used.  This causes the problem for
> DISABLE_RUNNABLE_METHOD_REFCOUNT() which basically says an object shouldn't be
> refcounted.  As I proposed in my fix to DISABLE_RUNNABLE_METHOD_REFCOUNT, a
> base::integral_type value should be added to the UnretainedWrapper class, so
> that MaybeScopedRefptr adds the scoped_refptr if T::is_refcounted::value.

For this last point, I'm hoping we just completely kill
DISABLE_RUNNABLE_METHOD_REFCOUNT() and force everyone to explicitly wrap their
objects with Unreation() when creating a callback.  That side-steps this issue,
no?

Powered by Google App Engine
This is Rietveld 408576698