Skip to content

Commit 696e76f

Browse files
committed
Use integer fill for integer storages (avoid a conversion to double)
1 parent 11350f7 commit 696e76f

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

boost_histogram/_internal/hist.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,10 @@ def fill(self, *args, **kwargs):
184184
----------
185185
*args : Union[Array[float], Array[int], Array[str], float, int, str]
186186
Provide one value or array per dimension.
187-
weight : List[Union[Array[float], Array[int], Array[str], float, int, str]]]
188-
Provide weights (only if the histogram storage supports it)
189-
sample : List[Union[Array[float], Array[int], Array[str], float, int, str]]]
187+
weight : List[Union[Array[float], Array[int], float, int]]]
188+
Provide weights (float only if the histogram storage supports it)
189+
sample : List[Union[Array[float], float]]]
190190
Provide samples (only if the histogram storage supports it)
191-
192191
"""
193192

194193
self._hist.fill(*args, **kwargs)

include/fill.hpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ using arg_t = variant::variant<c_array_t<double>,
101101
int,
102102
c_array_t<std::string>,
103103
std::string>;
104-
105-
using weight_t = variant::variant<variant::monostate, double, c_array_t<double>>;
104+
template <class T>
105+
using weight_t = variant::variant<variant::monostate, T, c_array_t<T>>;
106106

107107
inline auto get_vargs(const vector_axis_variant& axes, const py::args& args) {
108108
if(args.size() != axes.size())
@@ -131,25 +131,26 @@ inline auto get_vargs(const vector_axis_variant& axes, const py::args& args) {
131131
return vargs;
132132
}
133133

134-
inline auto get_weight(py::kwargs& kwargs) {
134+
template <class T>
135+
auto get_weight(py::kwargs& kwargs) {
135136
// default constructed as monostate to indicate absence of weight
136-
variant::variant<variant::monostate, double, c_array_t<double>> weight;
137+
weight_t<T> weight;
137138
auto w = optional_arg(kwargs, "weight");
138139
if(!w.is_none()) {
139-
if(is_value<double>(w))
140-
weight = py::cast<double>(w);
140+
if(is_value<T>(w))
141+
weight = py::cast<T>(w);
141142
else
142-
weight = py::cast<c_array_t<double>>(w);
143+
weight = py::cast<c_array_t<T>>(w);
143144
}
144145
return weight;
145146
}
146147

147148
// for accumulators that accept a weight
148-
template <class Histogram, class VArgs>
149+
template <class weight_type, class Histogram, class VArgs>
149150
void fill_impl(bh::detail::accumulator_traits_holder<true>,
150151
Histogram& h,
151152
const VArgs& vargs,
152-
const weight_t& weight,
153+
const weight_t<weight_type>& weight,
153154
py::kwargs& kwargs) {
154155
finalize_args(kwargs);
155156

@@ -162,11 +163,11 @@ void fill_impl(bh::detail::accumulator_traits_holder<true>,
162163
}
163164

164165
// for accumulators that accept a weight and a double
165-
template <class Histogram, class VArgs>
166+
template <class weight_type, class Histogram, class VArgs>
166167
void fill_impl(bh::detail::accumulator_traits_holder<true, const double&>,
167168
Histogram& h,
168169
const VArgs& vargs,
169-
const weight_t& weight,
170+
const weight_t<weight_type>& weight,
170171
py::kwargs& kwargs) {
171172
auto s = required_arg(kwargs, "sample");
172173
finalize_args(kwargs);
@@ -191,10 +192,14 @@ void fill_impl(bh::detail::accumulator_traits_holder<true, const double&>,
191192
template <class Histogram>
192193
Histogram& fill(Histogram& self, py::args args, py::kwargs kwargs) {
193194
using value_type = typename Histogram::value_type;
194-
detail::fill_impl(bh::detail::accumulator_traits<value_type>{},
195-
self,
196-
detail::get_vargs(bh::unsafe_access::axes(self), args),
197-
detail::get_weight(kwargs),
198-
kwargs);
195+
using weight_type
196+
= boost::mp11::mp_if_c<std::is_integral<value_type>::value, int, double>;
197+
198+
detail::fill_impl<weight_type>(
199+
bh::detail::accumulator_traits<value_type>{},
200+
self,
201+
detail::get_vargs(bh::unsafe_access::axes(self), args),
202+
detail::get_weight<weight_type>(kwargs),
203+
kwargs);
199204
return self;
200205
}

0 commit comments

Comments
 (0)