Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpannableStringBuilder performance improvements?

I'm writing an app that constantly updates a TextView with a SpannableStringBuilder that uses a ton of spans. Benchmarking shows that, unsurprisingly, setText() and drawText() are both big blockers, and SpannableStringInternal.getSpans() has the single highest exclusive CPU time.

Is there a way to reuse the same span, so I don't have to clog up my SpannableString with multiple identical spans? Are there other ways I can improve SpannableString performance?

like image 784
Tom Quinn Avatar asked Sep 01 '26 07:09

Tom Quinn


1 Answers

After a lot of experimentation, I found that creating a custom View that overrides onDraw to draw its own text is absolutely the way to go here. There's no way to prevent getSpans from using a ton of reflection.

like image 148
Tom Quinn Avatar answered Sep 03 '26 00:09

Tom Quinn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!