This is indeed a good idea. You are right, especially all the different mouse and key listener (enter, exit, pressed, drag and drop, ...) are very rarely listened to.
Setting those properties will initialize the property.
So this complements the idea from Andy. Some properties can be more lazily initialized. While other may should not be initialized at all until someone wants to listen to it.
-- Marius
Am 14.02.26, 06:50 schrieb Glavo <zjx001202@gmail.com>:
I have another idea: event handle properties (like `ButtonBase#onActionProperty()`) are frequently set, but rarely have listeners added.
I think we can initialize these properties more lazily.
We can add the following method to `Node`:protected final <T extends Event> EventHandler<? super T> getEventHandler(final EventType<T> eventType) {
getInternalEventDispatcher().getEventHandlerManager().getEventHandler(eventType);
}
Then we can implement `onActionProperty()` like this:private ObjectProperty<EventHandler<ActionEvent>> onAction;
public EventHandler<ActionEvent> getOnAction() {
return onAction != null ? onAction.get() : (EventHandler<ActionEvent>) getEventHandler(ActionEvent.ACTION);
}
public void setOnAction(EventHandler<ActionEvent> value) {
if (onAction != null)
onAction.set(value);
else
setEventHandler(ActionEvent.ACTION, value);
}
public ObjectProperty<EventHandler<ActionEvent>> onActionProperty() {
if (_onAction_ == null) {
_onAction_ = new ObjectPropertyBase<>((EventHandler<ActionEvent>) getEventHandler(ActionEvent.ACTION)) {
@Override
public Object getBean() {
return ButtonBase.this;
}
@Override
public String getName() {
return "onAction";
}
@Override
protected void invalidated() {
setEventHandler(ActionEvent.ACTION, get());
}
};
}
return onAction;
}
This allows us to eliminate the allocation of many properties.
Although there is a slight risk (the behavior will change slightly if the user updates the handler value with `setEventHandler(ActionEvent.ACTION, ...)` before calling `onActionProperty()`/`getOnAction()`), I think it is worthwhile.GlavoOn Thu, Feb 5, 2026 at 5:17 AM Andy Goryachev <[email protected]> wrote:I would like to share the results of a little experiment involving optimization of storage of Node properties. The basic idea is to create a compact fast map-like container to hold the rarely instantiated properties in order to reduce the application memory footprint.
The savings are not overwhelming, but not exactly zero. I would imagine this optimization might be more interesting in any resource constrained environment such as Android / iOS / RaspberryPi. Please refer to [0] for the details.
I encourage you to try it with your application, to see whether you notice any change in memory consumption and/or performance. Let me know what you think!
Cheers,-andy
References
