-
Notifications
You must be signed in to change notification settings - Fork 185
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
request to support automatic interning of Built
objects
#750
Comments
You could use the new static void _finalizeBuilder(void Function(MyBuilder b)) {
b.replace(intern(b.build()));
} I'd like to understand your use case a little better--aren't you worried about memory consumption? |
You mean I should add that static method as you typed it into each class I want to be automatically
Why would memory consumption be a problem? It should actually be better this way (other than the overhead of the My use case is that I'm using many instances of https://pub.dev/packages/over_react The main way that expensive DOM re-renders are avoided is by comparing objects for equality and skipping the re-render of a component when its new state data is In fact, if the library supported automatic guaranteed |
Yes, adding that static method into all the classes should do it. The memory problem is that the cache does not know when you're done using a particular object, so if you make lots of modifications to objects then the cache will fill up.
I'd definitely be interested to learn how interning behaves in practice! |
Ah, that's a good point about building up old values in the cache and taking up memory. I suppose that's why Java can automatically |
Yeah. So I wonder if we need some kind of eviction strategy. It would be very interested to see a real world benchmark :) |
@davidmorgan I tried your suggestion for V intern<V extends Built<V, B>, B extends Builder<V, B>>(V value) {...} However, when I add this code (I expanded out the lines you wrote a bit to help me track down the error): static void _finalizeBuilder(void Function(BoundSubstrandBuilder builder)) {
BoundSubstrand bss = builder.build();
BoundSubstrand bss_interned = intern(bss);
builder.replace(bss_interned);
} I get the following error:
I guessed that maybe the signature as you write it was a typo (since I assume the named parameter static void _finalizeBuilder(void Function(BoundSubstrandBuilder) builder) but that generates the same compiler error. I tried changing the signature to the one suggested by the compiler error, but then I get this error:
A workaround to make the compiler happy is to pass a V build_interned<V extends Built<V, B>, B extends Builder<V, B>>(B builder) {
V value = builder.build();
... // similar to code implementing build()
} But when I create an object of type BoundSubstrand bss = BoundSubstrand((b) => b
..field1=value1
// etc.
); there is infinite recursion. |
It would be great to have an option to support automatic interning of
Built
objects. (caching them so that objects that are equal according to==
are not duplicated, speeding up equality comparisons)In Dart 2.6, it is possible to use extension methods to add a
intern()
method toBuilt
types:Now, I can call
intern()
on instances ofBuilt
, e.g.,However, it would be great if this could be automated to happen on every call to
build()
. For example, if I have a nested builder, then I have to assign aBuilder
object to it, not aBuilt
, so it's not an option for my code to callintern()
immediately after. The call tobuild()
happens in the library code where I cannot control it.One option is a special subclass of
Built
that could be implemented instead ofBuilt
?Of course the same thing would be useful for
BuiltList
,BuiltMap
, etc.The text was updated successfully, but these errors were encountered: