Nincraft / Nincraft-Launcher

A custom Minecraft launcher for installing modpacks for the Nincraft server
http://nincraft.com
Other
2 stars 0 forks source link

Avoid pathological failure mode when trimming lines #2

Closed Nincodedo closed 9 years ago

Nincodedo commented 9 years ago

If for any reason the document associated with a LimitLinesDocumentListener ended up not getting shorter when its first line was removed (this is, of course, impossible), sklauncher would wedge in a busy loop that completely killed the GUI thread forever.

Solution, part #1: Don't do a while loop, just remove multiple lines if you need to.

But wait, why are we having this problem at all? It turns out that there are problems when the document's length changes, which can manifest as tons of BadLocationExceptions going uncaught in the AWT thread which is trying to refresh the window. Why? Probably because the insert into the document isn't happening in the Swing run queue, but concurrently with it. So use an invokeLater to do the actual insertion.

With these two changes, and a console set to 125 lines, I got no failures. Previously, if I set the console to 125 lines, it would die catastrophically before Minecraft was done starting up.

Also performance may be noticably better. It's pretty common for things to dump 20+ lines into the buffer at once. If you have a 10,000 line buffer, and you then delete the first line of it 20 times, that's actually a whole lot of allocation and shuffling going on.