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

Avoid calling deepcopy in from_dict methods when unnecessary #454

Merged
merged 3 commits into from
Jun 17, 2021

Conversation

lossyrob
Copy link
Member

Related Issue(s): #453

Description:
This PR adds a parameter to the STACObject.from_dict method and implementations named preserve_dict. If this is True, the dict that is passed in as a parameter will not be mutated during the operation - which is achieved by making a deep copy of dict parameter in the implementing methods. If preserve_dict is False, the incoming dict is mutated and a deepcopy is avoided. The default of this parameter is True, so that users can avoid a situation where their parameters are mutated unsuspectedly. In situations where the dict is a transient encoding of the STAC object, users should use preserve_dict=False to avoid incurring the performance cost of the deepcopy.

preserve_dict is set to False for methods that read a STAC object from a file, as there's no need to avoid mutating the dict in those cases.

This should help address some of the performance issues in described in stac-utils/pystac-client#49, as pystac-client will be able to use preserve_dict=False in the ItemCollection.from_dict method

PR Checklist:

  • Code is formatted (run pre-commit run --all-files)
  • Tests pass (run scripts/test)
  • Documentation has been updated to reflect changes, if applicable
  • This PR maintains or improves overall codebase code coverage.
  • Changes are added to the CHANGELOG. See the docs for information about adding to the changelog.

@lossyrob lossyrob requested a review from duckontheweb June 17, 2021 23:12
Copy link
Contributor

@duckontheweb duckontheweb left a comment

Choose a reason for hiding this comment

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

👍 , thanks for getting this in. Just 1 minor docstring change and a possible change to some assertions that you can take or leave.

pystac/stac_object.py Outdated Show resolved Hide resolved
tests/test_catalog.py Show resolved Hide resolved
@lossyrob lossyrob changed the title Fix/rde/avoid deepcopy Avoid calling deepcopy in from_dict methods when unnecessary Jun 17, 2021
Co-authored-by: Jon Duckworth <jon@radiant.earth>
@codecov-commenter
Copy link

Codecov Report

Merging #454 (68df9bd) into main (2739661) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #454   +/-   ##
=======================================
  Coverage   91.48%   91.48%           
=======================================
  Files          40       40           
  Lines        5237     5240    +3     
=======================================
+ Hits         4791     4794    +3     
  Misses        446      446           
Impacted Files Coverage Δ
pystac/catalog.py 94.72% <100.00%> (+0.01%) ⬆️
pystac/collection.py 95.51% <100.00%> (+0.01%) ⬆️
pystac/item.py 96.99% <100.00%> (+<0.01%) ⬆️
pystac/stac_io.py 77.19% <100.00%> (ø)
pystac/stac_object.py 94.70% <100.00%> (ø)

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 2739661...68df9bd. Read the comment docs.

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.

3 participants