<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
  <META NAME="GENERATOR" CONTENT="GtkHTML/3.14.3">
</HEAD>
<BODY>
In my effort to resurrect my long-ignored work on keeping large dialplans from slowing down the PBX, I unearthed the fact, that somehow, somewhere,&nbsp; around 20% of the performance of the PBX engine was &quot;missing&quot;!!!<BR>
<BR>
Where this performance went, I have no idea. But we all need to think on how we might get them back. I've come up with almost half of them (percentage points of performance, that is) in one optimization, perhaps others of you can come up with others.<BR>
<BR>
My test is a simple tight loop of 4 priorities, I've written about and talked about at conferences previously. Before, about a year ago, I was seeing 90,000 PIPS from my test system. Yesterday, I measured trunk at 76,000 PIPS. With my optimization, I see 83,000 PIPS. <BR>
<BR>
I've used gprof on my fast-ast2 branch of asterisk, and these are the contending routines for optimizations:<BR>
<BR>
&nbsp; %&nbsp;&nbsp; cumulative self&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self&nbsp;&nbsp;&nbsp;&nbsp; total<BR>
time&nbsp;&nbsp; seconds&nbsp; seconds&nbsp;&nbsp;&nbsp; calls&nbsp;&nbsp; s/call&nbsp;&nbsp; s/call&nbsp; name<BR>
23.83&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5.47&nbsp;&nbsp;&nbsp;&nbsp; 5.47&nbsp; 4000005&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; pbx_substitute_variables<BR>
11.69&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8.15&nbsp;&nbsp;&nbsp;&nbsp; 2.68&nbsp; 2000075&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; pbx_substitute_variables_helper_full<BR>
&nbsp; 7.89&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9.96&nbsp;&nbsp;&nbsp;&nbsp; 1.81 31000437&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_copy_string<BR>
&nbsp; 6.93&nbsp;&nbsp;&nbsp;&nbsp; 11.55&nbsp;&nbsp;&nbsp;&nbsp; 1.59&nbsp; 8000024&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_yylex<BR>
&nbsp; 6.76&nbsp;&nbsp;&nbsp;&nbsp; 13.10&nbsp;&nbsp;&nbsp;&nbsp; 1.55&nbsp; 2000004&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_yyparse<BR>
&nbsp; 6.45&nbsp;&nbsp;&nbsp;&nbsp; 14.58&nbsp;&nbsp;&nbsp;&nbsp; 1.48&nbsp; 3000009&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; pbx_retrieve_variable<BR>
&nbsp; 5.02&nbsp;&nbsp;&nbsp;&nbsp; 15.73&nbsp;&nbsp;&nbsp;&nbsp; 1.15&nbsp; 4000068&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_hashtab_hash_string<BR>
&nbsp; 3.66&nbsp;&nbsp;&nbsp;&nbsp; 16.57&nbsp;&nbsp;&nbsp;&nbsp; 0.84&nbsp; 4000023&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; update_scoreboard<BR>
&nbsp; 2.51&nbsp;&nbsp;&nbsp;&nbsp; 17.14&nbsp;&nbsp;&nbsp;&nbsp; 0.58&nbsp; 8000056&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_hashtab_lookup<BR>
&nbsp; 1.74&nbsp;&nbsp;&nbsp;&nbsp; 17.54&nbsp;&nbsp;&nbsp;&nbsp; 0.40&nbsp; 1999999&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_parseable_goto<BR>
&nbsp; 1.29&nbsp;&nbsp;&nbsp;&nbsp; 17.84&nbsp;&nbsp;&nbsp;&nbsp; 0.30&nbsp; 4000014&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; pbx_extension_helper<BR>
&nbsp; 1.26&nbsp;&nbsp;&nbsp;&nbsp; 18.13&nbsp;&nbsp;&nbsp;&nbsp; 0.29&nbsp; 2000004&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_yy_scan_bytes<BR>
&nbsp; 1.24&nbsp;&nbsp;&nbsp;&nbsp; 18.41&nbsp;&nbsp;&nbsp;&nbsp; 0.29&nbsp; 6000018&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; parse_variable_name<BR>
&nbsp; 1.22&nbsp;&nbsp;&nbsp;&nbsp; 18.69&nbsp;&nbsp;&nbsp;&nbsp; 0.28&nbsp; 1000003&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; pbx_builtin_setvar<BR>
&nbsp; 1.05&nbsp;&nbsp;&nbsp;&nbsp; 18.93&nbsp;&nbsp;&nbsp;&nbsp; 0.24&nbsp; 1000000&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; pbx_builtin_gotoif<BR>
&nbsp; 1.00&nbsp;&nbsp;&nbsp;&nbsp; 19.16&nbsp;&nbsp;&nbsp;&nbsp; 0.23&nbsp; 1000003&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; pbx_builtin_setvar_helper<BR>
&nbsp; 0.96&nbsp;&nbsp;&nbsp;&nbsp; 19.38&nbsp;&nbsp;&nbsp;&nbsp; 0.22&nbsp; 3000009&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; substring<BR>
&nbsp; 0.92&nbsp;&nbsp;&nbsp;&nbsp; 19.59&nbsp;&nbsp;&nbsp;&nbsp; 0.21&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp; 0.21&nbsp;&nbsp;&nbsp; 22.66&nbsp; _ast_pbx_run<BR>
&nbsp; 0.87&nbsp;&nbsp;&nbsp;&nbsp; 19.79&nbsp;&nbsp;&nbsp;&nbsp; 0.20&nbsp; 2000002&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; set_one_cid<BR>
&nbsp; 0.87&nbsp;&nbsp;&nbsp;&nbsp; 19.99&nbsp;&nbsp;&nbsp;&nbsp; 0.20&nbsp; 4000014&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; pbx_find_extension<BR>
&nbsp; 0.79&nbsp;&nbsp;&nbsp;&nbsp; 20.17&nbsp;&nbsp;&nbsp;&nbsp; 0.18&nbsp; 2000004&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_yyensure_buffer_stack<BR>
&nbsp; 0.74&nbsp;&nbsp;&nbsp;&nbsp; 20.34&nbsp;&nbsp;&nbsp;&nbsp; 0.17&nbsp; 4000005&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; pbx_exec<BR>
&nbsp; 0.70&nbsp;&nbsp;&nbsp;&nbsp; 20.50&nbsp;&nbsp;&nbsp;&nbsp; 0.16&nbsp; 4000008&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; yy_get_next_buffer<BR>
&nbsp; 0.70&nbsp;&nbsp;&nbsp;&nbsp; 20.66&nbsp;&nbsp;&nbsp;&nbsp; 0.16&nbsp; 2000004&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_yy_scan_string<BR>
&nbsp; 0.65&nbsp;&nbsp;&nbsp;&nbsp; 20.81&nbsp;&nbsp;&nbsp;&nbsp; 0.15&nbsp; 4000005&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_cdr_setapp<BR>
&nbsp; 0.59&nbsp;&nbsp;&nbsp;&nbsp; 20.95&nbsp;&nbsp;&nbsp;&nbsp; 0.14&nbsp; 2000004&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_expr<BR>
&nbsp; 0.57&nbsp;&nbsp;&nbsp;&nbsp; 21.08&nbsp;&nbsp;&nbsp;&nbsp; 0.13&nbsp;&nbsp;&nbsp; 999999&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; op_plus<BR>
&nbsp; 0.48&nbsp;&nbsp;&nbsp;&nbsp; 21.19&nbsp;&nbsp;&nbsp;&nbsp; 0.11&nbsp; 2000004&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; yy_get_previous_state<BR>
&nbsp; 0.46&nbsp;&nbsp;&nbsp;&nbsp; 21.29&nbsp;&nbsp;&nbsp;&nbsp; 0.11 10000020&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_yyfree<BR>
&nbsp; 0.44&nbsp;&nbsp;&nbsp;&nbsp; 21.39&nbsp;&nbsp;&nbsp;&nbsp; 0.10&nbsp; 1000004&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_var_assign<BR>
&nbsp; 0.39&nbsp;&nbsp;&nbsp;&nbsp; 21.48&nbsp;&nbsp;&nbsp;&nbsp; 0.09&nbsp; 2000004&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_yy_scan_buffer<BR>
&nbsp; 0.37&nbsp;&nbsp;&nbsp;&nbsp; 21.57&nbsp;&nbsp;&nbsp;&nbsp; 0.09&nbsp; 2000004&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_yylex_destroy<BR>
&nbsp; 0.35&nbsp;&nbsp;&nbsp;&nbsp; 21.65&nbsp;&nbsp;&nbsp;&nbsp; 0.08&nbsp; 4000042&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; hashtab_compare_contexts<BR>
&nbsp; 0.31&nbsp;&nbsp;&nbsp;&nbsp; 21.72&nbsp;&nbsp;&nbsp;&nbsp; 0.07&nbsp; 4000012&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; free_value<BR>
&nbsp; 0.26&nbsp;&nbsp;&nbsp;&nbsp; 21.78&nbsp;&nbsp;&nbsp;&nbsp; 0.06&nbsp; 2000004&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_yylex_init<BR>
&nbsp; 0.26&nbsp;&nbsp;&nbsp;&nbsp; 21.84&nbsp;&nbsp;&nbsp;&nbsp; 0.06&nbsp; 1999999&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_explicit_goto<BR>
&nbsp; 0.26&nbsp;&nbsp;&nbsp;&nbsp; 21.90&nbsp;&nbsp;&nbsp;&nbsp; 0.06&nbsp; 1000000&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; op_lt<BR>
&nbsp; 0.26&nbsp;&nbsp;&nbsp;&nbsp; 21.96&nbsp;&nbsp;&nbsp;&nbsp; 0.06&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9632&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_strip<BR>
&nbsp; 0.24&nbsp;&nbsp;&nbsp;&nbsp; 22.01&nbsp;&nbsp;&nbsp;&nbsp; 0.06&nbsp; 4000008&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_yy_load_buffer_state<BR>
&nbsp; 0.22&nbsp;&nbsp;&nbsp;&nbsp; 22.06&nbsp;&nbsp;&nbsp;&nbsp; 0.05&nbsp; 8000959&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; ast_check_hangup<BR>
&nbsp; 0.22&nbsp;&nbsp;&nbsp;&nbsp; 22.11&nbsp;&nbsp;&nbsp;&nbsp; 0.05&nbsp; 1999999&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; pbx_builtin_goto<BR>
&nbsp; 0.17&nbsp;&nbsp;&nbsp;&nbsp; 22.15&nbsp;&nbsp;&nbsp;&nbsp; 0.04&nbsp; 5000017&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp; _init_manager_event_buf<BR>
<BR>
<BR>
The list above is just the top contenders. Since the loop is executed a million times, <BR>
all the functions called a million or more times were involved in the &quot;core&quot; of<BR>
the PBX (only SETs/no-ops/gotoIf apps used, along with ${} and $[] expressions.)<BR>
<BR>
Most immediately apparent is that any optimizations to the substitute_variables<BR>
functions could yield HUGE speedups in the code. (And, conversely, any additions<BR>
will immediately slow down asterisk). I plan to overhaul the entire parsing/evaluation<BR>
mechanics for AEL3, which hopefully speed up this stuff, but in the meantime,<BR>
any speedups anywhere might yield some good results for us all.<BR>
<BR>
Note: here's the extensions.conf version of the dialplan I used. Zap lines are set up<BR>
so when an extension is picked up, you are in the &quot;extension&quot; context. Dial &quot;81&quot; to start<BR>
the test.<BR>
<BR>
<BR>
[dialextens]<BR>
exten =&gt; _10X,1,Dial(Zap/${EXTEN:2}|30|tw)<BR>
exten =&gt; _1ZX,1,Dial(Zap/${EXTEN:1}|30|tw)<BR>
<BR>
<BR>
[dialthrus]<BR>
exten =&gt; _3XX,1,Dial(Zap/${EXTEN:1}|30|tw)<BR>
<BR>
<BR>
[t1incoming]<BR>
include =&gt; dialextens<BR>
include =&gt; parkedcalls<BR>
exten =&gt; s,1,Answer()<BR>
exten =&gt; s,2,Background(welcome-to-test-machine)<BR>
<BR>
<BR>
[incoming]<BR>
include =&gt; dialextens<BR>
include =&gt; parkedcalls<BR>
exten =&gt; s,1,Answer()<BR>
exten =&gt; s,2,Background(welcome-to-test-machine)<BR>
<BR>
<BR>
[extension]<BR>
include =&gt; dialextens<BR>
include =&gt; dialthrus<BR>
exten =&gt; 5,1,Record(recording:gsm)<BR>
exten =&gt; 5,2,Background(recording)<BR>
exten =&gt; 81,1,Set(iterations=$[1000000])<BR>
exten =&gt; 81,2,Set(time1=${EPOCH})<BR>
exten =&gt; 81,3,Set(i=$[1])<BR>
<FONT COLOR="#ff6600">exten =&gt; 81,4,GotoIf($[${i}&lt;${iterations}]?5:8)</FONT><BR>
<FONT COLOR="#ff6600">exten =&gt; 81,5,NoOp(Hello)</FONT><BR>
<FONT COLOR="#ff6600">exten =&gt; 81,6,Set(i=$[${i}+1])</FONT><BR>
<FONT COLOR="#ff6600">exten =&gt; 81,7,Goto(4)</FONT><BR>
exten =&gt; 81,8,NoOp(Finish for-extension-1)<BR>
exten =&gt; 81,9,Set(time2=${EPOCH})<BR>
exten =&gt; 81,10,Verbose(The time diff is $[${time2} - ${time1} ] seconds)<BR>
exten =&gt; 81,11,Verbose(Which means that the priorities/sec = $[4* ${iterations} / (${time2} - ${time1}) ])<BR>
exten =&gt; 81,12,SayNumber($[4 * ${iterations} / (${time2} - ${time1}) ])<BR>
exten =&gt; 82,1,NoOp(Never here)<BR>
exten =&gt; 82,2,Verbose(Not here either!)<BR>
exten =&gt; 83,1,Read(x|beep|1||1|0.3)<BR>
exten =&gt; 83,2,Read(x|beep|1||1|0.3)<BR>
exten =&gt; 83,3,Read(x|beep|1||1|0.3)<BR>
<BR>
murf<BR>
<TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<PRE>
-- 
Steve Murphy
Software Developer
Digium
</PRE>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>