-
Notifications
You must be signed in to change notification settings - Fork 30
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
[ENH] Add cache/memoization to PVcell properties (#121) #123
base: master
Are you sure you want to change the base?
Conversation
I'm totally struggling to understand what the existing code in SPACES = simple, performance, accurate, collaborate, extensible, & stable |
I admit I was rather put off by that code too, but your explanation makes sense. I suppose if Even with the speed-up in this PR, some quick testing shows instantiation as about 50x the execution time of |
I don't have a plan. I like the idea of replacing copy with instantiation. I don't know where I got the idea copy would be faster. 50x is a big difference. |
Even though a more involved fix might be better overall, a very unintrusive option would be a new PVcell method that looks like this:
and use |
Sounds great. I was thinking something similar but just named So wait, I'm confused which is slower, instantiation or copy? At first I thought copy was faster, but then it seemed like you were saying copy was slower, now it seems like copy is faster. Anyway thanks for diving into this. Anyway I think I misunderstood you earlier Ping @chetan201 |
Sorry, should have been more clear. Copy is faster than instantiation, presumably because instantiation incurs the cost of all the numerical IV calculations but copying does not. Here are the timings with the clone method (updated from above to use the parent def clone(self):
cloned = copy.copy(self)
super(PVcell, cloned).__setattr__('_cache', {})
return cloned In [1]: import pvmismatch
In [2]: %timeit _ = pvmismatch.PVcell()
132 µs ± 3.15 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [3]: cell = pvmismatch.PVcell()
In [4]: %timeit _ = cell.clone()
3.32 µs ± 117 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) |
This is very cool! Thanks! I just realized another reason why shallow copy better than deep copy or instantiation: we want all cells (and other components) to refernece the same @chetan201 do you know if SunPower intends to continue maintaining PVMismatch? |
this is really cool indeed! For some reason, I haven't been getting notifications from this repo to my email so missed all these reminders. My apologies. |
@chetan201 FYI I still need to include the above cloning idea to get this PR ready to merge. I'll try to get to that in the next few days. |
I think this is ready for review. Two comments:
|
@ahoffmanSPWR you should check this out! |
This PR adds a caching mechanism to the PVcell class such that successive calls to a property reuse the previously calculated value instead of recalculating it and causing a cascade of other property recalculations as well.
There are two things I want to highlight:
test_setsuns
suite due to PVmodule's use ofcopy.copy()
on PVcell objects: because it is a shallow copy, the cache dictionary is shared by all instances, which is of course not desirable. By replacing all of thecopy
calls withdeepcopy
I am able to get the tests to pass, but that approach seems rather heavy-handed and I wonder if others see a better approach.Here is a timing comparison -- nothing rigorous, just
pytest --durations=0 pvmismatch\tests
. Note that these timings were performed using thedeepcopy
modification described above, which is not currently committed in this PR.