Skip to content

Conversation

@mbien
Copy link
Member

@mbien mbien commented Apr 10, 2025

Instead of updating WeakSet (#8380) we deprecate it for removal and replace all usage.
Once the build starts failing we can still decide what to do with it.

  • WeakSet is now deprecated for removal
  • usage can be replaced with WeakHashMap and Collections.newSetFromMap()

part of #8257

have to take a good look at all usages which used putifAbsent, some might need tweaks

  • ElementHandle deduplicaiton mechanism can be replicated using a WeakHashMap and storing the handle as key and as WeakReference-wrapped value. This way the old element can be extracted if there is a match, while allowing the map to shring if the handles aren't used anymore. Not 100% sure yet what overhead this is supposed to avoid though since the ElementHandle is created in all paths, but an old handle is returned on hit while discarding the new one.
  • WebLogicConfiguration removed the cache since it was redundant. The caller is cached. Someone liked caches.

main difference is that all calls to WeakSet#add were implemented via putIfAbsent. This is not the case in WeakHashMap, however, during the typical use case (e.g listener storage) this should not make a difference.

@mbien mbien added Code cleanup Label for cleanup done on the Netbeans IDE Platform [ci] enable platform tests (platform/*) ci:all-tests [ci] enable all tests labels Apr 10, 2025
@mbien mbien force-pushed the deprecate-weakset branch from f0bb224 to 146e3b2 Compare April 11, 2025 00:17
@mbien mbien added the do not merge Don't merge this PR, it is not ready or just demonstration purposes. label Apr 11, 2025
@mbien mbien force-pushed the deprecate-weakset branch from 146e3b2 to 80e3966 Compare April 11, 2025 02:25
@mbien mbien removed the do not merge Don't merge this PR, it is not ready or just demonstration purposes. label Apr 11, 2025
@mbien
Copy link
Member Author

mbien commented Apr 12, 2025

trying to figure out what the purpose of the ElementHandle deduplication code is. Its not for instance comparison reasons, since

$a == $b
:: $a instanceof org.netbeans.api.java.source.ElementHandle
&& $b instanceof org.netbeans.api.java.source.ElementHandle
&& !isNullLiteral($b)
=>
$a.equals($b)
;;

$a != $b
:: $a instanceof org.netbeans.api.java.source.ElementHandle 
&& $b instanceof org.netbeans.api.java.source.ElementHandle
&& !isNullLiteral($b)
=>
!$a.equals($b)
;;

produced no relevant hits.

Here is the original commit emilianbold/netbeans-releases@3dab461 referencing https://bz.apache.org/netbeans/show_bug.cgi?id=197743

https://bz.apache.org/netbeans/show_bug.cgi?id=197743#c2 indicates it was one of the changes to attempt to reduce memory consumption in large files.

Will check if this deduplication cache could be removed again. A new handle has to be created first before checking if it is already there (no construction overhead reduction) and secondly the Object itself is just two constants: ElementKind and a String array for its signatures. Would have been good to still have the original performance test file for this (Big.java). Edit: this must be it https://bz.apache.org/netbeans/show_bug.cgi?id=194816, test project attached.

@mbien mbien force-pushed the deprecate-weakset branch from 80e3966 to 6ce9029 Compare April 13, 2025 01:42
@mbien mbien marked this pull request as ready for review April 13, 2025 01:42
@mbien
Copy link
Member Author

mbien commented Apr 13, 2025

removed the ElementHandle deduplicaiton code after testing with the original reproducer project (added to the reprorepo).

this spawned two more PRs to tackle performance problems I noticed while playing with that test project:

filed issue for third scaling problem:

@mbien mbien added this to the NB27 milestone Apr 15, 2025
@mbien mbien force-pushed the deprecate-weakset branch from 6ce9029 to d1add04 Compare April 15, 2025 10:43
@mbien
Copy link
Member Author

mbien commented Apr 30, 2025

planning to get this PR in as soon as possible after RC3

@mbien
Copy link
Member Author

mbien commented May 8, 2025

I am running this together with some of the performance enhancement PRs and everything is working fine. I was mostly worried about the removal of the ElementHandle deduplication code but even synthetic tests with an unreasonable amount of fields/methods/inner classes which hit the code path often do fine.

So my current thinking is that we can indeed remove the deduplication (see second commit with the "problem cases" ElementHandle#create).

edit: will refresh this PR with a rebase - no other changes

@mbien mbien added the ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) label May 8, 2025
@mbien mbien force-pushed the deprecate-weakset branch from d1add04 to da32ef5 Compare May 8, 2025 00:38
@mbien
Copy link
Member Author

mbien commented May 28, 2025

almost forgot about this PR. Planning to merge this ASAP to make progress with #8257. since there is still much to do in #8256. Last chance to review ;)

@mbien mbien force-pushed the deprecate-weakset branch from da32ef5 to 5037826 Compare May 30, 2025 00:33
Copy link
Contributor

@lkishalmi lkishalmi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go for this!

Copy link
Member

@neilcsmith-net neilcsmith-net left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks sane, not tested.

Had a concern at first about the explicit sizing of the WeakHashMaps but see that WeakSet used the same load factor logic. I'm curious how many of those capacities are correct, but better to merge matching existing behaviour.

@mbien
Copy link
Member Author

mbien commented Jun 2, 2025

those sizing values are a "false friend", somewhat similar to String#replaceAll. It is likely that they don't do what some authors thought they would do, but in practice it probably doesn't matter all that much. Post JDK 21 bump this should be all changed to [Map|SetImpl]#newFoo[Map|Set] More details with a short demonstrator is on an old blog entry I once wrote.

@neilcsmith-net
Copy link
Member

Yes, I know - that's what caught my eye looking at this. Funnily enough your blog post was one of the first things that popped up when I searched for the JDK 19+ methods to check the difference in behaviour! 😄

mbien added 2 commits June 2, 2025 22:08
 - WeakSet is now deprecated for removal
 - usage can be replaced with WeakHashMap and
   Collections.newSetFromMap()
 - web logic config factory doesn't seem to need caching, it is only
   called from one place, and that object is cached too
 - ElementHandle used the WeakSet to deduplicate instances (original
   reason were memory concerns), testing showed that this had little
   impact and can be likely also removed
@mbien mbien removed the ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) label Jun 2, 2025
@mbien mbien force-pushed the deprecate-weakset branch from 5037826 to 36590fb Compare June 2, 2025 20:10
@mbien mbien merged commit cc0d3ca into apache:master Jun 2, 2025
121 of 123 checks passed
@mbien
Copy link
Member Author

mbien commented Jun 2, 2025

thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:all-tests [ci] enable all tests Code cleanup Label for cleanup done on the Netbeans IDE Platform [ci] enable platform tests (platform/*)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants