Browse code

- tcp optimization: in tcp_reader try reading from a fd immediately after receiving it (since it receives it only if there's data waiting on it => avoids a few sys calls before starting readingi or for a bad connect attempt)

Andrei Pelinescu-Onciul authored on 12/01/2008 15:17:40
Showing 1 changed files
... ...
@@ -619,15 +619,16 @@ again:
619 619
 							   because we always alloc BUF_SIZE+1 */
620 620
 			*req->parsed=0;
621 621
 #ifdef USE_STUN
622
-			if (req->state==H_STUN_END){
622
+			if (unlikely(req->state==H_STUN_END)){
623 623
 				/* stun request */
624 624
 				ret = stun_process_msg(req->start, req->parsed-req->start,
625 625
 									 &con->rcv);
626 626
 			}else
627 627
 #endif
628
-				ret = receive_msg(req->start, req->parsed-req->start, &con->rcv);
628
+				ret = receive_msg(req->start, req->parsed-req->start,
629
+									&con->rcv);
629 630
 				
630
-			if (ret < 0) {
631
+			if (unlikely(ret < 0)) {
631 632
 				*req->parsed=c;
632 633
 				resp=CONN_ERROR;
633 634
 				goto end_req;
... ...
@@ -660,7 +661,7 @@ again:
660 660
 		
661 661
 		
662 662
 	end_req:
663
-		if (bytes_read) *bytes_read=total_bytes;
663
+		if (likely(bytes_read)) *bytes_read=total_bytes;
664 664
 		return resp;
665 665
 }
666 666
 
... ...
@@ -784,6 +785,18 @@ again:
784 784
 							con, con->id, atomic_get(&con->refcnt));
785 785
 				goto con_error;
786 786
 			}
787
+			/* if we received the fd there is most likely data waiting to
788
+			 * be read => process it first to avoid extra sys calls */
789
+			resp=tcp_read_req(con, &n);
790
+			if (unlikely(resp<0)){
791
+				/* some error occured, but on the new fd, not on the tcp
792
+				 * main fd, so keep the ret value */
793
+				if (unlikely(resp!=CONN_EOF))
794
+					con->state=S_CONN_BAD;
795
+				release_tcpconn(con, resp, tcpmain_sock);
796
+				break;
797
+			}
798
+			
787 799
 			/* must be before io_watch_add, io_watch_add might catch some
788 800
 			 * already existing events => might call handle_io and
789 801
 			 * handle_io might decide to del. the new connection =>