Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor use of deepcopy by docval #238

Merged
merged 5 commits into from
Jan 7, 2020
Merged

Refactor use of deepcopy by docval #238

merged 5 commits into from
Jan 7, 2020

Conversation

rly
Copy link
Contributor

@rly rly commented Jan 7, 2020

Fix #237

This PR improves runtime of hdmf tests by about 20% and runtime of pynwb tests by about 25%.

The docval list of dictionaries does not need to be deepcopied on every call to a function with docval. The list of dictionaries consists of just strings, types, tuples of strings and types, and critically here, default values. If the default values are not properly copied when used to set the values of missing arguments, missing arguments will all share the same list/dict default value, which is bad.

Rather than deepcopy the whole list of dictionaries, just deepcopy the default value when it is used to set the value of a missing argument.

This PR also removes a few other unnecessary/redundant calls to deepcopy regarding docval.

@@ -411,7 +411,6 @@ def foo(self, **kwargs):
rtype = options.pop('rtype', None)
is_method = options.pop('is_method', True)
allow_extra = options.pop('allow_extra', False)
val_copy = __sort_args(_copy.deepcopy(validator))
Copy link
Contributor Author

@rly rly Jan 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sorting performed on this line is unnecessary given sorting in lines 429-432 (old line numbers) below. The deepcopy is also unnecessary given the other changes.

@codecov
Copy link

codecov bot commented Jan 7, 2020

Codecov Report

Merging #238 into dev will decrease coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev     #238      +/-   ##
==========================================
- Coverage   70.48%   70.43%   -0.05%     
==========================================
  Files          32       32              
  Lines        5933     5923      -10     
  Branches     1390     1388       -2     
==========================================
- Hits         4182     4172      -10     
  Misses       1320     1320              
  Partials      431      431
Impacted Files Coverage Δ
src/hdmf/build/map.py 71.92% <100%> (ø) ⬆️
src/hdmf/utils.py 92.03% <100%> (-0.19%) ⬇️
src/hdmf/spec/namespace.py 74.24% <100%> (ø) ⬆️
src/hdmf/spec/spec.py 68.2% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c4a53f9...2aec033. Read the comment docs.

@@ -225,7 +224,7 @@ def __parse_args(validator, args, kwargs, enforce_type=True, enforce_shape=True,
ret[argname] = args[argsi]
argsi += 1
else:
ret[argname] = arg['default']
ret[argname] = _copy.deepcopy(arg['default'])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the key changes -- only default values are deep-copied, and only when they are used.

@@ -436,7 +423,7 @@ def dec(func):
def func_call(*args, **kwargs):
self = args[0]
parsed = __parse_args(
_copy.deepcopy(loc_val),
loc_val,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the key changes. Previously, this list of dictionaries (the docval specification/validator) was deepcopied on every call to a function with docval.

@rly rly requested a review from a team January 7, 2020 00:17
@rly rly merged commit db1b1ad into dev Jan 7, 2020
@rly rly deleted the docval_deepcopy branch January 7, 2020 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

docval uses a significant portion of runtime
2 participants