使用Pimpl的高级变体时可能会影响性能
Possible performance hit when using an advanced variation of Pimpl?
想象一下。
我希望创建不公开任何底层实现的类。即使这样也不意味着有一个指向实现的指针。
我会使用全局对象池来完成此操作。
像这样的东西。
在类TestImpl中实现的类Test。
构造Test时,它会在全局对象池中为自己创建一个TestImpl。
对象池基本上是每种类型的映射。键将是测试实例的内存地址,值将是相应的TestImpl。所以每次调用Test上的成员函数时,它首先从对象池中获取相应的TestImpl,使用this指针作为键。
这种对象池当然必须同步,需要锁。
你认为这样的解决方案会带来多大的性能冲击?
ifc.h
struct ifc {
virtual ~ifc() {}
virtual void api() = 0;
static ifc* create();
};
impl.cc
#include "ifc.h"
struct impl: ifc {
virtual void api() override;
}
void impl::api() {
// code here
}
ifc* ifc::create() {
// or use shared pointer / untrusive pointer and return object back to a pool
// if dynamic memory allocation is not desired (but rather just keep it simple
// and plug something like TCMalloc first (which does low level pooling)
// and see if it gets better)
return new impl();
}
相关文章:
- 数组中的不同浮点值会影响性能 10 倍 - 为什么?
- 为 lambda 分配名称会影响性能吗?
- 通过默认复制构造函数比较 C++ 字符串是否会影响性能,原因为何?
- 隐式转换函数的返回对象时是否会影响性能?
- 何时应使用 C++ 固定宽度整数类型,它们如何影响性能?
- 性能 - 链接 C 尖锐和C++如何影响性能
- C 指针向量如何影响性能
- 公开类的内部组件,以防止编写过多代码和影响性能
- .NET 4,C ,IF..Else和Switch()如何影响性能
- 变量会影响性能吗
- 更改自动分配内存的范围是否会影响性能
- 如何修复不影响性能的"runtime error"?
- 返回间接类型化对象是否会影响性能
- 调用恒定复杂度O(1)5行函数会显著影响性能,原因如下
- 使用Pimpl的高级变体时可能会影响性能
- 功能原型设计会影响性能吗
- 在C/ c++中包含未使用的头文件会影响性能吗?
- 我可以在不影响性能的情况下将此宏更改为内联函数吗
- Dispatch_async在下面的代码中影响性能
- c++中的操作符重载如何影响性能