linkml / linkml-runtime

Runtime support for linkml generated models
https://linkml.io/linkml/
Creative Commons Zero v1.0 Universal
22 stars 21 forks source link

Perf: Remove unnecessary `deepcopy` calls in `Schemaview.induced_slot` #296

Closed sneakers-the-rat closed 5 months ago

sneakers-the-rat commented 5 months ago

Following up on https://github.com/linkml/linkml/issues/1604

This is one of the largest sources of slow performance in the non-graph-based generators.

Currently, when I run the test suite, it takes me 50 minutes, which is entirely too long imo - we want tested contributions, but if i have to wait 50 minutes before submitting a PR or between every change then the odds start to really climb that i'm just going to forget about it.

This gets us down to 40 minutes essentially for free (and when using pydanticgen makes a ~60s generation process into ~2-3s).

First: we can remove the deepcopy at the end - that's wholly unnecessary, since induced_slot is already deepcopied earlier in the method. we don't need to protect something that will die immediately when the method ends.

Second, we also don't need the deepcopy in the middle of the method:

so we shouldn't need deepcopy in induced_slot since the thing we draw from should be cached and copied already - future calls to get_slot should get the cached copy even if we mutate it downstream. I replaced it with a regular copy since that's much faster if it's necessary at all.

Third: Avoid double-calling get_slot with differing call signature since the first call is only necessary if we take the first leg of the if cls statement.

Fourth: the output of induced_slot is also cached, but it still seems like we're getting cache misses from the difference between slot name as a str vs SlotDefinitionName. not fixed in this PR.

Cumulative time in Schemaview.induced_slot before: 510s (95832 calls) Cumulative time in Schemaview.induced_slot after: 243s

codecov[bot] commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (2e35728) 62.90% compared to head (fe8e821) 62.90%. Report is 1 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #296 +/- ## ======================================= Coverage 62.90% 62.90% ======================================= Files 62 62 Lines 8529 8529 Branches 2239 2239 ======================================= Hits 5365 5365 Misses 2554 2554 Partials 610 610 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.