Breaking and Restoring the BBcode of my RichTextLabel causes my framerate to shoot up?

Godot Version

4.7

Question

The short version is, some UI elements in my game slow the framerate down a lot, but for reasons I can’t ascertain, 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 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?

Happy to produce any of the relevant code and/or screen record the relevant behavior and share a link here, if that’d be helpful.

Do you do anything with those images every frame except for when you are searching?

No, the images are static. When a player clicks to open a specific forum thread, the ready function populates the screen with the posts in the thread (including the images referenced by the BBcode) and after that the only thing they do is get scrolled up/down as the user scrolls with the scroll bar.

I blame the engine. My code for image renderingin my LITE Web Browser is abysmal. Chalk it up to me not knwoing how to Godotically doing it. We must wait on God off topic.

What does this mean? Do you remove the tag?
I would like to see the search/replace code.

Sure. The code is here: #This func is triggered by pressing "Find Next" in the Ctrl+F windowfunc _on_f - Pastebin.com

So when I use the search term “img” the raw bbcode would go from something like this:
[img]res://Images/map.png[/img]

To something like this:
[[bgcolor=orange]img[/bgcolor]]res://Images/map.png[/[bgcolor=orange]img[/bgcolor]]

And in-game, that means a displayed image turns into a line of bbcode text where the letters “img” are highlighted orange.

Nothing in that code stands out as a potential culprit.
The only thing I notice is that the breaking of the bbcode creates a tag that isn’t recognized.
[ [bgcolor=orange]img[/bgcolor] ] ← this is actually in tag format and is followed up by a matching closing tag → [/ [bgcolor=orange]img[/bgcolor] ]
But I tried something similar in an online bbcode tester and it seems to just ignore the broader tag and color the tag the way you describe.
My one and only suggestion is to test run it with the outer [ ] removed:
[bgcolor=orange]img[/bgcolor]res://Images/map.png/[bgcolor=orange]img[/bgcolor]
(I would give this about a 0.01% chance of being the issue)

Sorry, I can’t be much help here.