get-size2 0.11.1

Determine the size in bytes an object occupies inside RAM.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// The crate itself is `no_std`, so the tests have to import the `alloc` based prelude items
// explicitly.
use std::string::String;
use std::vec;

use std::mem::size_of;

use get_size2::*;

const ARCSTR_HEADER: usize = 2 * size_of::<usize>();

#[test]
fn arcstr() {
    let value = arcstr::ArcStr::from("Hello, world");
    assert_eq!(value.get_heap_size(), ARCSTR_HEADER + "Hello, world".len());
    assert_eq!(
        value.get_size(),
        size_of::<arcstr::ArcStr>() + ARCSTR_HEADER + value.len()
    );
}

#[test]
fn arcstr_shared() {
    const TEXT: &str = "Hello world";
    let value = arcstr::ArcStr::from(TEXT);
    let shared = (arcstr::ArcStr::clone(&value), arcstr::ArcStr::clone(&value));
    assert_eq!(shared.get_heap_size(), (ARCSTR_HEADER + TEXT.len()) * 2);
    let (size, tracker) = shared.get_heap_size_with_tracker(StandardTracker::new());
    assert_eq!(size, ARCSTR_HEADER + TEXT.len());

    let (size, tracker) = value.get_heap_size_with_tracker(tracker);
    assert_eq!(size, 0);
    let separate = arcstr::ArcStr::from(TEXT);
    let (size, _) = separate.get_heap_size_with_tracker(tracker);
    assert_eq!(size, ARCSTR_HEADER + TEXT.len());

    let (size, _) = value.get_heap_size_with_tracker(NoTracker::new(false));
    assert_eq!(size, 0);
}

#[test]
fn arcstr_static() {
    for value in [
        arcstr::ArcStr::new(),
        arcstr::ArcStr::default(),
        arcstr::ArcStr::from(""),
        arcstr::literal!("Hello world"),
    ] {
        assert_eq!(value.get_heap_size(), 0);
        assert_eq!(value.get_size(), size_of::<arcstr::ArcStr>());
        assert_eq!(arcstr::ArcStr::clone(&value).get_heap_size(), 0);
    }
}

#[test]
fn arcstr_substr() {
    let parent = arcstr::ArcStr::from("Hello world");
    let first = parent.substr(..5);
    let second = parent.substr(6..);
    assert_eq!(first.get_heap_size(), ARCSTR_HEADER + parent.len());
    assert_eq!(
        first.get_size(),
        size_of::<arcstr::Substr>() + ARCSTR_HEADER + parent.len()
    );
    assert_eq!(second.get_heap_size(), ARCSTR_HEADER + parent.len());

    let (size, tracker) = (first, second).get_heap_size_with_tracker(StandardTracker::new());
    assert_eq!(size, ARCSTR_HEADER + parent.len());
    let (size, _) = parent.get_heap_size_with_tracker(tracker);
    assert_eq!(size, 0);
}

#[test]
fn arcstr_substr_static() {
    assert_eq!(arcstr::Substr::new().get_heap_size(), 0);
    assert_eq!(
        arcstr::literal!("Hello world").substr(6..).get_heap_size(),
        0
    );
}

#[test]
fn chrono() {
    use chrono::TimeZone;

    let timedelta = chrono::TimeDelta::seconds(5);
    assert_eq!(timedelta.get_heap_size(), 0);

    let datetime = chrono::Utc.with_ymd_and_hms(2014, 7, 8, 9, 10, 11).unwrap();
    assert_eq!(datetime.naive_utc().get_heap_size(), 0);
    assert_eq!(datetime.naive_utc().date().get_heap_size(), 0);
    assert_eq!(datetime.naive_utc().time().get_heap_size(), 0);
    assert_eq!(datetime.timezone().get_heap_size(), 0);
    assert_eq!(datetime.fixed_offset().timezone().get_heap_size(), 0);
    assert_eq!(datetime.get_heap_size(), 0);
}

#[test]
fn chrono_tz() {
    use chrono::TimeZone;

    let datetime = chrono_tz::UTC
        .with_ymd_and_hms(2014, 7, 8, 9, 10, 11)
        .unwrap();
    assert_eq!(datetime.offset().get_heap_size(), 0);
}

#[test]
fn url() {
    const URL_STR: &str = "https://example.com/path?a=b&c=d";

    let url = url::Url::parse(URL_STR).unwrap();
    assert_eq!(url.get_heap_size(), URL_STR.len());
}

