Commit 1fdcca3d authored by tzik's avatar tzik Committed by Commit bot

Split BindStateBase ctor for non-cancellable Bind

http://crrev.com/59aa6bb1162b31d2 added a parameter of BindStateBase,
and that causes 0.8% (40kB) binary size bloat on cronet, by adding
a new instruction to the constructor invocation at BindState ctor.

To reduce the binary size, this CL splits BindStateBase into two: one
for cancellable bind and another for non-cancellable one, so that
non-cancellable bind doesn't need to pass an extra argument to
BindStateBase.

This reduces the stripped binary size of chrome on Linux by 56kB.

Review-Url: https://codereview.chromium.org/2322313002
Cr-Commit-Position: refs/heads/master@{#418502}
parent 4279cdda
......@@ -392,6 +392,7 @@ struct BindState;
template <typename BindStateType, typename SFINAE = void>
struct CancellationChecker {
static constexpr bool is_cancellable = false;
static bool Run(const BindStateBase*) {
return false;
}
......@@ -402,6 +403,7 @@ struct CancellationChecker<
BindState<Functor, BoundArgs...>,
typename std::enable_if<IsWeakMethod<FunctorTraits<Functor>::is_method,
BoundArgs...>::value>::type> {
static constexpr bool is_cancellable = true;
static bool Run(const BindStateBase* base) {
using BindStateType = BindState<Functor, BoundArgs...>;
const BindStateType* bind_state = static_cast<const BindStateType*>(base);
......@@ -411,6 +413,7 @@ struct CancellationChecker<
template <typename Signature, typename... BoundArgs>
struct CancellationChecker<BindState<Callback<Signature>, BoundArgs...>> {
static constexpr bool is_cancellable = true;
static bool Run(const BindStateBase* base) {
using Functor = Callback<Signature>;
using BindStateType = BindState<Functor, BoundArgs...>;
......@@ -424,10 +427,29 @@ struct CancellationChecker<BindState<Callback<Signature>, BoundArgs...>> {
// This stores all the state passed into Bind().
template <typename Functor, typename... BoundArgs>
struct BindState final : BindStateBase {
using IsCancellable = std::integral_constant<
bool, CancellationChecker<BindState>::is_cancellable>;
template <typename ForwardFunctor, typename... ForwardBoundArgs>
explicit BindState(BindStateBase::InvokeFuncStorage invoke_func,
ForwardFunctor&& functor,
ForwardBoundArgs&&... bound_args)
// IsCancellable is std::false_type if the CancellationChecker<>::Run
// returns always false. Otherwise, it's std::true_type.
: BindState(IsCancellable{},
invoke_func,
std::forward<ForwardFunctor>(functor),
std::forward<ForwardBoundArgs>(bound_args)...) {}
Functor functor_;
std::tuple<BoundArgs...> bound_args_;
private:
template <typename ForwardFunctor, typename... ForwardBoundArgs>
explicit BindState(std::true_type,
BindStateBase::InvokeFuncStorage invoke_func,
ForwardFunctor&& functor,
ForwardBoundArgs&&... bound_args)
: BindStateBase(invoke_func, &Destroy,
&CancellationChecker<BindState>::Run),
functor_(std::forward<ForwardFunctor>(functor)),
......@@ -435,10 +457,17 @@ struct BindState final : BindStateBase {
DCHECK(!IsNull(functor_));
}
Functor functor_;
std::tuple<BoundArgs...> bound_args_;
template <typename ForwardFunctor, typename... ForwardBoundArgs>
explicit BindState(std::false_type,
BindStateBase::InvokeFuncStorage invoke_func,
ForwardFunctor&& functor,
ForwardBoundArgs&&... bound_args)
: BindStateBase(invoke_func, &Destroy),
functor_(std::forward<ForwardFunctor>(functor)),
bound_args_(std::forward<ForwardBoundArgs>(bound_args)...) {
DCHECK(!IsNull(functor_));
}
private:
~BindState() {}
static void Destroy(BindStateBase* self) {
......
......@@ -9,13 +9,26 @@
namespace base {
namespace internal {
namespace {
bool ReturnFalse(const BindStateBase*) {
return false;
}
} // namespace
BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
void (*destructor)(BindStateBase*))
: BindStateBase(polymorphic_invoke, destructor, &ReturnFalse) {
}
BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
void (*destructor)(BindStateBase*),
bool (*is_cancelled)(const BindStateBase*))
: polymorphic_invoke_(polymorphic_invoke),
ref_count_(0),
destructor_(destructor),
is_cancelled_(is_cancelled) {}
: polymorphic_invoke_(polymorphic_invoke),
ref_count_(0),
destructor_(destructor),
is_cancelled_(is_cancelled) {}
void BindStateBase::AddRef() {
AtomicRefCountInc(&ref_count_);
......
......@@ -35,6 +35,8 @@ class BASE_EXPORT BindStateBase {
using InvokeFuncStorage = void(*)();
protected:
BindStateBase(InvokeFuncStorage polymorphic_invoke,
void (*destructor)(BindStateBase*));
BindStateBase(InvokeFuncStorage polymorphic_invoke,
void (*destructor)(BindStateBase*),
bool (*is_cancelled)(const BindStateBase*));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment