我目前正在使用boost.parameter和一些工厂函数,编译时间越来越多。
目前我有一个像这样的共同模式:
auto thing = makeThing(property1 = foo::bar, "myThing"_thingName);
其中makeThing
有30个参数,其中大多数都有默认值。我想保留"命名参数,如"语法和按类型而非按位置匹配参数的能力。
如何在不更改工厂调用站点的语法的情况下获得更好的编译速度?
注意:根据boost.MPL速度和强度速度之间的差异来判断,如果现代元编程技术用于boost.parameter等效,那么在编译时应该至少有一个数量级的改进。
更新:以下是我正在做的事情的精简示例: 在裸机嵌入式上下文中,我根据基于策略的类设计习语将不同的外设抽象为复杂的模板类。每个类在编译时都会获取大量配置信息,并且只使用所需的功能(不能依赖优化器来去掉未使用的东西,因为所有的SFR交互都是可观察的,因此是不稳定的,所以不允许这样做)。
这些基于策略的类对于用户来说是非常难看的,并且大多数嵌入式用户如果看到<在公共界面中,所以我使用boost.parameter创建一个性感的工厂,他们可以传递所有类型编码的愿望(如hana风格),我生成类作为本地静态连接到所需的ISR并传回一个处理。
namespace usb
{
BOOST_PARAMETER_NAME(hw)
BOOST_PARAMETER_NAME(vid)
BOOST_PARAMETER_NAME(pid)
BOOST_PARAMETER_NAME(device_class)
BOOST_PARAMETER_NAME(max_packet_ep0)
BOOST_PARAMETER_NAME(max_packet)
BOOST_PARAMETER_NAME(packet_pool_size)
BOOST_PARAMETER_NAME(device_description)
BOOST_PARAMETER_NAME(device_calss_description)
BOOST_PARAMETER_NAME(can_power_down)
BOOST_PARAMETER_NAME(stall_supported)
BOOST_PARAMETER_NAME(setup_packet_timeout)
//...
BOOST_PARAMETER_FUNCTION(
(usb_handle),
make,
tag,
(required(hw, *))
(optional
(vid, *, 0x6001_ci)
(pid, *, 0x1234_ci)
(device_class, *, cdc_class{})
(max_packet_ep0, *, 8_ci)
(max_packet, *, 64_ci)
(packet_pool_size, *, 12_ci)
(device_description, *, "")
(device_calss_description, *, "")
(can_power_down, *, 0_ci)
(stall_supported, *, 0_ci)
(setup_packet_timeout, *, 100_ci)
)
)
{
// makes a local static policy based class taylored at compile time
// to support only the specified features
return{}; //returns a handle to the local static
}
}
大多数工厂都有10-25个参数,预处理时间似乎是杀手锏。无论用户是否实际调用该功能,每个工厂需要1-5秒。
更新2:好的赏金已经结束,所以看起来没有解决方案。如果我找时间,我会写一个boost.parameter替换并在此处链接。使命名参数函数的返回类型依赖于输入类型以便更接近boost.hana样式语义也是很好的。
答案 0 :(得分:1)
您可以实现via global constexpr静态对象,如下所示:
struct init_
{
my_class operator()(int i) {return i;}
my_class operator= (int i) {return i;}
};
consexpr static init_ init;
//make function
template<typename ...Args>
thingy make(Args&&...args);
auto x = make(init=42);
您可能需要向init_添加constexpr ctor。然后,您可以使用boost.fusion或boost.hana来获取序列的for_each并初始化您的类型。
struct thingy { int x;};
struct x_init
{
int init;
x_init (int i) : init(i) {};
void operator()(thingy & t)
{ t.x = init;}
};
struct x_
{
constexpr x_() {};
x_init operator()(int i) const {return i;}
x_init operator= (int i) const {return i;}
};
constexpr static x_ x;
template<typename ...Args>
thingy make(Args &&...)
{
thingy t;
auto vec = boost::fusion::make_vector(std::forward<Args>(args)...);
boost::fusion::for_each(vec, [t](auto & init){init(t);});
}
auto t = make(x=42);
Boost.Process实际上使用它,如果你在github上查看我的存储库,你会发现它的一个相当复杂的版本。