-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Make a Node a factory for itself #199
Conversation
This patch refactors the Node to also be a factory for self: this saves about 90K as there are zero fields & field inits now and half as much anonymous classes. Jar size is reduce to just under 700K NodeFactory is now an iterface with default methods and factories are not singletons anymore.
I guess the only flaw is that this adds extra instance fields due to the factory node's unused ones? The alternatives would all have to add some, too, of course. |
Not really, the factory is stateless. Take the generated ...
// factory methods:
public <K2, V2> Node<K2, V2> newNode(K2 key, ReferenceQueue<K2> keyReferenceQueue, V2 value,
ReferenceQueue<V2> valueReferenceQueue, int weight, long now) {
return new WSo<>(key, keyReferenceQueue, value, valueReferenceQueue, weight, now);
}
public <K2, V2> Node<K2, V2> newNode(Object keyReference, V2 value,
ReferenceQueue<V2> valueReferenceQueue, int weight, long now) {
return new WSo<>(keyReference, value, valueReferenceQueue, weight, now);
}
public <K2> Object newLookupKey(K2 key) {
return new LookupKeyReference<K2>(key);
}
public <K2> Object newReferenceKey(K2 key, ReferenceQueue<K2> referenceQueue) {
return new WeakKeyReference<K2>(key, referenceQueue);
}
... There are no instance fields in the factory as it is now an interface. |
Sure there is. |
Yes, the Wso used as factory will have some extra fields, but the Wso instances used as |
Yep, that's all I meant. It wasn't meant as more than an observation |
Sorry, I thought you are concerned with bloating the Node footprint :) |
Btw the K2, V2 are there to fix a few warnings. If we can live with the shadowing warnings that's 2K more saved and the psychological barrier of 700KB is passed :) |
That would be okay or you could make I'm not excited at the extra top-level fields, but I am also trying to see if there is any justification for that feeling. Its probably the right tradeoff. |
When looking at the method handle approach, I guess it actually is worse because each |
The enum constants would also create 144 field + 144 classes (in the metaspace) when probably 2-3 are really used. I would expect that memory usage is reduced a bit - assuming an app has a few caches now it would also need a create a few nodeFactories. The enum approach would always create 144 factories. |
Oh I am definitely for removing the enum. It was just easy to write when trying to get the codegen working. There was a lot of upfront development costs to get this project going. I can merge this unless you are still iterating on any of it? |
I typed the |
How about this: make the node class a factory for itself :) (details in the commit message)
Zero reflection on the fast path + 90KB classes less.
It is not how a real gentleman would structure their Java code but the circumstances are extreme :)