#[test]
fn bytes() {
    const BYTES_STR: &str = "Hello world";

    let bytes = bytes::Bytes::from(BYTES_STR);
    assert_eq!(bytes.get_heap_size(), BYTES_STR.len());

    let mut bytes_mut = bytes::BytesMut::from(BYTES_STR);
    assert_eq!(bytes_mut.get_heap_size(), BYTES_STR.len());
    bytes_mut.truncate(0);
    assert_eq!(bytes_mut.get_heap_size(), 0);
}

#[test]
fn compact_str() {
    const STR: &str = "Hello world";
    const LONG_STR: &str = "A much looooonger string that exceeds 24 bytes.";

    let value = compact_str::CompactString::from(STR);
    assert_eq!(value.get_heap_size(), 0);

    let mut value = compact_str::CompactString::from(LONG_STR);
    assert_eq!(value.get_heap_size(), value.capacity());

    value.shrink_to_fit();

    assert_eq!(value.len(), value.capacity());
    assert_eq!(value.get_heap_size(), LONG_STR.len());
}

#[test]
fn test_dashmap() {
    const VALUE_STR: &str = "Hello world";

    let map: dashmap::DashMap<i32, String> = dashmap::DashMap::new();
    assert_eq!(map.get_heap_size(), 0);
    map.insert(0, String::from(VALUE_STR));
    assert!(map.get_heap_size() >= size_of::<(i32, String)>() + VALUE_STR.len());

    let set: dashmap::DashSet<String> = dashmap::DashSet::new();
    assert_eq!(set.get_heap_size(), 0);
    set.insert(String::from(VALUE_STR));
    assert!(set.get_heap_size() >= size_of::<String>() + VALUE_STR.len());
}

#[test]
fn test_half() {
    let a = half::f16::from_f32(1.5);
    assert_eq!(a.get_heap_size(), 0);
    assert_eq!(a.get_size(), size_of::<half::f16>());

    let b = half::bf16::from_f32(1.5);
    assert_eq!(b.get_heap_size(), 0);
    assert_eq!(b.get_size(), size_of::<half::bf16>());
}

#[test]
fn hashbrown() {
    use std::hash::{BuildHasher, RandomState};

    const VALUE_STR: &str = "A very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonng string.";

    let hasher = RandomState::new();

    let mut map = hashbrown::HashTable::new();
    assert_eq!(map.get_heap_size(), 0);
    map.insert_unique(
        hasher.hash_one(VALUE_STR),
        String::from(VALUE_STR),
        |value| hasher.hash_one(value),
    );
    assert!(map.get_heap_size() >= size_of::<String>() + VALUE_STR.len());

    let mut map = hashbrown::HashMap::<i32, String, RandomState>::default();
    assert_eq!(map.get_heap_size(), 0);
    map.insert(0, String::from(VALUE_STR));
    assert!(map.get_heap_size() >= size_of::<(i32, String)>() + VALUE_STR.len());

    let mut set = hashbrown::HashSet::<String, RandomState>::default();
    assert_eq!(set.get_heap_size(), 0);
    set.insert(String::from(VALUE_STR));
    assert!(set.get_heap_size() >= size_of::<String>() + VALUE_STR.len());
}

#[test]
fn smallvec() {
    const ITEM_STR: &str = "Hello world";
    let mut vec = smallvec::SmallVec::<[String; 2]>::from([String::new(), String::from(ITEM_STR)]);

    assert_eq!(vec.get_heap_size(), ITEM_STR.len());
    vec.push(String::new());

    assert_eq!(
        vec.get_heap_size(),
        ITEM_STR.len() + std::mem::size_of::<String>() * vec.capacity()
    );

    vec.shrink_to_fit();

    assert_eq!(
        vec.get_heap_size(),
        ITEM_STR.len() + std::mem::size_of::<String>() * 3
    );
}

#[test]
fn thin_vec() {
    const ITEM_STR: &str = "Hello world";

    assert_eq!(thin_vec::ThinVec::<String>::default().get_heap_size(), 0);

    let mut vec = thin_vec::ThinVec::<String>::from([String::new(), String::from(ITEM_STR)]);
    assert_eq!(
        vec.get_heap_size(),
        ITEM_STR.len()
            + std::mem::size_of::<String>() * vec.capacity()
            + std::mem::size_of::<usize>() * 2
    );

    vec.shrink_to_fit();

    assert_eq!(
        vec.get_heap_size(),
        ITEM_STR.len()
            + std::mem::size_of::<String>() * vec.len()
            + std::mem::size_of::<usize>() * 2
    );
}

