@@ -5,6 +5,7 @@ use std::{
5
5
ops:: { Deref , DerefMut } ,
6
6
} ;
7
7
8
+ use deepsize:: DeepSizeOf ;
8
9
use moka:: sync:: Cache ;
9
10
use std:: sync:: Arc ;
10
11
use trustify_common:: { cpe:: Cpe , purl:: Purl } ;
@@ -25,7 +26,7 @@ impl fmt::Display for AnalysisStatus {
25
26
}
26
27
}
27
28
28
- #[ derive( Debug , Clone , PartialEq , Eq , ToSchema , serde:: Serialize ) ]
29
+ #[ derive( Debug , Clone , PartialEq , Eq , ToSchema , serde:: Serialize , DeepSizeOf ) ]
29
30
pub struct PackageNode {
30
31
pub sbom_id : String ,
31
32
pub node_id : String ,
@@ -37,33 +38,6 @@ pub struct PackageNode {
37
38
pub document_id : String ,
38
39
pub product_name : String ,
39
40
pub product_version : String ,
40
- pub approximate_memory_size : u32 ,
41
- }
42
-
43
- impl PackageNode {
44
- pub ( crate ) fn set_approximate_memory_size ( & self ) -> PackageNode {
45
- // Is there a better way to do this?
46
- let size = size_of :: < PackageNode > ( )
47
- + self . sbom_id . len ( )
48
- + self . node_id . len ( )
49
- + self . purl . iter ( ) . fold ( 0 , |acc, purl|
50
- // use the json string length as an approximation of the memory size
51
- acc + serde_json:: to_string ( purl) . unwrap_or_else ( |_| "" . to_string ( ) ) . len ( ) )
52
- + self . cpe . iter ( ) . fold ( 0 , |acc, cpe|
53
- // use the json string length as an approximation of the memory size
54
- acc + serde_json:: to_string ( cpe) . unwrap_or_else ( |_| "" . to_string ( ) ) . len ( ) )
55
- + self . name . len ( )
56
- + self . version . len ( )
57
- + self . published . len ( )
58
- + self . document_id . len ( )
59
- + self . product_name . len ( )
60
- + self . product_version . len ( ) ;
61
-
62
- PackageNode {
63
- approximate_memory_size : size. try_into ( ) . unwrap_or ( u32:: MAX ) ,
64
- ..self . clone ( )
65
- }
66
- }
67
41
}
68
42
69
43
impl fmt:: Display for PackageNode {
@@ -188,20 +162,26 @@ pub struct GraphMap {
188
162
}
189
163
190
164
#[ allow( clippy:: ptr_arg) ] // &String is required by Cache::builder().weigher() method
191
- fn weigher ( key : & String , value : & Arc < PackageGraph > ) -> u32 {
192
- let mut result = key. len ( ) ;
193
- for n in value. raw_nodes ( ) {
194
- result += n. weight . approximate_memory_size as usize ;
195
- }
196
- result += size_of_val ( value. raw_edges ( ) ) ;
197
- result. try_into ( ) . unwrap_or ( u32:: MAX )
165
+ fn size_of_graph_entry ( key : & String , value : & Arc < PackageGraph > ) -> u32 {
166
+ (
167
+ key. deep_size_of ( )
168
+ + value. as_ref ( ) . deep_size_of ( )
169
+ // Also add in some entry overhead of the cache entry
170
+ + 20
171
+ // todo: find a better estimate for the the moka ValueEntry
172
+ )
173
+ . try_into ( )
174
+ . unwrap_or ( u32:: MAX )
198
175
}
199
176
200
177
impl GraphMap {
201
178
// Create a new instance of GraphMap
202
179
pub fn new ( cap : u64 ) -> Self {
203
180
GraphMap {
204
- map : Cache :: builder ( ) . weigher ( weigher) . max_capacity ( cap) . build ( ) ,
181
+ map : Cache :: builder ( )
182
+ . weigher ( size_of_graph_entry)
183
+ . max_capacity ( cap)
184
+ . build ( ) ,
205
185
}
206
186
}
207
187
0 commit comments