Comments (6)
Enabling cache with param.depends
should be done carefully as a method can have state information not put into the decorator but available in the class itself, making it a very hard problem.
from param.
Caching param.depends
is effectively an absolute no-go for that reason. In the case of pn.bind
you can at least have a reasonable expectation that the function is only dependent on the state of the arguments and memoize on those, in the case of pn.depends
(especially in a class setting) you could memoize on the explicitly stated dependencies but that still seems quite implicit and that assumption will break in certain scenarios.
from param.
@MarcSkovMadsen can you quantify how inefficient param.depends
is?
from param.
I mean param.depends
itself isn't inefficient, the problem is that it can lead users to write inefficient code. So I'd say it's at least partially a usage and documentation issue. It's perfectly possible to write apps using depends
that cache intermediate results on parameters, in fact that's a pattern I use relatively frequently, i.e. instead of doing this:
class Adder(param.Parameterized):
a = param.Number()
b = param.Number()
@param.depends('a', 'b')
def result(self):
return self.a+self.b
you write:
class Adder(param.Parameterized):
a = param.Number()
b = param.Number()
result = param.Number()
@param.depends('a', 'b', watch=True)
def _calculate(self):
self.result = self.a+self.b
When using bind
on the other hand there is no obvious way to cache the result.
from param.
I mean
param.depends
itself isn't inefficient, the problem is that it can lead users to write inefficient code. So I'd say it's at least partially a usage and documentation issue. It's perfectly possible to write apps usingdepends
that cache intermediate results on parameters, in fact that's a pattern I use relatively frequently, i.e. instead of doing this:class Adder(param.Parameterized): a = param.Number() b = param.Number() @param.depends('a', 'b') def result(self): return self.a+self.byou write:
class Adder(param.Parameterized): a = param.Number() b = param.Number() result = param.Number() @param.depends('a', 'b', watch=True) def _calculate(self): self.result = self.a+self.bWhen using
bind
on the other hand there is no obvious way to cache the result.
Agree with Philipp. The main problem is that the bound functions are called the same number of times as you depend on them instead of one time.
The examples I provided are the same as Philipp provided. Philipp second example look simple, but if you start having multiple parameters to update it becomes less obvious what goes on. I think its friction that you have to write that extra boiler plate code in every Parameterized class you write instead of just providing a cache=True
or cache=('a','b')
parameter to param.depends
or similar.
from param.
I don't buy the argument of having state that is not-depended on. Right now without concrete examples I believe that is an antipattern.
Pro adding caching I would also argue that
- There is a huge difference in how parameters and bound functions are treated by Panel. Parameters are displayed using widgets and bound functions using panes. Thus if a bound function is inefficient and I change to a Parameter+
watch=True
implementation its not only the Parameterized class that needs to change. Its all the places where its used. - Caching is not built into Param. Its built into Panel (
pn.cache
) thus you cannot actually build the caching your self in the Param world. Making it even harder to make your Parameterized classes efficient and optionally using it in Panel. - "Modern" frameworks are "React" like. And the ones I've seen in Python does the caching automatically for the user (as well as the "parameter" dependency resolution).
from param.
Related Issues (20)
- Param with bounds should default to lower bound HOT 7
- Add .is_active parameter to reactive expressions HOT 3
- Let reactive expressions .watch method support async functions
- Context manager .param.update should restore references
- Let pn.Param accept a list of objects HOT 1
- Warn on errors thru rx updates
- Parameter called outputs does not work with `.rx` HOT 4
- Triggering a function in one instance based on parameter changes in another HOT 2
- Initialization changes constant attribute. HOT 3
- Missing `param_help.png` in `User Guide/Parameters`
- Discussion about the intro video HOT 1
- Should methods on Parameterizeds be treated as references by default HOT 3
- `name` of a Parameterized instance overridden when is the default of a `readonly/constant` Parameter HOT 2
- Error on printing rx
- RX: output not updated when indexing a DataFrame with value_start/value_end HOT 1
- Small text issue in doc Getting Started
- Ability to specify multiple classes in class_ for ClassSelector parameter type needs to be documented
- Cannot use param.Parameterized inheritance when inheriting alongside abstract classes
- Getting a "'_thread.RLock'" object error when instantiating a LangChain model as a default object
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from param.