Experiment: Node properties

Glavo zjx001202 at gmail.com
Sat Feb 14 05:46:43 UTC 2026


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.

Glavo

On Thu, Feb 5, 2026 at 5:17 AM Andy Goryachev <andy.goryachev at oracle.com>
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*
>
> [0]
> https://github.com/andy-goryachev-oracle/Test/blob/main/doc/Experiments/NodeProperties.md
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/openjfx-dev/attachments/20260214/fcfc68ff/attachment.htm>


More information about the openjfx-dev mailing list