多线程文本处理的成本/收益

时间:2022-04-01 21:01:12

I am working on a real-time syntax highlighter for the iPhone and I have created a custom UIView that takes a string, parses it and then highlights it in its drawRect: method. I've also implemented a blinking cursor. However, it is starting to get a little slow and I think that when I implement multi-line processing and chunk-processing, it will slow it down more. However, I tried placing the [formattedTextView setNeedsDisplayInRect:] call in a function in my view controller and then calling on a separate thread using [self performSelectorInBackground:@selector(updateDisplay) withObject:nil]. The keyboard is more responsive now, but this seems like a bad use of threads on a single-core processor.

我正在为iPhone制作一个实时语法荧光笔,我创建了一个自定义UIView,它接受一个字符串,解析它,然后在其drawRect:方法中突出显示它。我还实现了一个闪烁的光标。然而,它开始变得有点慢,我认为当我实现多行处理和块处理时,它会减慢更多。但是,我尝试将[formattedTextView setNeedsDisplayInRect:]调用放在我的视图控制器中的函数中,然后使用[self performSelectorInBackground:@selector(updateDisplay)withObject:nil]调用单独的线程。键盘现在响应更快,但这似乎是在单核处理器上使用线程很糟糕。

Are there any problems with doing something like this?

做这样的事情有什么问题吗?

Thanks

2 个解决方案

#1


As you pointed out yourself, on a single processor multithreading will not bring a huge performance boost, but will come with a stability and complexity penalty.

正如您自己指出的那样,在单处理器上,多线程不会带来巨大的性能提升,但会带来稳定性和复杂性的损失。

On-the-fly Syntax-coloring is a hard problem, stuffed with possibilites for optimization:

即时语法着色是一个难题,充满了可能性以进行优化:

  • Are you applying a bunch of Regexes on the whole text? (bad) or do you parse the text to be held in an efficient datastructure like an ast?
  • 你在整个文本中应用了一堆正则表达式吗? (糟糕的)或者您是否解析了像ast这样有效的数据结构中的文本?

  • Are you limiting the colorized painting to the visible area?
  • 您是否将彩色绘画限制在可见区域?

#2


I am working on a real-time syntax highlighter

我正在研究一种实时语法荧光笔

Are you doing keyword highlighting or have you written a BNF parser?

你在做关键字突出显示还是你写过BNF解析器?

For the latter, why not have it running in a thread; the GUI simply displays the colouring of all that's been decoded so far. That way you get instant update but delayed colouring.

对于后者,为什么不让它在一个线程中运行; GUI只显示到目前为止已解码的所有颜色。这样你就可以获得即时更新但延迟着色。

#1


As you pointed out yourself, on a single processor multithreading will not bring a huge performance boost, but will come with a stability and complexity penalty.

正如您自己指出的那样,在单处理器上,多线程不会带来巨大的性能提升,但会带来稳定性和复杂性的损失。

On-the-fly Syntax-coloring is a hard problem, stuffed with possibilites for optimization:

即时语法着色是一个难题,充满了可能性以进行优化:

  • Are you applying a bunch of Regexes on the whole text? (bad) or do you parse the text to be held in an efficient datastructure like an ast?
  • 你在整个文本中应用了一堆正则表达式吗? (糟糕的)或者您是否解析了像ast这样有效的数据结构中的文本?

  • Are you limiting the colorized painting to the visible area?
  • 您是否将彩色绘画限制在可见区域?

#2


I am working on a real-time syntax highlighter

我正在研究一种实时语法荧光笔

Are you doing keyword highlighting or have you written a BNF parser?

你在做关键字突出显示还是你写过BNF解析器?

For the latter, why not have it running in a thread; the GUI simply displays the colouring of all that's been decoded so far. That way you get instant update but delayed colouring.

对于后者,为什么不让它在一个线程中运行; GUI只显示到目前为止已解码的所有颜色。这样你就可以获得即时更新但延迟着色。