[PATCH 2/2] libstdc++: Optimize operator<< for piecewise distributions.

Tomasz Kaminski tkaminsk@redhat.com
Tue May 26 12:53:51 GMT 2026


On Tue, May 26, 2026 at 11:37 AM Jonathan Wakely <jwakely@redhat.com> wrote:

> On Tue, 26 May 2026 at 10:21, Tomasz Kamiński <tkaminsk@redhat.com> wrote:
> >
> > This avoids creating an temporary vector and uses _M_int and _M_den
> > members of _M_param. Empty _M_int (default) values are handled by
> > printing values direclty.
> >
> > libstdc++-v3/ChangeLog:
> >
> >         * include/bits/random.h
> (piecewise_constant_distribution::param_type)
> >         (piecewise_linear_distribution::param_type): Befriend operator<<.
> >         * include/bits/random.tcc
> >         (operator<<(basic_ostream&, piecewise_linear_distribution)
> >         (operator<<(basic_ostream&, piecewise_constant_distribution):
> >         Use __x._M_param._M_int and __x._M_param._M_den instead of
> accessors.
> > ---
> > In addition to avoid allocating a temporary vectors, this output the
> > type stored in _M_den vector, even if the signature of interface changes.
> > And also gives a choice of the output, with simple replacements of auto
> > in for each loop.
> >
> > Testing on x86_64-linux. All *piecewise* test already passed.
> > OK for trunk when all test passes?
>
> OK
>
I would prefer to land the commit with additional test (Patch 1/2) first,
so we know that output was not affected by this (and later changes).

>
>
> >
> >  libstdc++-v3/include/bits/random.h   | 10 +++++++
> >  libstdc++-v3/include/bits/random.tcc | 44 ++++++++++++++++------------
> >  2 files changed, 36 insertions(+), 18 deletions(-)
> >
> > diff --git a/libstdc++-v3/include/bits/random.h
> b/libstdc++-v3/include/bits/random.h
> > index 5d037465d3c..a0592a00763 100644
> > --- a/libstdc++-v3/include/bits/random.h
> > +++ b/libstdc++-v3/include/bits/random.h
> > @@ -6474,6 +6474,11 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)
> >         std::vector<_RealType> _M_int;
> >         std::vector<double> _M_den;
> >         std::vector<double> _M_cp;
> > +
> > +       template<typename _RealType1, typename _CharT, typename _Traits>
> > +         friend std::basic_ostream<_CharT, _Traits>&
> > +         operator<<(std::basic_ostream<_CharT, _Traits>&,
> > +                    const
> std::piecewise_constant_distribution<_RealType1>&);
> >        };
> >
> >        piecewise_constant_distribution()
> > @@ -6758,6 +6763,11 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)
> >         std::vector<double> _M_den;
> >         std::vector<double> _M_cp;
> >         std::vector<double> _M_m;
> > +
> > +       template<typename _RealType1, typename _CharT, typename _Traits>
> > +         friend std::basic_ostream<_CharT, _Traits>&
> > +         operator<<(std::basic_ostream<_CharT, _Traits>&,
> > +                    const
> std::piecewise_linear_distribution<_RealType1>&);
> >        };
> >
> >        piecewise_linear_distribution()
> > diff --git a/libstdc++-v3/include/bits/random.tcc
> b/libstdc++-v3/include/bits/random.tcc
> > index 80b51d099fa..e581f912764 100644
> > --- a/libstdc++-v3/include/bits/random.tcc
> > +++ b/libstdc++-v3/include/bits/random.tcc
> > @@ -3173,15 +3173,19 @@ namespace __detail
> >        __os.fill(__space);
> >        __os.precision(std::numeric_limits<_RealType>::max_digits10);
> >
> > -      std::vector<_RealType> __int = __x.intervals();
> > -      __os << __int.size() - 1;
> > -
> > -      for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
> > -       __os << __space << *__xit;
> > -
> > -      std::vector<double> __den = __x.densities();
> > -      for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
> > -       __os << __space << *__dit;
> > +      const auto& __int = __x._M_param._M_int;
> > +      if (__int.empty())
> > +       __os << size_t(1)
> > +            << __space << _RealType(0) << __space << _RealType(1)
> > +            << __space << _RealType(1);
> > +      else
> > +       {
> > +         __os << __int.size() - 1;
> > +         for (auto __xv : __int)
> > +           __os << __space << __xv;
> > +         for (auto __dv : __x._M_param._M_den)
> > +           __os << __space << __dv;
> > +       }
> >
> >        __os.flags(__flags);
> >        __os.fill(__fill);
> > @@ -3436,15 +3440,19 @@ namespace __detail
> >        __os.fill(__space);
> >        __os.precision(std::numeric_limits<_RealType>::max_digits10);
> >
> > -      std::vector<_RealType> __int = __x.intervals();
> > -      __os << __int.size() - 1;
> > -
> > -      for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
> > -       __os << __space << *__xit;
> > -
> > -      std::vector<double> __den = __x.densities();
> > -      for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
> > -       __os << __space << *__dit;
> > +      auto const& __int = __x._M_param._M_int;
> > +      if (__int.empty())
> > +       __os << size_t(1)
> > +            << __space << _RealType(0) << __space << _RealType(1)
> > +            << __space << _RealType(1) << __space << _RealType(1);
> > +      else
> > +       {
> > +         __os << __int.size() - 1;
> > +         for (auto __xv : __int)
> > +           __os << __space << __xv;
> > +         for (auto __dv : __x._M_param._M_den)
> > +           __os << __space << __dv;
> > +       }
> >
> >        __os.flags(__flags);
> >        __os.fill(__fill);
> > --
> > 2.54.0
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20260526/75c2d7fa/attachment-0001.htm>


More information about the Libstdc++ mailing list