Skip to content
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

[gtk] Make Fonts GC disposable #1054

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

laeubi
Copy link
Contributor

@laeubi laeubi commented Feb 20, 2024

Current State

Currently handling fonts in SWT is hard and cumbersome, because one can't use the usual java techniques users are forced to either explicitly manage fonts (what maybe retain them longer as needed) or even create and dispose them for individual widgets (what hurts performance). This also leads to a lot of boilerplate code e.g. installing dispose handlers or having managed caches or local resource managers as well as requires documentation who 'owns' a font for example set on a label. Especially for newcommers or seldom users of SWT this is hard to get right and hinders them to use SWT correctly as java is usually managing objects automatically.

What this changes

This now introduces the concept of Cleaners added to Java 9+ that allows automatic management of external resources that get automatically disposed when the JVM decides that an object is only phantom reachable and eligible for garbage collection.

I also used this to have a Handle act as a proxy to the native method calls so these are much more readable and the grouped together even related calls or required transformations to reduce code duplication.

Further considerations

This itself is just meant as an initial starting point, we actually should extends this concept to any native resource handled by SWT (e.g. Images) so it become much more convenient for users to use these. I just choosing a font here as the only managed resource is a long pointer.

I also like to encourage users of other platforms (Win/Mac) to use this as a hint to prototype something similar like this as well so we can evaluate how it works out.

Also this will not replace dispose() or make it completely redundant as of course a user can always better dispose a resource directly if it is known that it is no longer in use. Also it might not be suitable for any "root objects" like a Display that is like a Thread in java some kind of UI root that always needs to be disposed explicitly.

Copy link
Contributor

github-actions bot commented Feb 20, 2024

Test Results

   299 files  ±0     299 suites  ±0   5m 58s ⏱️ -57s
 4 099 tests ±0   4 091 ✅ ±0   8 💤 ±0  0 ❌ ±0 
12 209 runs  ±0  12 134 ✅ ±0  75 💤 ±0  0 ❌ ±0 

Results for commit f534d57. ± Comparison against base commit 7b18570.

♻️ This comment has been updated with latest results.

@laeubi laeubi force-pushed the gtk_automatic_resource_handling branch 2 times, most recently from 69201cf to 1c5de02 Compare February 25, 2024 16:49
@laeubi laeubi force-pushed the gtk_automatic_resource_handling branch from 1c5de02 to 2b249d4 Compare March 5, 2024 08:20
@laeubi laeubi changed the title [gtk] Automatic resource handling / disposing for native font handles [gtk] Make Fonts GC disposable Mar 7, 2024
@laeubi
Copy link
Contributor Author

laeubi commented Mar 7, 2024

I adjusted the title a bit now to make clear I don't want to replace the current disposal of resources, but make them GC disposable (that is for example it would be possible to hold Weak/Soft/PhantomReferences of items).

@mickaelistria
Copy link
Contributor

That seems very good and can probably prevent some "no more handles" error from happening.
Just one question (not a change request), could this apply to any resource, eg also Colors, Cursor, Image...?

@laeubi
Copy link
Contributor Author

laeubi commented Mar 7, 2024

could this apply to any resource, eg also Colors, Cursor, Image...?

Yes this basically could (and should) be applied everywhere we are holding a native resource that must be disposed when the object is disposed. (Colors don't need to be disposed anyways but thats a different topic).

@laeubi
Copy link
Contributor Author

laeubi commented Mar 7, 2024

One example where it is useful to only hold a SoftReference is for example here:

Currently with SWT I only have two choices:

  1. I hold a hard reference and only free that on request, e.g. when the control is disposed, or even when the bundle is stopped, e.g. by using ResourceManagers
  2. I only hold a SoftReference to the ImageData, but this means I need to create a new Image each time I want to paint that and instantly dispose it, what makes it quite cumbersome and maybe not very performant.

A common usage for this would be some kind of a custom drawn control, where I want to store the last drawn state as an Image and repaint just paint the image.

Currently handling fonts in SWT is hard and cumbersome, because one
can't use the usual java techniques users are forced to either
explicitly manage fonts (what maybe retain them longer as needed) or
even create and dispose them for individual widgets (what hurts
performance). This also leads to a lot of boilerplate code e.g.
installing dispose handlers or having managed caches or local resource
managers as well as requires documentation who 'owns' a font for example
set on a label. Especially for newcommers or seldom users of SWT this is
hard to get right and hinders them to use SWT correctly as java is
usually managing objects automatically.

This now introduces the concept of Cleaners added to Java 9+ that allows
automatic management of external resources that get automatically
disposed when the JVM decides that an object is only phantom reachable
and eligable for garbage collection.
@laeubi laeubi force-pushed the gtk_automatic_resource_handling branch from 2b249d4 to f534d57 Compare March 10, 2024 05:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants