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

Optimize find referenced elements #167

Merged

Conversation

nthykier
Copy link
Contributor

The biggest win (at least in my test case) is the "findReferencedElements: Handle referencingProps separately", which avoids a large number of unnecessary string operations.

But there is also a large potential in "Avoid recomputing findReferencedElements in removeUnusedDefs" for any SVG that need a number of calls to "removeUnreferencedElements" or where "removeUnusedDefs" recurses a lot of times. My particular test case does not seem to trigger any of that though (with a total of one call to removeUnreferencedElements)

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Split the handling of referencingProps into a separate loop that calls
findReferencingProperty directly.  This saves a bunch of "make list,
join list, append to another list and eventually split text into two
elements" operations.

This gives approximately 10% faster runtimes on 341 kB flamegraph
generated by the "nytprof" Perl profiler.

Signed-off-by: Niels Thykier <niels@thykier.net>
The removeUnusedDefs function does not actually remove anything (that
is left for its callers to do).  This implies that
findReferencedElements will return the same value before, during and
after a call to removeUnusedDefs.  Therefore, we can reuse the value
from findReferencedElements when recursing into child nodes.

Signed-off-by: Niels Thykier <niels@thykier.net>
It was a dict with a two element list a la:

  {
    "id1": [len(nodeListX), nodeListX]],
    "id2": [len(nodeListY), nodeListY]],
    ...
  }

This can trivially be simplified to:

  {
    "id1": nodeListX,
    "id2": nodeListY,
    ...
  }

The two call-sites that actually needs the length (e.g. to sort by how
often the id is used) can trivially compute that via a call to "len".

All other call sites either just need to tell if an ID is used at all
or work the nodes referencing the id (e.g. to remap the id).  The
former are unaffected by this change and the latter can now avoid a
layer of indirection.

This refactoring has negiable changes to the runtime and probably also
to memory (not tested, but it is a minor constant improvement per
referenced id).

Signed-off-by: Niels Thykier <niels@thykier.net>
@codecov-io
Copy link

codecov-io commented Feb 17, 2018

Codecov Report

Merging #167 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #167      +/-   ##
==========================================
- Coverage   91.49%   91.48%   -0.01%     
==========================================
  Files           5        5              
  Lines        2139     2138       -1     
==========================================
- Hits         1957     1956       -1     
  Misses        182      182
Impacted Files Coverage Δ
scour/scour.py 93.14% <100%> (-0.01%) ⬇️

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 7249ae8...258c782. Read the comment docs.

@Ede123
Copy link
Member

Ede123 commented Feb 17, 2018

Perfect! 6% speedup also confirmed in a class of files I'm currently working with.

@Ede123 Ede123 merged commit c54a723 into scour-project:master Feb 17, 2018
@nthykier
Copy link
Contributor Author

Thanks for the review and merge. :)

@nthykier nthykier deleted the optimize-find-referenced-elements branch February 17, 2018 13:32
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.

None yet

3 participants