v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
std-object-sizes.h
Go to the documentation of this file.
1// Copyright 2023 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_WASM_STD_OBJECT_SIZES_H_
6#define V8_WASM_STD_OBJECT_SIZES_H_
7
8#if !V8_ENABLE_WEBASSEMBLY
9#error This header should only be included if WebAssembly is enabled.
10#endif // !V8_ENABLE_WEBASSEMBLY
11
12#include <map>
13#include <unordered_map>
14#include <unordered_set>
15#include <vector>
16
17#include "include/v8config.h"
18#include "src/base/vector.h"
19
20namespace v8::internal::wasm {
21
22// These helpers are used to estimate the memory consumption of standard
23// data structures off the managed heap.
24// The size of the container itself is not included here, because it's
25// typically included in the size of the containing element.
26
27template <typename T>
28inline size_t ContentSize(const std::vector<T>& vector) {
29 // We use {capacity()} rather than {size()} because we want to compute
30 // actual memory consumption.
31 return vector.capacity() * sizeof(T);
32}
33
34template <typename T>
35inline size_t ContentSize(const base::OwnedVector<T>& vector) {
36 return vector.size() * sizeof(T);
37}
38
39template <typename Key, typename T>
40inline size_t ContentSize(const std::map<Key, T>& map) {
41 // Very rough lower bound approximation: two internal pointers per entry.
42 return map.size() * (sizeof(Key) + sizeof(T) + 2 * sizeof(void*));
43}
44
45template <typename Key, typename T, typename Hash>
46inline size_t ContentSize(const std::unordered_map<Key, T, Hash>& map) {
47 // Very rough lower bound approximation: two internal pointers per entry.
48 size_t raw = map.size() * (sizeof(Key) + sizeof(T) + 2 * sizeof(void*));
49 // In the spirit of computing lower bounds of definitely-used memory,
50 // we assume a 75% fill ratio.
51 return raw * 4 / 3;
52}
53
54template <typename T, typename Hash>
55inline size_t ContentSize(const std::unordered_set<T, Hash>& set) {
56 // Very rough lower bound approximation: two internal pointers per entry.
57 size_t raw = set.size() * (sizeof(T) + 2 * sizeof(void*));
58 // In the spirit of computing lower bounds of definitely-used memory,
59 // we assume a 75% fill ratio.
60 return raw * 4 / 3;
61}
62
63// To make it less likely for size estimation functions to become outdated
64// when the classes they're responsible for change, we insert static asserts
65// about the respective class's size into them to at least catch some possible
66// future modifications. Since object sizes are toolchain specific, we define
67// restrictions here under which we enable these checks.
68// When one of these checks fails, that probably means you've added fields to
69// a class guarded by it. Update the respective EstimateCurrentMemoryConsumption
70// function accordingly, and then update the check's expected size.
71#if V8_TARGET_ARCH_X64 && defined(__clang__) && V8_TARGET_OS_LINUX && \
72 !V8_USE_ADDRESS_SANITIZER && !V8_USE_MEMORY_SANITIZER && defined(DEBUG) && \
73 V8_COMPRESS_POINTERS && !defined(V8_GC_MOLE) && defined(_LIBCPP_VERSION)
74#define UPDATE_WHEN_CLASS_CHANGES(classname, size) \
75 static_assert(sizeof(classname) == size, \
76 "Update {EstimateCurrentMemoryConsumption} when adding " \
77 "fields to " #classname)
78#else
79#define UPDATE_WHEN_CLASS_CHANGES(classname, size) (void)0
80#endif
81
82} // namespace v8::internal::wasm
83
84#endif // V8_WASM_STD_OBJECT_SIZES_H_
#define T
size_t ContentSize(const std::vector< T > &vector)