How can I select unicode text range in stc.StyledTextCtrl widgets?

Here is the demo code, I wrote comments in the code:

Environment is Python 3.8 and wxpython-4.2.1. But the other versions behave the same.

import wx
import as stc

class MyTextCtrl(stc.StyledTextCtrl):
    def __init__(self, parent):
        stc.StyledTextCtrl.__init__(self, parent)

    def SetUnicodeSelection(self, p1, p2):
        # This is my user define function. I convert the unicode text range into bytes range.
        text = self.GetValue()
        p1, p2 = (len(text[:p].encode()) for p in (p1, p2))  # unicode index -> bytes index
        self.SetSelection(p1, p2)

if __name__ == '__main__':
    app = wx.App()

    frame = wx.Frame(None, -1, 'Test Unicode Selection')

    text = MyTextCtrl(frame)

    # Initial some unicode text

    # The text of ",有鬼神掌着生死权" is the 6 to 15 chars of the demo text.
    # If I select the range of 6 to 15, it will be wrong.
    # How can I select this text by builtin function, but not my user define function?

    # text.SetSelection(6, 15)       # <- this selection will be wrong
    text.SetUnicodeSelection(6, 15)  # <- use user define function will be correct, and the definition is above



The method I’m using can set the correct selection range, but it may cause performance problem in some cases.
For example, when the text is very long (e.g. 100K), and there are many selections (e.g. 3000) that need to be set. I have to encode the long text many times, and calculate the length after encoding, which takes a very long time.
Therefore, I’m wondering if there are any better built-in functions available to achieve this function?