Thank you, Nir, for a thoughtful discussion.
It looks like whatever requirement that resulted in a lazy initialization is
not applicable anymore, or perhaps some later code changes in CssStyleHelper
changed the design in such a way that the lazy initialization is no longer
possible.
The idea of using reflection or
MethodHandlers.lookup().lookupClass().getSuperclass() is an interesting one.
Would it work when reflection is disabled by the application? The reflection
is sometimes disabled using a custom SecurityManager, though its days might be
numbered. Once SecurityManager is removed, will there be a way for an
application developer to disable reflection?
I do like the idea about annotations, but there is still some amount of code in
CssMetaData.isSettable(...) { ... property == null | !property.isBound() }
so I am not sure if a pure annotation-based solution is possible (I could be
wrong).
What do you think?
-andy
From: openjfx-dev <[email protected]> on behalf of Nir Lisker
<[email protected]>
Date: Friday, December 1, 2023 at 20:35
To: John Hendrikx <[email protected]>
Cc: [email protected] <[email protected]>
Subject: Re: CssMetaData.combine()
John answered already to most of the points, but I want to give my own insights
as well.
Even though the syntax is ugly, the current implementation of the static
getClassCssMetaData() is nearly perfect, considering the lack of some kind of a
'lazy' keyword in java.
I don't buy Nir's argument about "questionable API". The API is codified by
Node.getCssMetaData() and the current implementation will be perfect with the
proposed utility method
Let's look at what implementation is required from a user who wants to write
their own styleable control:
1. Create styleable properties.
2. Create a list of these properties to be passed on.
3. Create a public static method that returns the concatenation of this list
with the one of its parent. (This method happens to be poorly documented, as
mstr said.)
4. Create a public non-static method that calls the static method in a
forced-override pattern because otherwise you will be calling the wrong static
method. (This method's docs seem to be just wrong because you don't always want
to delegate to Node's list.)
This is mostly redundant work with copy-paste and pitfalls, especially the need
to manually specify the parent. I would say that this is a very cumbersome
implementation that would not pass code review.
I'm not sure if users even need access to those styleable lists themselves,
maybe for GUI builders/analyzers? Surely you don't need 2 methods that do the
same thing, and both of those codify the API.
What the current code does is two things - a lazy initialization, meaning the
code will get executed only when needed, and it has zero per-instance overhead.
I don't think anyone can suggest a better way of doing it.
I'm confused by the notion that this is important. We're talking about static
data, that is, per class, not per instance. How many styleable classes do we
intend to use in an application? 100? Are we talking about saving 1KB of memory
or 1 millisecond of runtime? *Per instance* is important, *per class* is
negligible.
And why is the need for laziness? John also mentioned that any displayed
instance of a class will initialize these anyway (on first use). A benefit can
only arise if we create an instance but don't show it, in which case why did we
create it?
And I would be very much interested to hear from Nir his idea of an API that is
not questionable. I think we'll all benefit from learning how to make javafx
better.
Are we stuck with the current behavior of steps 2 to 4 above, or can we
circumvent it for future cases? Do we only deal with controls here, or skins
also because (as mentioned by John and Michael) they can also add styleable
properties?
If I had to touch the least amount of code, I would at least make the
concatenating method auto-resolve the parent of the current class by calling
`MethodHandles.lookup().lookupClass().getSuperclass()`, eliminating that
pitfall. Then we don't need the static method as far as I can see since its
whole purpose was to allow this recursive concatenation (except in cases like
ContextMenuContext that do something weird).
I think that a better overall approach could be with annotations on styleable
properties and an annotation processor. It can have the following benefits:
* Automatic access to the declared styleable properties.
* Usable both in controls and in skins (or other classes).
* Auto-generation of the css reference that is coupled with these.
* Mention of the corresponding css attribute in their documentation (like I
wanted previously
https://mail.openjdk.org/pipermail/openjfx-dev/2022-February/033482.html).
It will depend on what exactly we need to do with these properties.
By the way, John
Lazy initialization in many places that IMHO is not needed
I noticed this for the first time in classes like Box, Sphere and Cylinder.
Their dimension properties are lazily initialized, but are also initialized on
construction, so I never understood what the point was.
On Fri, Dec 1, 2023 at 5:57 AM John Hendrikx
<[email protected]<mailto:[email protected]>> wrote:
Hi Andy,
Let me start to say that I had no problem with this PR being merged as I
already agreed with one of the first versions.
Sometimes then on the same PR there can be some discussions on what else can be
done in this area, potentially maybe even alleviating the need for the change
(X/Y problem, ie. why do you need this method? Because you need to concatenate
lists, but the underlying reason is that the CSS property initialization is
somewhat clumsy).
On 01/12/2023 01:11, Andy Goryachev wrote:
Dear colleagues:
there were a couple of comments after I withdrew
https://github.com/openjdk/jfx/pull/1296for reasons of frustration, so I wanted
to respond to those in the openjfx list.
> I pondered that back when I was working on replacing these static
> initializers with the .of collection variants. It doesn't work here for
> problem stated above - we need to modify an unmodifiable list, which is why I
> didn't touch them in that pass. While the proposed method is useful for
> eliminating some ugly syntax, cementing a questionable API with more a public
> API doesn't seem to me like the right direction. If the method is made
> internal only, then that's fine. Alternatively, if the method is made useful
> outside of this specific context, then even if it won't be used here, it
> could be used in other places, and that's also fine.
Even though the syntax is ugly, the current implementation of the static
getClassCssMetaData() is nearly perfect, considering the lack of some kind of a
'lazy' keyword in java.
It may be "nearly perfect" from an optimization viewpoint, but it is clumsy and
unwieldy for anyone wanting to implement CSS properties.
What the current code does is two things - a lazy initialization, meaning the
code will get executed only when needed, and it has zero per-instance overhead.
I don't think anyone can suggest a better way of doing it.
This was already mentioned on the PR, but I'll repeat it here: what is the lazy
initialization for? As soon as these Nodes need to be shown, all the metadata
will have been queried already. I don't see any benefit making them lazy so you
can create Nodes faster, as long as they are never shown.
I don't buy Nir's argument about "questionable API". The API is codified by
Node.getCssMetaData() and the current implementation will be perfect with the
proposed utility method (and maybe we can address some other comments from
https://git.openjdk.org/jfx/pull/1293#discussion_r1411406802 ).
How can there be any doubt that this API is questionable? It ignores a core
feature of Java (inheritance) and moves this burden to the user by calling
static methods of its direct parent... in order to implement CSS property
**inheritance** -- it also burdens any subclass with the caching of these
properties (because "performance"), and to make those properties publicly (and
statically) available so another subclass might "inherit" them.
The API is clumsy enough that I loathe creating stylable properties for the
sheer amount of boilerplate that surrounds them.
Some alternatives have been suggested, but are shot down without thinking along
to see if there might be something better possible here. Solutions where some
of the common logic is moved to either Node or the CSS subsystem are certainly
worth considering.
... a few bytes and cpu cycles would get saved ...
This is not for you specifically, but JavaFX has a lot of "optimizations", some
resulting in really questionable patterns that have/are hurting us:
- Reimplementing core collection classes for some benefit, but then only
partially implementing them (and often buggy), and/or completely breaking the
collection contract [BitSet]
- Lazy initialization in many places that IMHO is not needed (benchmark should
be time to show window, anything accessed before that need not be lazy, and is
likely counterproductive)
- Using plain arrays in many places, with a lot of custom code that's already
available in some standard collection class or as a standard pattern; the
custom code often has untested edge cases that contain bugs [ExpressionHelper]
- Making things mutable; surely mutating something must always be faster than
having to create a new object? Except that if there's a lot of duplication
going on because these objects are unshareable (because mutable), the
cost/benefit is no longer so clear (but try to prove that with a micro
benchmark) [PseudoClassState / StyleClassSet]
- Also see many usages of LinkedList, a class that if you'd never use it, you'd
be better off 99.999% of the time; use of that class should always be explained
in a comment, and proven to be better with a benchmark [too many places to list]
The list goes on; many of the optimizations I've seen would make sense for
C/C++, but not for Java. Now I don't mind some optimizations, but practically
none of them are documented (deviating from the standard path always deserves
an explanation for the next developer) and I suspect they were never verified
either. I've done extensive "optimization" before, with benchmarks, and you'd
be surprised what is actually faster, and what makes no difference whatsoever
-- even then, after benchmarking, if the difference is small, it's best to use
established patterns, as that's what the JDK optimizes for. What is marginally
faster now, may not be faster on the next JDK, with a different GC, when run in
a real application (caches will be used differently), or on a completely
different architecture.
--John