jacobslusser / ScintillaNET

A Windows Forms control, wrapper, and bindings for the Scintilla text editor.
MIT License
965 stars 245 forks source link

[Question] the Text property and searching #254

Closed zhaDeth closed 8 years ago

zhaDeth commented 8 years ago

I used vb's Regex to find matches in the text and Since I wanted it to search all the text, I passed the "Text" property (byRef), thinking it would be faster than passing a whole copy of the text but it didn't work like I expected:

a) Even tho the Text property is actually a copy of the whole text (according to the wiki), I guessed the Text property is only updated if the text has changed since the last time the property was accessed, is it ? Or is it copied each time ?

b) Passing the Text property byRef seems to make it think it was modified. I tried making an empty sub with a byRef string as the only parameter and passing Text to it triggers the BeforeDelete event with the whole text, so I guess it probably deletes the whole text and re-write it every time which means I should probably pass it byVal but then it will make a copy each time and if im wrong about a), this means it is copied twice each time..

c) Avoiding using Text is most probably the anwser to my Regex searching problem, I since noticed that the Scintilla object has regex searching in it so that should keep the whole text from being copied. And for other times where I need get the whole text and pass it byRef, I can use getTextRange(0,TextLength-1) to make sure it doesnt think modifications have been made, but doesnt that mean beign able to get the whole text from Text possibly slower than from getTextRange, and if im wrong about a), doesnt it make using Text to get the whole text something that should always be avoided ?

I don't mean to ask for changes on how the Text property behaves, but I thought about updating the wiki with some clarifications about the stuff I noticed and don't want to go inserting false infos in there and as I am not familiar with C#, I didn't trust the answers I would find from reading the source code.

jacobslusser commented 8 years ago

a). Scintilla will allocate a new String each time you get the Text property. There is no internal mechanism for change detection, but you could easily implement your own wrapper which monitors the change events and invalidates the cache.

b). I'm not an expert in VB.NET at all.. but I don't think passing Text ByVal will create a new copy of the string -- it will create a copy of the string pointer (the information which says where the string is located in memory) but not the string contents.

c). If you can use the regex features built into Scintilla that is ideal because it will avoid making a copy of the Text property, however, they are admittedly very limited. I could check the source code, but I believe internally the Text property is roughly equivalent to GetTextRange(0, TextLength).

In summary, you might be overthinking this a bit. Performance testing will tell you everything you need to know. Avoid "premature optimization". Chances are an occasional regex over the Text property is not going to adversely affect your application performance at all.

What are you doing with your regex and how often will you be doing it?

zhaDeth commented 8 years ago

b) you are right, vb's strings are actually pointers, passing byVal only copies the pointer, I remember seeing an increase in performance by passing them byRef but I probably made other changes and thought the byRef was what made it faster and used byRef with strings ever since

im making a super nintendo ROM disassembly editor, I use regex to find text matching certain patterns to apply visual styles and keep track of stuff. The disassemblies are split in about 1MB .txt files, contain assembly code, comments, raw bytes for tables and gfx etc.. the goal is to use the existing txt files and use regex to cope with the fact that many people edited the files and didn't always use the same syntax, which makes some of the regex very complicated and slow. I managed to keep my app running fast by only using the slower ones when opening files, otherwise even by only using the text that requires styling, typing text was slow. I will save the files in a custom format and make everything coherent so that I dont have to use such complicated regex, make file opening faster and hopefull apply styling to everything dynamically.

jacobslusser commented 8 years ago

I love your project idea! I actually tried to create my own NES emulator in C# (called it SweetNES which I thought was pretty clever) but never finished it. I got it to the point where it could run the 6502 processor code, but lost interest trying to emulate the graphics and sound.

Have you read the guide yet on Custom Syntax Highlighting? Also there have been a few good discussions in the forums about syntax highlighting and regex that you might find helpful such as #226, #76, #49, etc.... just search the forums.

If you are interested I would be happy to review any of your code and make suggestions if you want to PM it to me.

jslusser [dot] mobile [at] gmail [dot] com