object-introspection/test/integration/std_unordered_multiset_custom_operator.toml
Jake Hillion 71e734b120 tbv2: calculate total memory footprint
Add the option to calculate total size (inclusive size) by wrapping the
existing iterator. This change provides a new iterator, `SizedIterator`, which
wraps an existing iterator and adds a new field `size` to the output element.

This is achieved with a two pass algorithm on the existing iterator:
1. Gather metadata for each element. This includes the total size up until that
   element and the range of elements that should be included in the size.
2. Return the result from the underlying iterator with the additional
   field.

This algorithm is `O(N)` time on the number of elements in the iterator and
`O(N)` time, storing 16 bytes per element. This isn't super expensive but is a
lot more than the current algorithm which requires close to constant space.
Because of this I've implemented it as a wrapper on the iterator rather than on
by default, though it is now on in every one of our integration test cases.

Test plan:
- Added to the integration tests for full coverage.
2024-01-04 09:21:35 +00:00

77 lines
2.1 KiB
TOML

definitions = '''
template <unsigned int N>
class CustomIntHasher
{
double d[N];
public:
size_t operator() (int const& key) const
{
return std::hash<int>{}(key);
}
};
template <unsigned int N>
class CustomEqualFnInt
{
double d[N];
public:
bool operator() (int const& t1, int const& t2) const
{
return t1 == t2;
}
};
struct Foo {
std::unordered_multiset<int> m1;
std::unordered_multiset<int, CustomIntHasher<8>> m2;
std::unordered_multiset<int, std::hash<int>, CustomEqualFnInt<8>> m3;
std::unordered_multiset<int, CustomIntHasher<8>, CustomEqualFnInt<8>> m4;
};
'''
includes = ["unordered_set"]
[cases]
[cases.a]
param_types = ["const Foo&"]
setup = '''
Foo foo;
for (int i = 0; i < 3; i++) {
foo.m1.insert(i);
}
for (int i = 0; i < 5; i++) {
foo.m2.insert(i);
}
for (int i = 0; i < 7; i++) {
foo.m3.insert(i);
}
for (int i = 0; i < 9; i++) {
foo.m4.insert(i);
}
return {foo};
'''
expect_json = '''[{
"staticSize":480,
"dynamicSize":704,
"members":[
{"name":"m1", "staticSize":56, "dynamicSize":140, "length":3, "capacity":3, "elementStaticSize":12},
{"name":"m2", "staticSize":120, "dynamicSize":164, "length":5, "capacity":5, "elementStaticSize":12},
{"name":"m3", "staticSize":120, "dynamicSize":188, "length":7, "capacity":7, "elementStaticSize":12},
{"name":"m4", "staticSize":184, "dynamicSize":212, "length":9, "capacity":9, "elementStaticSize":12}
]}]'''
expect_json_v2 = '''[{
"staticSize":480,
"exclusiveSize":0,
"size":1472,
"members":[
{"name":"m1", "staticSize":56, "exclusiveSize":220, "size":232, "length":3, "capacity":3},
{"name":"m2", "staticSize":120, "exclusiveSize":324, "size":344, "length":5, "capacity":5},
{"name":"m3", "staticSize":120, "exclusiveSize":364, "size":392, "length":7, "capacity":7},
{"name":"m4", "staticSize":184, "exclusiveSize":468, "size":504, "length":9, "capacity":9}
]}]'''