#[test]
fn test_indexmap() {
    use std::hash::RandomState;

    const VALUE_STR: &str = "A very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonng string.";

    let hasher = RandomState::new();

    let mut map = indexmap::IndexMap::with_capacity_and_hasher(1, hasher);
    // The allocation is `capacity * size_of::<(K, V)>()`, which is pointer-width
    // dependent (40 bytes on 64-bit, 20 on 32-bit) — don't hard-code it.
    assert_eq!(
        map.get_heap_size(),
        map.capacity() * size_of::<(&'static str, String)>()
    );
    map.insert(VALUE_STR, String::from(VALUE_STR));
    assert!(map.get_heap_size() >= size_of::<(&'static str, String)>() + VALUE_STR.len());

    let mut map = indexmap::IndexMap::<i32, String, RandomState>::default();
    assert_eq!(map.get_heap_size(), 0);
    map.insert(0, String::from(VALUE_STR));
    assert!(map.get_heap_size() >= size_of::<(i32, String)>() + VALUE_STR.len());

    let mut set = indexmap::IndexSet::<String, RandomState>::default();
    assert_eq!(set.get_heap_size(), 0);
    set.insert(String::from(VALUE_STR));
    assert!(set.get_heap_size() >= size_of::<String>() + VALUE_STR.len());
}

#[test]
fn test_parking_lot() {
    use std::sync::Arc;

    const S: &str = "Hello world";

    let v = vec![String::from(S)];
    let expected = size_of::<String>() + S.len();

    let m = parking_lot::Mutex::new(v.clone());
    assert_eq!(m.get_heap_size(), expected);

    let r = parking_lot::RwLock::new(v);
    assert_eq!(r.get_heap_size(), expected);

    let tracker = Arc::new(parking_lot::Mutex::new(StandardTracker::new()));
    let (size, _) = r.get_heap_size_with_tracker(tracker);
    assert_eq!(size, expected);
}

#[test]
fn test_ordermap() {
    use std::hash::RandomState;

    const VALUE_STR: &str = "A very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonng string.";

    let hasher = RandomState::new();

    let mut map = ordermap::OrderMap::with_capacity_and_hasher(1, hasher);
    // The allocation is `capacity * size_of::<(K, V)>()`, which is pointer-width
    // dependent (40 bytes on 64-bit, 20 on 32-bit) — don't hard-code it.
    assert_eq!(
        map.get_heap_size(),
        map.capacity() * size_of::<(&'static str, String)>()
    );
    map.insert(VALUE_STR, String::from(VALUE_STR));
    assert!(map.get_heap_size() >= size_of::<(&'static str, String)>() + VALUE_STR.len());

    let mut map = ordermap::OrderMap::<i32, String, RandomState>::default();
    assert_eq!(map.get_heap_size(), 0);
    map.insert(0, String::from(VALUE_STR));
    assert!(map.get_heap_size() >= size_of::<(i32, String)>() + VALUE_STR.len());

    let mut set = ordermap::OrderSet::<String, RandomState>::default();
    assert_eq!(set.get_heap_size(), 0);
    set.insert(String::from(VALUE_STR));
    assert!(set.get_heap_size() >= size_of::<String>() + VALUE_STR.len());
}

#[test]
fn test_roaring_bitmap() {
    // Empty bitmap: no containers, no allocations.
    let empty = roaring::RoaringBitmap::new();
    assert_eq!(empty.get_heap_size(), 0);

    // Dense run that fits in one array container: should equal what
    // `statistics()` reports for an array container — `capacity * 4`.
    let dense: roaring::RoaringBitmap = (1..100).collect();
    let stats = dense.statistics();
    assert_eq!(stats.n_containers, 1);
    assert_eq!(stats.n_array_containers, 1);
    assert_eq!(dense.get_heap_size() as u64, stats.n_bytes_array_containers);
    assert!(dense.get_heap_size() > 0);

    // Bitmap container kicks in past ~4096 entries; verify we count
    // the fixed 8 KiB allocation. Equality against the sum of all three
    // flavor totals catches regressions that drop a flavor.
    let wide: roaring::RoaringBitmap = (0..5000).collect();
    let stats = wide.statistics();
    assert!(stats.n_bitset_containers >= 1);
    assert_eq!(
        wide.get_heap_size() as u64,
        stats.n_bytes_array_containers
            + stats.n_bytes_bitset_containers
            + stats.n_bytes_run_containers
    );

    // Run containers are produced by `optimize()` when a container has
    // long dense runs. Verify we count run-container bytes too.
    let mut run = (0..10_000).collect::<roaring::RoaringBitmap>();
    run.optimize();
    let stats = run.statistics();
    assert!(stats.n_run_containers >= 1);
    assert_eq!(
        run.get_heap_size() as u64,
        stats.n_bytes_array_containers
            + stats.n_bytes_bitset_containers
            + stats.n_bytes_run_containers
    );

    // Tracker-variant: threading a real tracker through must return the
    // same size as the no-tracker path.
    let tracker = StandardTracker::new();
    let (size, _) = wide.get_heap_size_with_tracker(tracker);
    assert_eq!(size, wide.get_heap_size());
}

#[test]
fn test_roaring_treemap() {
    let empty = roaring::RoaringTreemap::new();
    assert_eq!(empty.get_heap_size(), 0);

    // Two high-32-bit partitions → two inner RoaringBitmaps. The second
    // partition holds many values so the per-bitmap heap size is
    // non-trivial (exercises propagation through the BTreeMap walk).
    let mut tm = roaring::RoaringTreemap::new();
    tm.insert(1);
    for v in 0..5000u64 {
        tm.insert((1u64 << 32) + v);
    }
    let mut bitmap_count = 0;
    let mut expected_inner: usize = 0;
    for (_, bitmap) in tm.bitmaps() {
        bitmap_count += 1;
        expected_inner +=
            bitmap.get_heap_size() + size_of::<u32>() + size_of::<roaring::RoaringBitmap>();
    }
    assert_eq!(bitmap_count, 2);
    assert_eq!(tm.get_heap_size(), expected_inner);

    // Tracker-variant: threading a tracker through the BTreeMap walk
    // must match the no-tracker path.
    let tracker = StandardTracker::new();
    let (size, _) = tm.get_heap_size_with_tracker(tracker);
    assert_eq!(size, tm.get_heap_size());
}

#[test]
fn test_orx_concurrent_vec() {
    use orx_concurrent_vec::{ConcurrentElement, ConcurrentVec};

    const S: &str = "Hello world";

    let empty: ConcurrentVec<u32> = ConcurrentVec::new();
    assert_eq!(
        empty.get_heap_size(),
        empty.capacity() * size_of::<ConcurrentElement<u32>>()
    );

    let vec: ConcurrentVec<u32> = ConcurrentVec::new();
    vec.extend(0u32..16);
    assert_eq!(
        vec.get_heap_size(),
        vec.capacity() * size_of::<ConcurrentElement<u32>>()
    );

    let vec: ConcurrentVec<String> = ConcurrentVec::new();
    vec.push(String::from(S));
    vec.push(String::from(S));
    let expected = vec.capacity() * size_of::<ConcurrentElement<String>>() + S.len() * 2;
    assert_eq!(vec.get_heap_size(), expected);

    let tracker = StandardTracker::new();
    let (size, _) = vec.get_heap_size_with_tracker(tracker);
    assert_eq!(size, vec.get_heap_size());
}

#[test]
fn test_portable_atomic() {
    // Atomics are stack-only, so heap size is 0 and total size equals the
    // type's own size. `AtomicU64`/`AtomicI64`/`AtomicU128`/`AtomicI128` are
    // the whole point of the feature: they exist here even on targets without
    // native 64/128-bit atomics.
    let u64 = portable_atomic::AtomicU64::new(1);
    assert_eq!(u64.get_heap_size(), 0);
    assert_eq!(u64.get_size(), size_of::<portable_atomic::AtomicU64>());

    let i64 = portable_atomic::AtomicI64::new(-1);
    assert_eq!(i64.get_heap_size(), 0);
    assert_eq!(i64.get_size(), size_of::<portable_atomic::AtomicI64>());

    let u128 = portable_atomic::AtomicU128::new(1);
    assert_eq!(u128.get_heap_size(), 0);
    assert_eq!(u128.get_size(), size_of::<portable_atomic::AtomicU128>());

    let b = portable_atomic::AtomicBool::new(true);
    assert_eq!(b.get_heap_size(), 0);
    assert_eq!(b.get_size(), size_of::<portable_atomic::AtomicBool>());
}

#[test]
fn uuid() {
    for value in [uuid::Uuid::nil(), uuid::Uuid::from_u128(0x1234)] {
        assert_eq!(value.get_heap_size(), 0);
        assert_eq!(value.get_size(), size_of::<uuid::Uuid>());
        assert_eq!(value.get_size(), 16);

        let (size, tracker) = value.get_heap_size_with_tracker(NoTracker::new(false));
        assert_eq!(size, 0);
        assert!(!tracker.answer());
    }
}