Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
ennye
Novice


Joined: 17 Dec 2007
Posts: 49

PostPosted: Tue Apr 29, 2008 12:18 am   

[CMUD] Speed optimization hints and rules.
 
General feeling since using cmud after years in zmud is... a bit faster!
But.. ctrl+q test says muuuch sloower. (5,0 zmud to 15,0 cmud.)
Always tried to optimize my scripts but it was always based on unprooven users hints or general programming rules.

Post here your hints about script speed optimization.
Prooved hints please and clear discussion.
Maybe this should go into FAQ section some day.
Any1 with better forum experience plz keep discussion ;)
Reply with quote
ennye
Novice


Joined: 17 Dec 2007
Posts: 49

PostPosted: Tue Apr 29, 2008 12:24 am   
 
To better understand speed issues can any1 explain how trigger matching is made? Or point to relative topic ?
Reply with quote
ennye
Novice


Joined: 17 Dec 2007
Posts: 49

PostPosted: Tue Apr 29, 2008 12:29 am   
 
Triggers (no variable write):
Quote:

1)
^Test pattern blue.$
^Test pattern black.$
vs.
^Test pattern {blue|black}.$


Quote:

2)
^{Test pattern blue.|Test pattern black.}$
vs
^Test pattern {blue|black}.$


Quote:

3)
^Test pattern {blue|black|...|100other_elements}.$
vs
^Test pattern {@color}.$


Quote:

4)
^Test pattern *.$
vs
^Test pattern %w.$


Quote:

5)
^This is very long and boring phrase used as trigger pattern example.$ (one of 10 other triggers matching 10 other long phrases)
vs
* example.$ (1 trigger matching 10 other phrases)
vs
^This is * example.$ (1 trigger matching 10 other phrases)
vs
^This is {@cutpart} example.$ (1 trigger matching 10 other phrases using variable @cutpart)




Triggers With variable assignment:
Quote:

6)
^Test pattern (*).$ (color=%1 in value)
vs
^Test pattern (%w).$ (color=%1 in value)
vs
^Test pattern &color.$




How about leaving out some part of variable? FE. :
Quote:

7)
#va testvar {Phrase1|Phrase2|Phrase3} (1000 phrases)
Trigger: ^{@testvar}.$
OR
#va testvar {Phrase1.|Phrase2.|Phrase3.}
Trigger: ^{@testvar}$

What is matched first? Checking all possible lines or checking variable part first or checking if colon part is present right before endline mark first ? Does it make any speed difference?


Last edited by ennye on Tue Apr 29, 2008 12:58 am; edited 1 time in total
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Tue Apr 29, 2008 12:45 am   
 
Trigger matching just uses the regex engine - you can see the regex that's being used for your pattern on the Test Pattern tab. There're many guides, tutorials, articles and tools out there that explain how regex matching works and give tips for optimising them.

If you're unsure about which option is going to be faster, the easiest way is to use the untitled session and write a script that'll create a couple of hundred triggers of each type. Then disable all the options but one and see how long it takes. Repeat for each option.

A couple of things you need to know about speed testing:
1) It'll be slower if the buffer isn't full, because CMUD needs to allocate memory to hold the lines in the buffer. Press Ctrl+Q a few times to fill up the buffer before you start speed testing.
2) It'll be slower the first time you run each trigger in a session, because CMUD needs to compile them first. Again, press Ctrl+Q before you start speed testing to give you an accurate result.

Most importantly, though, is that speed depends on many things. You should look into optimising your scripts as well as your patterns using local variables, events and other features that've been added to CMUD.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Tue Apr 29, 2008 12:53 am   
 
Also, tell us what version of Windows you are using. Ctrl-Q speed is significantly slower on Vista for example.

Trigger matching is done using the industry standard PCRE library (Perl-Compatible Regular Expressions). However, with CMUD's compiler and caching, there are many different optimizations, all at too low of a level to really describe here.

Honestly, the best way to answer your specific examples is to try them in a blank CMUD session and use the Ctrl-Q test to compare speeds. But very often you will find that it will depend upon the exact text you are trying to match.

In *general*, using a single trigger is faster than multiple triggers (your first example). Btw, it would help if you numbered your examples to make them easier to refer to.

CMUD used regular expressions (PCRE) internally. PCRE doesn't have any knowledge of stuff like CMUD variables. So CMUD performs a conversion step before calling PCRE. For example, when using a @variable in a trigger pattern, CMUD expands the variable before sending it to the PCRE library. CMUD caches this result and continues to use the cached value until the @variable changes value. So it doesn't actually matter whether you use a variable of whether you put all of the text into the trigger verbatim. The same end-result gets sent to the PCRE library.

Using &varname in a pattern is really the same as using "var=%1" in the value. Again, PCRE doesn't know about CMUD variables. So when CMUD converts this pattern to a regular expression, the &varname is replaced with (.*) and the variable name is stored for later. If the pattern matches, then CMUD internally does the "var=%1" to assign the command.

None of the things that you mentioned should really have much difference between CMUD and zMUD...both use the PCRE library. A good tool to look for speed issues in CMUD is to open the Script Debugger in the Window menu. This will slow down your scripts, but you'll get to see exactly what triggers are firing and what scripts are running, along with timestamp values so you can see what scripts are slow.

Another good tip is to put your various triggers into class folders, and then disable the class folders and do the Ctrl-Q test to determine where your slowest triggers are. Once you identify the triggers that are the slowest, then you can ask about them specifically for optimization.

Finally, try to avoid putting a * at the beginning or end of a pattern. CMUD already does this by default (unless you anchor it with a ^ or $). So adding your own * just makes PCRE take longer (since now it has two wildcards together).
Reply with quote
ennye
Novice


Joined: 17 Dec 2007
Posts: 49

PostPosted: Tue Apr 29, 2008 12:55 am   
 
I understand, that ctrl+q test measures JUST matching speed, not result of matching. It doesnt fire any triggers ?
So point of my question is only matching purposes of any optimizations.

Not everything goes with regex i think. For example how about nesting triggers deeper inside class folder tree... have read somewhere its much slower.
Reply with quote
ennye
Novice


Joined: 17 Dec 2007
Posts: 49

PostPosted: Tue Apr 29, 2008 1:21 am   
 
So there is no real speed difference in:

Quote:
ONE 1000 elements variable "creatures" (fe {xxx yyy orc|xxx zzz orc(500 elements)|xxx yyy goblin|xxx yyy goblin(500 elements)} and make 1 trigger {@creatures}


than making

Quote:
2 variables "orcs" "goblins" having 500 elements each {xxx yyy|xxx zzz} and 2 triggers "{@orcs} orc" and "{@goblins} goblin"


?
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Tue Apr 29, 2008 2:51 am   
 
ennye wrote:
ctrl+q test measures JUST matching speed, not result of matching.

This is false. Ctrl+Q just prints 1000 lines reading "abcdefghijklmnopqrstuvwxyz 0123456789" to the screen. Your triggers probably won't match that text, but if they do, their scripts will be run. Test case:

#trig {%w %d} {#add var 1}
press Ctrl+Q
#say @var

It's significant that your triggers probably won't match these lines, because using this kind of garbage line isn't always the best way to test your triggers. It's likely that many of your triggers will be rejected very quickly, when in actual play they'll be rejected much more slowly (because lines from your MUD may tend to match part of the pattern more often). Optimising regexes is very difficult because of this - it's often possible to optimise your regexes to fail faster, but that's usually at the expense of causing them to match more slowly. It's a knotty issue.

And in response to your second post, the simple answer is "try it and see". The first will probably be faster, but just write a test case and find out.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
ennye
Novice


Joined: 17 Dec 2007
Posts: 49

PostPosted: Tue Apr 29, 2008 10:47 am   
 
Quote:
This is false. Ctrl+Q just prints 1000 lines reading "abcdefghijklmnopqrstuvwxyz 0123456789" to the screen. Your triggers probably won't match that text,


Isnt it matching speed? :) Matching has 2 possibilities... match or do not match. So this test actualy tests the quality of our trigger patterns(more sophisticated or more non-optimized time consuming queries..) Thats what i was asking for.

So best way to test overall performance of our scripts would be sending full buffer (from log) as it came from mud, and measure times. (Not possible now) CtrlQ is quite useless unless you want to know only how your patterns look like.
Reply with quote
Larkin
Wizard


Joined: 25 Mar 2003
Posts: 1113
Location: USA

PostPosted: Tue Apr 29, 2008 11:27 am   
 
ennye wrote:
Isnt it matching speed? :) Matching has 2 possibilities... match or do not match. So this test actualy tests the quality of our trigger patterns(more sophisticated or more non-optimized time consuming queries..) Thats what i was asking for.


There are two possible end results, yes, but the path taken to get there is what matters more. Some trigger patterns go through more steps before they fail to match, thus they are slower. There is at least one thread around here on the use of atomic expressions in regex trigger patterns to reduce the number of steps (preventing some of the backtracking that normally occurs).
Reply with quote
gamma_ray
Magician


Joined: 17 Apr 2005
Posts: 496

PostPosted: Wed Apr 30, 2008 9:51 pm   
 
Atomic grouping is what Larkin's talking about, it's pretty interesting stuff.
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net