Encoder indent suppression: str.replace → re.sub (fixes #170)#171
Merged
dave-doty merged 1 commit intoUC-Davis-molecular-computing:devfrom Mar 17, 2021
Merged
Conversation
…cular-computing#170) Using re.sub (re is already imported), along with a replacement function that pulls from the replacement map, avoids repeated string replacements within Python and significantly speeds up encoding for large files. Note that I think this will raise a KeyError if the user has something with a name fitting "@@(\d+)@@" where the number is larger than the largest unique_id, and will corrupt the file if it is not. However, this is similar to the current situation.
dave-doty
approved these changes
Mar 17, 2021
Member
dave-doty
left a comment
There was a problem hiding this comment.
I don't quite understand what's going on in this new line of code, but it doesn't seem to break any unit tests, so I'll take you word that it works.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Using re.sub (re is already imported), along with a replacement function that pulls from the replacement map, avoids repeated string replacements within Python and significantly speeds up encoding for large files. Note that I think this will raise a KeyError if the user has something with a name fitting "@@(\d+)@@" where the number is larger than the largest unique_id, and will corrupt the file if it is not. However, this is similar to the current situation, where I think it will never raise an error, but could corrupt the file.
This also changes the type of _replacement_map to avoid a mypy warning: it should be a correct narrowing of the value from Any to str, however.
Trying this with the design I'm trying to repeatedly save, the change brings write time down from 20 seconds to a little over 1 second.