Browse code

- store all the module response_f callbacks in an array (small performance improvement when lots of modules are loaded)

Andrei Pelinescu-Onciul authored on 29/02/2008 20:21:58
Showing 3 changed files
... ...
@@ -556,7 +556,7 @@ int forward_reply(struct sip_msg* msg)
556 556
 	char* new_buf;
557 557
 	struct dest_info dst;
558 558
 	unsigned int new_len;
559
-	struct sr_module *mod;
559
+	int r;
560 560
 #ifdef USE_TCP
561 561
 	char* s;
562 562
 	int len;
... ...
@@ -575,15 +575,10 @@ int forward_reply(struct sip_msg* msg)
575 575
 			goto error;
576 576
 		}
577 577
 	}
578
-	/* quick hack, slower for multiple modules*/
579
-	for (mod=modules;mod;mod=mod->next){
580
-		if ((mod->exports) && (mod->exports->response_f)){
581
-			DBG("forward_reply: found module %s, passing reply to it\n",
582
-					mod->exports->name);
583
-			if (mod->exports->response_f(msg)==0) goto skip;
584
-		}
585
-	}
586
-
578
+	
579
+	/* check modules response_f functions */
580
+	for (r=0; r<mod_response_cbk_no; r++)
581
+		if (mod_response_cbks[r](msg)==0) goto skip;
587 582
 	/* we have to forward the reply stateless, so we need second via -bogdan*/
588 583
 	if (parse_headers( msg, HDR_VIA2_F, 0 )==-1 
589 584
 		|| (msg->via2==0) || (msg->via2->error!=PARSE_OK))
... ...
@@ -36,6 +36,7 @@
36 36
  *  2005-01-07  removed find_module-overloading problems, added
37 37
  *               find_export_record
38 38
  *  2006-02-07  added fix_flag (andrei)
39
+ *  2008-02-29  store all the reponse callbacks in their own array (andrei)
39 40
  */
40 41
 
41 42
 
... ...
@@ -89,6 +90,10 @@ struct sr_module* modules=0;
89 90
 #endif
90 91
 
91 92
 
93
+int mod_response_cbk_no=0;
94
+response_function* mod_response_cbks=0;
95
+
96
+
92 97
 /* initializes statically built (compiled in) modules*/
93 98
 int register_builtin_modules()
94 99
 {
... ...
@@ -455,6 +460,10 @@ void destroy_modules()
455 460
 		t=foo;
456 461
 	}
457 462
 	modules=0;
463
+	if (mod_response_cbks){
464
+		pkg_free(mod_response_cbks);
465
+		mod_response_cbks=0;
466
+	}
458 467
 }
459 468
 
460 469
 #ifdef NO_REVERSE_INIT
... ...
@@ -474,6 +483,21 @@ int init_modules(void)
474 483
 							" module %s\n", t->exports->name);
475 484
 				return -1;
476 485
 			}
486
+		if ( t->exports && t->exports->response_f)
487
+			mod_response_cbk_no++;
488
+	}
489
+	mod_response_cbks=pkg_malloc(mod_response_cbk_no * 
490
+									sizeof(response_function));
491
+	if (mod_response_cbks==0){
492
+		LOG(L_ERR, "init_modules(): memory allocation failure"
493
+					" for %d response_f callbacks\n", mod_response_cbk_no);
494
+		return -1;
495
+	}
496
+	for (t=modules, i=0; t && (i<mod_response_cbk_no); t=t->next){
497
+		if (t->exports && t->exports->response_f){
498
+			mod_response_cbks[i]=t->exports->response_f;
499
+			i++;
500
+		}
477 501
 	}
478 502
 	return 0;
479 503
 }
... ...
@@ -591,6 +615,26 @@ static int init_mod( struct sr_module* m )
591 615
  */
592 616
 int init_modules(void)
593 617
 {
618
+	struct sr_module* t;
619
+	int i;
620
+	
621
+	for(t = modules; t; t = t->next)
622
+		if ( t->exports && t->exports->response_f)
623
+			mod_response_cbk_no++;
624
+	mod_response_cbks=pkg_malloc(mod_response_cbk_no * 
625
+									sizeof(response_function));
626
+	if (mod_response_cbks==0){
627
+		LOG(L_ERR, "init_modules(): memory allocation failure"
628
+					" for %d response_f callbacks\n", mod_response_cbk_no);
629
+		return -1;
630
+	}
631
+	for (t=modules, i=0; t && (i<mod_response_cbk_no); t=t->next){
632
+		if (t->exports && t->exports->response_f){
633
+			mod_response_cbks[i]=t->exports->response_f;
634
+			i++;
635
+		}
636
+	}
637
+	
594 638
 	return init_mod(modules);
595 639
 }
596 640
 
... ...
@@ -189,7 +189,9 @@ struct sr_module{
189 189
 };
190 190
 
191 191
 
192
-struct sr_module* modules; /* global module list*/
192
+extern struct sr_module* modules; /* global module list*/
193
+extern response_function* mod_response_cbks;/* response callback array */
194
+extern int mod_response_cbk_no;    /* size of reponse callbacks array */
193 195
 
194 196
 int register_builtin_modules();
195 197
 int register_module(struct module_exports*, char*,  void*);