From 0ade558999d6d13fb58f35853f201791e0b9339a Mon Sep 17 00:00:00 2001 From: Avi Kivity Date: Tue, 3 Apr 2018 12:22:14 +0300 Subject: [PATCH 1/2] api: simplify 6-argument map_reduce_cf() variant The 6-argument map_reduce_cf function is identical to the 5-argument version, except that it applies performs an extra cast (by calling the 6th argument's operator=()). Simplify the code by calling the 5-argument version from the 6-argument version. Reduces binary size by ~10%. --- api/column_family.hh | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/api/column_family.hh b/api/column_family.hh index 3764b76db7..e29c5c22a6 100644 --- a/api/column_family.hh +++ b/api/column_family.hh @@ -58,20 +58,10 @@ future map_reduce_cf(http_context& ctx, const sstring& n }); } -template -future map_reduce_cf_raw(http_context& ctx, const sstring& name, I init, - Mapper mapper, Reducer reducer, Result result) { - auto uuid = get_uuid(name, ctx.db.local()); - return ctx.db.map_reduce0([mapper, uuid](database& db) { - return mapper(db.find_column_family(uuid)); - }, init, reducer); -} - - template future map_reduce_cf(http_context& ctx, const sstring& name, I init, Mapper mapper, Reducer reducer, Result result) { - return map_reduce_cf_raw(ctx, name, init, mapper, reducer, result).then([result](const I& res) mutable { + return map_reduce_cf_raw(ctx, name, init, mapper, reducer).then([result](const I& res) mutable { result = res; return make_ready_future(result); }); From 6c35db2c4441831ce53037803b78f5ace778b2f5 Mon Sep 17 00:00:00 2001 From: Avi Kivity Date: Tue, 3 Apr 2018 13:08:22 +0300 Subject: [PATCH 2/2] api: type-erase all-column_family map_reduce variant Encapsulate the map_reduce parameters in type-erased std::function, as well as the iterator-on-all-column-families logic. Reduces binary size by 18%. --- api/column_family.hh | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/api/column_family.hh b/api/column_family.hh index e29c5c22a6..0d3879fcca 100644 --- a/api/column_family.hh +++ b/api/column_family.hh @@ -67,16 +67,33 @@ future map_reduce_cf(http_context& ctx, const sstring& n }); } -template -future map_reduce_cf_raw(http_context& ctx, I init, - Mapper mapper, Reducer reducer) { - return ctx.db.map_reduce0([mapper, init, reducer](database& db) { +struct map_reduce_column_families_locally { + std::any init; + std::function mapper; + std::function reducer; + std::any operator()(database& db) const { auto res = init; for (auto i : db.get_column_families()) { res = reducer(res, mapper(*i.second.get())); } return res; - }, init, reducer); + } +}; + +template +future map_reduce_cf_raw(http_context& ctx, I init, + Mapper mapper, Reducer reducer) { + using mapper_type = std::function; + using reducer_type = std::function; + auto wrapped_mapper = mapper_type([mapper = std::move(mapper)] (column_family& cf) mutable { + return I(mapper(cf)); + }); + auto wrapped_reducer = reducer_type([reducer = std::move(reducer)] (std::any a, std::any b) mutable { + return I(reducer(std::any_cast(std::move(a)), std::any_cast(std::move(b)))); + }); + return ctx.db.map_reduce0(map_reduce_column_families_locally{init, std::move(wrapped_mapper), wrapped_reducer}, std::any(init), wrapped_reducer).then([] (std::any res) { + return std::any_cast(std::move(res)); + }); }