HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
estimateSize.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_EXEC_VDF_ESTIMATE_SIZE_H
8 #define PXR_EXEC_VDF_ESTIMATE_SIZE_H
9 
10 /// \file
11 
12 #include "pxr/pxr.h"
13 
15 
16 #include <memory>
17 #include <vector>
18 
20 
21 /// Estimate the memory footprint of instance \p t of type T.
22 ///
23 /// This function estimates the memory footprint of an instance of type T.
24 /// Internally this is used to total up cache sizes, and give the system an
25 /// opportunity to limit the memory used for execution caches.
26 ///
27 /// By default, this function invokes the sizeof() operator for type T. The
28 /// returned value is not a good estimate of the true memory footprint, if
29 /// instances of T allocate heap memory.
30 ///
31 /// Plugin writers can *overload* (not specialize) VdfEstimateSize for their own
32 /// types in order to provide a better estimate. Note that in order for ADL to
33 /// be guaranteed to find the new overload, the pattern is to declare it as an
34 /// inline friend within the namespace of the class itself. Further, not
35 /// polluting the unqualified namespace with these overloads also results in
36 /// more succinct compiler errors if overload resolution were to fail.
37 ///
38 /// Please follow the pattern established in this example:
39 ///
40 /// ```{.cpp}
41 /// struct MyType {
42 /// uint8_t *data;
43 /// size_t numData;
44 ///
45 /// friend size_t
46 /// VdfEstimateSize(const MyType &t)
47 /// {
48 /// return sizeof(MyTpe) + (sizeof(uint8_t) * t.numData);
49 /// }
50 /// };
51 /// ```
52 ///
53 template < typename T >
54 inline size_t
55 VdfEstimateSize(const T &)
56 {
57  return sizeof(T);
58 }
59 
60 /// Overload for TfSmallVector<T, N>
61 ///
62 template < typename T, uint32_t N >
63 inline size_t
65 {
66  // It would be more accurate the iterate over v and estimate the size of
67  // each element of v, but we are optimizing for performance rather than
68  // accuracy for now.
69  const size_t numExternal = (v.capacity() > N) ? v.capacity() : 0;
70  const size_t elementSize = v.empty()
71  ? sizeof(T) : VdfEstimateSize(v.front());
72  return sizeof(TfSmallVector<T, N>) + numExternal * elementSize;
73 }
74 
75 /// Overload for std::vector<T>
76 ///
77 template < typename T, typename A >
78 inline size_t
79 VdfEstimateSize(const std::vector<T, A> &v)
80 {
81  // It would be more accurate the iterate over v and estimate the size
82  // of each element of v, but we are optimizing for performance rather
83  // than accuracy for now.
84  const size_t elementSize = v.empty()
85  ? sizeof(T) : VdfEstimateSize(v.front());
86  return sizeof(std::vector<T, A>) + v.capacity() * elementSize;
87 }
88 
89 /// Overload for std::shared_ptr<T>
90 ///
91 template < typename T >
92 inline size_t
93 VdfEstimateSize(const std::shared_ptr<T> &p)
94 {
95  return sizeof(std::shared_ptr<T>) + (p ? VdfEstimateSize(*p) : 0);
96 }
97 
99 
100 #endif /* PXR_EXEC_VDF_ESTIMATE_SIZE_H */
size_type capacity() const
Definition: smallVector.h:617
const GLdouble * v
Definition: glcorearb.h:837
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
reference front()
Definition: smallVector.h:699
bool empty() const
Definition: smallVector.h:608
PXR_NAMESPACE_OPEN_SCOPE size_t VdfEstimateSize(const T &)
Definition: estimateSize.h:55
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GA_API const UT_StringHolder N