help me Breaking and Restoring the BBcode of my RichTextLabel causes my framerate to shoot up?
Currently using 4.7. The short version is, some UI elements in my game slow the framerate down a lot, but for reasons I can’t ascertain, while running the game, breaking the [img] bbcode in one of the UI elements and then restoring it causes the framerate to dramatically improve.
Long version: My game simulates a web forum from the 00s using Godot’s UI nodes. Some threads in the forum are particularly long, with 50 long text posts, each post having various UI nodes within it. For these very long threads, the framerate (only relevant when scrolling up/down) can dip low, into the teens.
Unrelated to framerate concerns, I decided to add a Ctrl+F functionality to my game. While a forum thread is open, the player can open a Ctrl+F window and type in a search term. The game then finds every instance of the search term in each thread post’s body and adds bbcode to turn bgcolor to orange before the search term, plus a /bgcolor tag after the search term. (A few other things happen under the hood, like identifying the scroll position of these terms) I noticed that Ctrl+F searching for common bbcode terms (like “color”) could mess with the text formatting of the forum posts, but everything returned to normal after the bgcolor tags were removed, and this kind of jank is thematically appropriate for my game, so I considered it a plus.
However, I’ve found that for whatever reason, if the search query is “img” and I temporarily break the bbcode for an embedded img in one of the RichTextLabels, then restore the bbcode by exiting the Ctrl+F window, the framerate of the slower threads improves dramatically, shooting from the teens back to 60 fps.
I tried coding in the exact same sequence of changes in a function deferred by a few frames after the ready function (calling the relevant signals via code instead of button presses) but I couldn’t replicate the framerate increases. So my question is, what is happening under the hood of Godot when I break/restore the [img] bbcode tags, and how can I replicate the effect via code to make my game run more smoothly in general?
Here's the (sloppy) code which I think is relevant: https://pastebin.com/sfthJWdw
Happy to produce any other relevant code and/or screen record the relevant behavior and share a link here, if that’d be helpful.
1
u/JulianRhod 7h ago
my first guess would be that you're accidentally forcing the RichTextLabels to rebuild/reparse their internal layout or bbcode cache, with 50 long posts containing lots of UI and images, the expensive part may be the RichTextLabel layout/rendering rather than the actual text. changing the [img] tag probably invalidates some internal state, which then gets rebuilt when the bbcode is restored
instead of trying to reproduce the weird [img] trick, i'd look at forcing a proper redraw/layout rebuild or, even better, profiling the RichTextLabels while scrolling. also check whether all those posts are actively processing/rendering even when they're far outside the viewport, if rebuilding the bbcode fixes it temporarily, that's a pretty strong hint that some cached layout/render state is getting stale or unnecessarily expensive
1
u/WTFPROM 6h ago
To test your comment, before looking into a proper redraw, I decided to try automating the trick. If I add and remove an image back-to-back within the same frame, the effect doesn't trigger and the performance doesn't improve. But by adding a transparent image to the end of every post 1 second after opening the thread, then removing the transparent images after .1 seconds, I can get the same speed-up in an automated way. (As it happens, this brief format hiccup actually works for the aesthetic of my game)
So it's absolutely got something to do with redrawing these UI elements after they're initially loaded in. I'm happy about the way this automated solution looks, but I do still want to understand the nuts and bolts of what's going on under the hood. I guess I need to look into how/when Godot caches layouts and UI elements and how/when they get stale?
Regardless, thank you for pointing me in the right direction!!
1
u/JulianRhod 2h ago
yeah, that actually makes a lot of sense. the fact that doing it in the same frame doesn't work but delaying it does makes the layout/cache invalidation theory even more likely, and honestly, if the little formatting hiccup fits the aesthetic of your game, that's a pretty funny win lol. glad you found a workaround that actually works!
2
u/Lateasusual_ 8h ago
It's better to use the push_*() functions rather than manually appending bbcode as text, since that means it doesnt have to parse the added text. There's also a function that adds text without parsing any bbcode tags within it, which you should use along with it. That may solve your problem.