什么是boost :: variant内存和性能成本? [英] What is boost::variant memory and performance cost?

查看:289
本文介绍了什么是boost :: variant内存和性能成本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

boost :: variant似乎是一个强大的容器来操纵异构的类型集合。我想知道它的成本。在内存中,我认为它占用最大类型的大小加上一个表示哪个()的整数。对于apply_visitor(),我认为它的性能非常好,它可以直接调用除了大量的ifs函数。我的观点是正确的吗?

boost::variant seems a powerful container to manipulate a heterogeneous set of types. I am wondering its cost. In memory, I think it takes up the size of the largest type plus an integer representing which(). For apply_visitor(), I think its performance is very good, it can call directly the function other than lots of ifs. Are my points right?


推荐答案

> boost :: variant 的大小是任何元素的最大大小,向上舍入为最大对齐所需,加上大小

The size of boost::variant is is the max size of any element, rounded up as needed for the largest alignment, plus the size of some integer, and again rounded up.

考虑这些类型的变体,假设标签 uint32_t

Think about a variant of these types, assuming the tag is uint32_t:

struct foo { uint32_t value[3]; }; // size 12, align 4
struct bar { uint64_t v2; }; // size 8, align 8

未标记的并集的大小必须为16,align 8;

An untagged union must have size 16, align 8; adding the 4-byte tag must go up to size 24 to keep align 8.

或考虑以下变体:

struct foo { uint8_t value[5]; }; // size 5, align 1
struct bar { uint16_t v2; }; // size 2, align 2

这些的未标记联合必须具有大小6,添加4字节标签会强制您指定大小为12,对齐4.

An untagged union of these must have size 6, align 2; adding the 4-byte tag forces you to size 12, align 4.

对于调用,我希望它使用函数数组查找自己的变体,这是必要的,因为boost不支持移动构造函数),因为如果链表现不佳,开关是不可能的。

For calling, I expect it uses an array-of-functions lookup (that's how I implemented my own variant, which was necessary since boost's didn't support move constructors), since if chains don't perform well and switches are impossible.

这篇关于什么是boost :: variant内存和性能成本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