From 762ec427538fc83577e2433fa91bcae32feb8ca6 Mon Sep 17 00:00:00 2001 From: Eric Niebler Date: Thu, 26 Nov 2026 18:51:16 -0800 Subject: [PATCH] hold environments that are free to copy by value in env adaptors `__fwd_env`, `__join`, `__hide_query` and `__hide_scheduler` held an lvalue environment by reference and an rvalue by value, so an environment that was reachable both ways had two spellings. everything computed from the type of an environment (completion signatures, completion domains, the attributes of the operations nested in it) was computed once for each. an environment that is trivially copy-constructible and trivially destructible is now held by value either way (`__env::__held_t`). copying it costs nothing, and its adaptors have one spelling. other lvalue environments are still held by reference. on test_merge_each_threaded.cpp (clang 22, frontend only), peak memory drops from 5.5 GB to 2.9 GB with extra type checking and from 2.4 GB to 1.5 GB without, and compile time by about 40%. Co-Authored-By: Claude Opus 5.5 --- include/stdexec/__detail/__domain.hpp | 15 ++++++++------ include/stdexec/__detail/__env.hpp | 29 +++++++++++++++++++++------ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/include/stdexec/__detail/__domain.hpp b/include/stdexec/__detail/__domain.hpp index c1dea734e..2e55cf904 100644 --- a/include/stdexec/__detail/__domain.hpp +++ b/include/stdexec/__detail/__domain.hpp @@ -27,6 +27,7 @@ import stdexec; # include "__completion_behavior.hpp" # include "__concepts.hpp" +# include "__env.hpp" # include "__meta.hpp" # include "__sender_introspection.hpp" # include "__utility.hpp" @@ -238,8 +239,8 @@ namespace STDEXEC template struct __hide_query { - constexpr explicit __hide_query(_Env &&__env, _Queries...) noexcept - : __env_{static_cast<_Env &&>(__env)} + constexpr explicit __hide_query(_Env __env, _Queries...) noexcept + : __env_(static_cast<_Env &&>(__env)) {} template _Query, class... _As> @@ -255,22 +256,24 @@ namespace STDEXEC _Env __env_; }; + // (The environment is held as __env::__held_t says.) template STDEXEC_HOST_DEVICE_DEDUCTION_GUIDE - __hide_query(_Env &&, _Queries...) -> __hide_query<_Env, _Queries...>; + __hide_query(_Env &&, _Queries...) -> __hide_query<__env::__held_t<_Env>, _Queries...>; //! @brief A wrapper around an environment that hides the get_start_scheduler //! and get_domain queries. template struct __hide_scheduler : __hide_query<_Env, get_start_scheduler_t, get_domain_t> { - constexpr explicit __hide_scheduler(_Env &&__env) noexcept + constexpr explicit __hide_scheduler(_Env __env) noexcept : __hide_query<_Env, get_start_scheduler_t, get_domain_t>{static_cast<_Env &&>(__env), {}, {}} {} }; template - STDEXEC_HOST_DEVICE_DEDUCTION_GUIDE __hide_scheduler(_Env &&) -> __hide_scheduler<_Env>; + STDEXEC_HOST_DEVICE_DEDUCTION_GUIDE + __hide_scheduler(_Env &&) -> __hide_scheduler<__env::__held_t<_Env>>; ////////////////////////////////////////////////////////////////////////////// //! @brief A query type for asking a sender's attributes for the domain on @@ -454,7 +457,7 @@ namespace STDEXEC else if constexpr (__callable) { using __sch_t = __call_result_t; - using __env_t = __hide_scheduler<_Env const &>; + using __env_t = __hide_scheduler<__env::__held_t<_Env const &>>; using __cmpl_sch_t = __call_result_t, __sch_t, __env_t>; return __detail::__scheduler_domain_t<__cmpl_sch_t, __env_t>{}; diff --git a/include/stdexec/__detail/__env.hpp b/include/stdexec/__detail/__env.hpp index feb59b738..63ba3b686 100644 --- a/include/stdexec/__detail/__env.hpp +++ b/include/stdexec/__detail/__env.hpp @@ -83,6 +83,22 @@ namespace STDEXEC template concept __is_fwd_env = __is_instance_of<__decay_t<_Env>, __fwd>; + // The type in which an environment adaptor holds the environment it adapts, given the + // type of the expression it adapts: an environment that is free to copy (trivially + // copy-constructible and trivially destructible) is held by value, as is an rvalue; any + // other lvalue is held by reference. + // + // So the type of an adaptor of an environment that is free to copy does not depend on + // whether the adaptor was given an lvalue or an rvalue. Everything computed from the type + // of an environment (completion signatures, completion domains, and so on, for the + // operation and for the operations nested in it) is computed once for each type, and an + // environment that was reachable both ways had two. + template + using __held_t = __if_c> + && std::is_trivially_destructible_v<__decay_t<_Env>>, + __decay_t<_Env>, + _Env>; + struct __fwd_fn { template @@ -91,11 +107,11 @@ namespace STDEXEC { if constexpr (__decays_to<_Env, env<>> || __is_fwd_env<_Env>) { - return static_cast<_Env>(static_cast<_Env &&>(__env)); + return static_cast<__held_t<_Env>>(static_cast<_Env &&>(__env)); } else { - return __fwd<_Env>{static_cast<_Env &&>(__env)}; + return __fwd<__held_t<_Env>>{static_cast<_Env &&>(__env)}; } } }; @@ -108,7 +124,7 @@ namespace STDEXEC { template STDEXEC_ATTRIBUTE(nodiscard, always_inline, host, device) - constexpr auto operator()(_Env &&__env) const noexcept -> _Env + constexpr auto operator()(_Env &&__env) const noexcept -> __held_t<_Env> { return static_cast<_Env &&>(__env); } @@ -123,12 +139,13 @@ namespace STDEXEC } else if constexpr (__decays_to<_Env2, env<>>) { - return static_cast<_Env1>(static_cast<_Env1 &&>(__env1)); + return static_cast<__held_t<_Env1>>(static_cast<_Env1 &&>(__env1)); } else { - return env<_Env1, __fwd_env_t<_Env2>>{static_cast<_Env1 &&>(__env1), - __fwd_fn()(static_cast<_Env2 &&>(__env2))}; + using __env1_t = __held_t<_Env1>; + return env<__env1_t, __fwd_env_t<_Env2>>{static_cast<_Env1 &&>(__env1), + __fwd_fn()(static_cast<_Env2 &&>(__env2))}; } } };