MCPcopy Create free account
hub / github.com/StackStorm/st2 / fast_deepcopy_dict

Function fast_deepcopy_dict

st2common/st2common/util/deep_copy.py:32–61  ·  view source on GitHub ↗

Perform a fast deep copy of the provided value. This function is designed primary to operate on values of a simple type (think JSON types - dicts, lists, arrays, strings, ints). It's up to 10x faster compared to copy.deepcopy(). In case the provided value contains non-simple

(value, fall_back_to_deepcopy=True)

Source from the content-addressed store, hash-verified

30
31
32def fast_deepcopy_dict(value, fall_back_to_deepcopy=True):
33 """
34 Perform a fast deep copy of the provided value.
35
36 This function is designed primary to operate on values of a simple type (think JSON types -
37 dicts, lists, arrays, strings, ints).
38
39 It's up to 10x faster compared to copy.deepcopy().
40
41 In case the provided value contains non-simple types, we simply fall back to "copy.deepcopy()".
42 This means that we can still use it on values which sometimes, but not always contain complex
43 types - in that case, when value doesn't contain complex types we will perform much faster copy
44 and when it does, we will simply fall back to copy.deepcopy().
45
46 :param fall_back_to_deepcopy: True to fall back to copy.deepcopy() in case we fail to fast deep
47 copy the value because it contains complex types or similar
48 :type fall_back_to_deepcopy: ``bool``
49 """
50 # NOTE: ujson / orjson round-trip is up to 10 times faster on smaller and larger dicts compared
51 # to copy.deepcopy(), but it has some edge cases with non-simple types such as datetimes, class
52 # instances, etc.
53 try:
54 value = orjson.loads(orjson.dumps(value, default=default))
55 except (OverflowError, ValueError, TypeError) as e:
56 if not fall_back_to_deepcopy:
57 raise e
58
59 value = copy.deepcopy(value)
60
61 return value

Callers 15

restore_varsMethod · 0.90
_re_run_live_actionMethod · 0.90
postMethod · 0.90
add_specMethod · 0.90
process_attribute_valueFunction · 0.90
respondFunction · 0.90
get_params_viewFunction · 0.90
to_modelMethod · 0.90
mask_secret_parametersFunction · 0.90
mask_inquiry_responseFunction · 0.90
escape_charsFunction · 0.90

Calls

no outgoing calls

Tested by 1