I have a text of 10k characters. And i want to color each letter individually. (probably not what JTextPane was designed to do)
I noticed that the following method takes a long time:
// The code snippet below is run via
// EventQueue.invokeLater { myRenderFrameFunction(text) }
// Omissions
text.forEachIndexed { index, character ->
styledDocument.setCharacterAttributes(index, 1, colorings[character], false)
}
I takesabout 27 ms to run
I'm not experienced with coroutines but I thought they could help me.
text.forEachIndexed { index, character ->
coroutineScope {
async {
styledDocument.setCharacterAttributes(index, 1, colorings[character], false)
}
}
}
}
If I run the above with either runBlocking {} or GlobalScope.launch {} I do not get any performance boost. (But i do get wacky behavior if i do it with GlobalScope)
(Side question: I feel like my code overwhelms EventQueue.invokeLater {...}), can i like slow down my frame generation to the ui's pace dynamically? somehow)
Why doesn't coroutines help in this instance? I feel like Im out of luck with this swing thing. Do you think making setCharacterAttributes(...) target spans of letters will have an impact for the strings with long stretches of the same letter?