views:

124

answers:

2

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.

Are there any problems with doing something like this?

Thanks

+1  A: 

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?
  • Are you limiting the colorized painting to the visible area?

Greets

Seb

flitzwald
I ended up removing the background-processing because it was causing a delay if many characters were typed in quick succession.As for your other points:- I am applying the regexes to the currently editable line.- I only update the current line by calling setNeedsDisplayInRect:, the rest of the UIView stays colored.It seems to me that the main cause of the slowdown is the blinking cursor (a CALayer with a CABasicAnimation set up in awakeFromNib) is there some way to optimize this animation?Also, what is an ast? I haven't heard of that before, I'm using a NSMutableArray.
Kyle
A: 

I am working on a real-time syntax highlighter

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

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.

Blank Xavier
I'm doing keyword highlighting with regular expressions, an actual parser would be nice, but I'm not sure I can write one.Thanks,Kyle
Kyle
A BNF parser is much easier than you think - it's a conceptual leap that's required, where you need to get your brain round the basic concept - the actual coding is pretty straightforward.
Blank Xavier
Are you aware of any code/articles that would help me get my brain around the concept? Because a parser would be far superior to mere keyword highlighting. Thanks
Kyle