- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我最近发布了一个有关由于C++中的虚拟性而导致的内存开销的问题。答案使我了解了vtable和vptr的工作原理。
我的问题如下:我在 super 计算机上工作,我有数十亿个对象,因此,由于虚拟性,我必须关心内存开销。经过一些措施,当我将类与虚函数一起使用时,每个派生对象都有其8字节的vptr。这一点一点都不能忽略。
我不知道英特尔icpc或g++是否具有某些配置/选项/参数,以使用精度可调的“全局” vtable和索引而不是vptr。因为这样可以让我为2亿个对象使用2字节的索引(无符号short int)而不是8字节的vptr(这样可以大大减少内存开销)。有没有办法用编译选项来做到这一点(或类似的东西)?
非常感谢你。
最佳答案
不幸的是...不是自动的。
但是请记住,v表只是运行时多态性的语法糖。如果您愿意重新设计代码,则有几种选择。
std::vector<Cat> cats;
std::vector<Dog> dogs;
std::vector<Ostrich> ostriches;
void dosomething(Animal const& a);
Cat
或
Dog
嵌入虚拟指针似乎很浪费,因为
知道是动态类型(它们是按值存储的)。
// Interface
class Animal {
public:
virtual ~Animal() {}
virtual size_t age() const = 0;
virtual size_t weight() const = 0;
virtual void eat(Food const&) = 0;
virtual void sleep() = 0;
private:
Animal(Animal const&) = delete;
Animal& operator=(Animal const&) = delete;
};
// Concrete class
class Cat {
public:
size_t age() const;
size_t weight() const;
void eat(Food const&);
void sleep(Duration);
};
template <typename T>
class AnimalT: public Animal {
public:
AnimalT(T& r): _ref(r) {}
virtual size_t age() const override { return _ref.age(); }
virtual size_t weight() const { return _ref.weight(); }
virtual void eat(Food const& f) override { _ref.eat(f); }
virtual void sleep(Duration const d) override { _ref.sleep(d); }
private:
T& _ref;
};
template <typename T>
AnimalT<T> iface_animal(T& r) { return AnimalT<T>(r); }
for (auto const& c: cats) { dosomething(iface_animal(c)); }
AnimalT<T>
也可以使用值(而不是引用),并提供
clone
方法,该方法允许您根据情况完全选择使用v指针。
template <typename T> struct ref { ref(T& t): _ref(t); T& _ref; };
template <typename T>
T& deref(T& r) { return r; }
template <typename T>
T& deref(ref<T> const& r) { return r._ref; }
template <typename T>
class AnimalT: public Animal {
public:
AnimalT(T r): _r(r) {}
std::unique_ptr< Animal<T> > clone() const { return { new Animal<T>(_r); } }
virtual size_t age() const override { return deref(_r).age(); }
virtual size_t weight() const { return deref(_r).weight(); }
virtual void eat(Food const& f) override { deref(_r).eat(f); }
virtual void sleep(Duration const d) override { deref(_r).sleep(d); }
private:
T _r;
};
template <typename T>
AnimalT<T> iface_animal(T r) { return AnimalT<T>(r); }
template <typename T>
AnimalT<ref<T>> iface_animal_ref(T& r) { return Animal<ref<T>>(r); }
struct FooVTable {
typedef void (Foo::*DoFunc)(int, int);
DoFunc _do;
};
Foo
中的层次结构提供一个全局数组:
extern FooVTable const* const FooVTableFoo;
extern FooVTable const* const FooVTableBar;
FooVTable const* const FooVTables[] = { FooVTableFoo, FooVTableBar };
enum class FooVTableIndex: unsigned short {
Foo,
Bar
};
Foo
类中保留最派生的类型:
class Foo {
public:
void dofunc(int i, int j) {
(this->*(table()->_do))(i, j);
}
protected:
FooVTable const* table() const { return FooVTables[_vindex]; }
private:
FooVTableIndex _vindex;
};
FooVTables
数组和
FooVTableIndex
枚举需要了解层次结构的所有类型。
enum
,列出了所有成员enumerator
传递给其基础enum
并适当地转换enum class FooType { Foo, Bar, Bor };
class Foo {
public:
int dodispatcher() {
switch(_type) {
case FooType::Foo:
return static_cast<Foo&>(*this).dosomething();
case FooType::Bar:
return static_cast<Bar&>(*this).dosomething();
case FooType::Bor:
return static_cast<Bor&>(*this).dosomething();
}
assert(0 && "Should never get there");
}
private:
FooType _type;
};
// FooList.inc
ACT_ON(Foo)
ACT_ON(Bar)
ACT_ON(Bor)
void Foo::dodispatcher() {
switch(_type) {
# define ACT_ON(X) case FooType::X: return static_cast<X&>(*this).dosomething();
# include "FooList.inc"
# undef ACT_ON
}
assert(0 && "Should never get there");
}
Foo.cpp
需要包括及其派生类 header 的所有。有效地密封了层次结构。关于c++ - C++/编译: is it possible to set the size of the vptr (global vtable + 2 bytes index),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10562268/
我有以下代码: foreach (byte b in bytes) { byte inv = byte.MaxValue - b; // Add the new value to a
我需要从这个文本文件source.txt中读取内容并将内容反向写入这个文本文件destination.txt。读取和写入必须使用逐字节完成! 我使用 BufferedReader 和 Buffered
我需要存储大量 RGB 颜色对象。对于某些常见用途,这些占用了我的应用程序总内存的 8% 到 12%。我目前将其定义如下: class MyColor { byte red; byte green;
我有一个由字节数组表示的整数。 byte[] result = getResult(); resultInt1 = Integer.parseInt(Bytes.toString(result));/
我正在尝试使用 Rusoto 库调用 AWS Lambda 函数。该请求有一个 JSON 编码的有效负载,我目前将其作为一个字符串,但该库为此坚持使用 bytes::bytes::Bytes 结构。我
我正在尝试基于 Tokio's example 编写一个 TCP 服务器. 当我尝试发送缓冲区时,编译器返回错误 0277。 我的代码:(playground) extern crate tokio;
我知道我可以通过 IList 进行枚举,例如: public byte[] ConvertToByteArray(IList> list) { IList newList = new List
考虑这样一个文本文件: Some text here. --- More text another line. --- Third part of text. 我想把它分成三部分,用---分隔符分开。
如果我有一个字节变量:byte b = 0; 为什么以下工作: b++; b += 1; // compiles ...但这不是吗? b = b + 1; // compile er
我有一个简单的字节数组,我想从中获取颜色。我的计划是用红色表示三位,绿色表示三位,蓝色表示两位。 8 位。 我认为颜色是正确的: 如有错误请指正 byte[] colours = new byte[
我的目标是比较两个字节数组中的两个字符串值。它实际上需要创建两个新的字符串对象才能使用 contains 方法。是选择正确还是有什么办法可以使用优化方式而不使用新的关键字。 if(new String
我正在使用github.com/tarm/serial来连接一些串行仪器。在开发过程中,我使用/dev/ttyp0和/dev/ptyp0对,其中go进程连接到一个,我使用screen连接到另一个。我编
好的,所以如果一个字节是 8 位,那么半字节就是 4 位。并且您可以将四分之一字节作为 2 位(尽管我想,如果有的话,它会被称为双位)。 虽然这是一致的,但如果我使用这个词,有人会感到困惑(或惊讶)吗
我在解释文件时遇到问题。文件构建如下: "name"-@-"date"-@-"author"-@-"signature" 签名是一个字节数组。当我读回文件时,我将其解析为 String 并拆分它: m
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
Java 让我很难过,因为它需要 ArrayList 的包装类秒。我将如何添加 byte[]到 ArrayList ? 最佳答案 LOL 认为我必须包装所有东西。 ArrayList作品。谢谢一晒。
我有一个 16 字节的 md5 散列,我需要使用 XOR 将其“折叠”成 4 字节数据:{1st 4 bytes} XOR {2nd 4 bytes} XOR {3rd 4 bytes} XOR {4
我正在学习SMSC smc91cx驱动代码,我学习了如何根据Application Note 9-6的说明编写smc91c111网卡的测试代码。 .我无法理解“传输数据包”下的以下说明: Write
我必须附加(可变数量的)字节数组。集合似乎只适用于包装类,即 Byte。大约 20 小时后,我想到了这个,并且它有效,但我想知道它是否可以改进(添加到列表,但欢迎任何其他改进建议:),即 Collec
我有两个基本相同的操作: insert_bytes(from, count) delete_bytes(start, stop) -> delete_bytes(from, count) insert
我是一名优秀的程序员,十分优秀!