@@ -2186,10 +2186,23 @@ namespace jsoncons {
21862186 // Use callback for precise allocated size
21872187 mem_size += get_usable_size (static_cast <const void *>(data_ptr));
21882188
2189- // Add all array elements to stack for processing (iterative, not recursive)
2189+ // Add array elements to stack for processing
2190+ // Optimization: only add elements that have dynamic memory
21902191 for (const auto & elem : arr)
21912192 {
2192- stack.push_back (&elem);
2193+ auto kind = elem.storage_kind ();
2194+ // Skip inline storage types (primitives, short strings, empty objects)
2195+ if (kind != json_storage_kind::null &&
2196+ kind != json_storage_kind::boolean &&
2197+ kind != json_storage_kind::int64 &&
2198+ kind != json_storage_kind::uint64 &&
2199+ kind != json_storage_kind::half_float &&
2200+ kind != json_storage_kind::float64 &&
2201+ kind != json_storage_kind::short_str &&
2202+ kind != json_storage_kind::empty_object)
2203+ {
2204+ stack.push_back (&elem);
2205+ }
21932206 }
21942207 }
21952208 break ;
@@ -2220,8 +2233,22 @@ namespace jsoncons {
22202233 std::size_t key_heap_size = get_usable_size (static_cast <const void *>(key_data));
22212234 mem_size += key_heap_size;
22222235
2223- // Add value to stack for processing (iterative, not recursive)
2224- stack.push_back (&member.value ());
2236+ // Add value to stack for processing
2237+ // Optimization: only add values that have dynamic memory
2238+ const auto & value = member.value ();
2239+ auto kind = value.storage_kind ();
2240+ // Skip inline storage types (primitives, short strings, empty objects)
2241+ if (kind != json_storage_kind::null &&
2242+ kind != json_storage_kind::boolean &&
2243+ kind != json_storage_kind::int64 &&
2244+ kind != json_storage_kind::uint64 &&
2245+ kind != json_storage_kind::half_float &&
2246+ kind != json_storage_kind::float64 &&
2247+ kind != json_storage_kind::short_str &&
2248+ kind != json_storage_kind::empty_object)
2249+ {
2250+ stack.push_back (&value);
2251+ }
22252252 }
22262253 break ;
22272254 }
0 commit comments