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 89
 #endif
90 90
 
91 91
 
92
+int mod_response_cbk_no=0;
93
+response_function* mod_response_cbks=0;
94
+
95
+
92 96
 /* initializes statically built (compiled in) modules*/
93 97
 int register_builtin_modules()
94 98
 {
... ...
@@ -455,6 +460,10 @@ void destroy_modules()
455 455
 		t=foo;
456 456
 	}
457 457
 	modules=0;
458
+	if (mod_response_cbks){
459
+		pkg_free(mod_response_cbks);
460
+		mod_response_cbks=0;
461
+	}
458 462
 }
459 463
 
460 464
 #ifdef NO_REVERSE_INIT
... ...
@@ -474,6 +483,21 @@ int init_modules(void)
474 474
 							" module %s\n", t->exports->name);
475 475
 				return -1;
476 476
 			}
477
+		if ( t->exports && t->exports->response_f)
478
+			mod_response_cbk_no++;
479
+	}
480
+	mod_response_cbks=pkg_malloc(mod_response_cbk_no * 
481
+									sizeof(response_function));
482
+	if (mod_response_cbks==0){
483
+		LOG(L_ERR, "init_modules(): memory allocation failure"
484
+					" for %d response_f callbacks\n", mod_response_cbk_no);
485
+		return -1;
486
+	}
487
+	for (t=modules, i=0; t && (i<mod_response_cbk_no); t=t->next){
488
+		if (t->exports && t->exports->response_f){
489
+			mod_response_cbks[i]=t->exports->response_f;
490
+			i++;
491
+		}
477 492
 	}
478 493
 	return 0;
479 494
 }
... ...
@@ -591,6 +615,26 @@ static int init_mod( struct sr_module* m )
591 591
  */
592 592
 int init_modules(void)
593 593
 {
594
+	struct sr_module* t;
595
+	int i;
596
+	
597
+	for(t = modules; t; t = t->next)
598
+		if ( t->exports && t->exports->response_f)
599
+			mod_response_cbk_no++;
600
+	mod_response_cbks=pkg_malloc(mod_response_cbk_no * 
601
+									sizeof(response_function));
602
+	if (mod_response_cbks==0){
603
+		LOG(L_ERR, "init_modules(): memory allocation failure"
604
+					" for %d response_f callbacks\n", mod_response_cbk_no);
605
+		return -1;
606
+	}
607
+	for (t=modules, i=0; t && (i<mod_response_cbk_no); t=t->next){
608
+		if (t->exports && t->exports->response_f){
609
+			mod_response_cbks[i]=t->exports->response_f;
610
+			i++;
611
+		}
612
+	}
613
+	
594 614
 	return init_mod(modules);
595 615
 }
596 616
 
... ...
@@ -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*);