codenameone / CodenameOne

Cross-platform framework for building truly native mobile apps with Java or Kotlin. Write Once Run Anywhere support for iOS, Android, Desktop & Web.
https://www.codenameone.com/
Other
1.66k stars 395 forks source link

GC storm in stringWidth #3770

Closed ddyer0 closed 6 months ago

ddyer0 commented 6 months ago

While sleuthing performance issues, I came across this method in JavaCEFSEPort

private int canDisplayUpTo(java.awt.Font fnt, String str) { String key = fnt.getName() + ":" + str; if (canDisplayUpToCache.containsKey(key)) { return canDisplayUpToCache.get(key); } ...

As you can see, this creates a garbage string to act as a key. This method gets called a lot - at least every time a string is drawn. I propose this ought to be replaced with a similar per-font cache which won't need to create strings for keys.

Of course, this method only directly affects the simulator. I haven't checked the corresponding code for ios and android.

shai-almog commented 6 months ago

This is in the JavaSE code. GC there is FAST and caching it might be slower than letting the GC do its thing.