--------------------- PatchSet 7056 Date: 2005/09/27 06:32:20 Author: adri Branch: tidyup_deferred_reads Tag: (none) Log: Stop wasting a call to mempoolalloc/free when the structure is only ever used one at a time per FD. Its not much of an optimisation, but its definitely cleaner. Members: src/comm.c:1.18.6.5.6.1->1.18.6.5.6.2 src/structs.h:1.48.2.34.4.1->1.48.2.34.4.2 Index: squid/src/comm.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/comm.c,v retrieving revision 1.18.6.5.6.1 retrieving revision 1.18.6.5.6.2 diff -u -r1.18.6.5.6.1 -r1.18.6.5.6.2 --- squid/src/comm.c 27 Sep 2005 04:57:14 -0000 1.18.6.5.6.1 +++ squid/src/comm.c 27 Sep 2005 06:32:20 -0000 1.18.6.5.6.2 @@ -1,6 +1,6 @@ /* - * $Id: comm.c,v 1.18.6.5.6.1 2005/09/27 04:57:14 adri Exp $ + * $Id: comm.c,v 1.18.6.5.6.2 2005/09/27 06:32:20 adri Exp $ * * DEBUG: section 5 Socket Functions * AUTHOR: Harvest Derived @@ -73,32 +73,32 @@ static int commRetryConnect(ConnectStateData * cs); CBDATA_TYPE(ConnectStateData); -static MemPool *comm_write_pool = NULL; static MemPool *conn_close_pool = NULL; static void CommWriteStateCallbackAndFree(int fd, int code) { - CommWriteStateData *CommWriteState = fd_table[fd].rwstate; - CWCB *callback = NULL; - void *data; - fd_table[fd].rwstate = NULL; - if (CommWriteState == NULL) - return; - if (CommWriteState->free_func) { - FREE *free_func = CommWriteState->free_func; - void *free_buf = CommWriteState->buf; - CommWriteState->free_func = NULL; - CommWriteState->buf = NULL; - free_func(free_buf); - } - callback = CommWriteState->handler; - data = CommWriteState->handler_data; - CommWriteState->handler = NULL; + CommWriteStateData CommWriteState = fd_table[fd].rwstate; + CWCB *callback = CommWriteState.handler; + void *data = CommWriteState.handler_data; + size_t offset = CommWriteState.offset; + FREE *free_func = CommWriteState.free_func; + void *buf = CommWriteState.buf; + + if (callback == NULL) + return; + + fd_table[fd].rwstate.handler = NULL; + + if (free_func != NULL) { + free_func(buf); + buf = NULL; + free_func = NULL; + } + if (callback && cbdataValid(data)) - callback(fd, CommWriteState->buf, CommWriteState->offset, code, data); + callback(fd, buf, offset, code, data); cbdataUnlock(data); - memPoolFree(comm_write_pool, CommWriteState); } /* Return the local port associated with fd. */ @@ -869,7 +869,6 @@ * Since Squid_MaxFD can be as high as several thousand, don't waste them */ RESERVED_FD = XMIN(100, Squid_MaxFD / 4); CBDATA_INIT_TYPE(ConnectStateData); - comm_write_pool = memPoolCreate("CommWriteStateData", sizeof(CommWriteStateData)); conn_close_pool = memPoolCreate("close_handler", sizeof(close_handler)); } @@ -938,15 +937,14 @@ void comm_write(int fd, const char *buf, int size, CWCB * handler, void *handler_data, FREE * free_func) { - CommWriteStateData *state = fd_table[fd].rwstate; + CommWriteStateData *state = &fd_table[fd].rwstate; debug(5, 5) ("comm_write: FD %d: sz %d: hndl %p: data %p.\n", fd, size, handler, handler_data); - if (NULL != state) { - debug(5, 1) ("comm_write: fd_table[%d].rwstate != NULL\n", fd); - memPoolFree(comm_write_pool, state); - fd_table[fd].rwstate = NULL; + + if (state->handler != NULL) { + debug(5, 1) ("comm_write: fd_table[%d].rwstate.handler != NULL\n", fd); + fd_table[fd].rwstate.handler = NULL; } - fd_table[fd].rwstate = state = memPoolAlloc(comm_write_pool); state->buf = (char *) buf; state->size = size; state->offset = 0; @@ -964,6 +962,7 @@ comm_write(fd, mb.buf, mb.size, handler, handler_data, memBufFreeFunc(&mb)); } + /* * hm, this might be too general-purpose for all the places we'd * like to use it. Index: squid/src/structs.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/structs.h,v retrieving revision 1.48.2.34.4.1 retrieving revision 1.48.2.34.4.2 diff -u -r1.48.2.34.4.1 -r1.48.2.34.4.2 --- squid/src/structs.h 27 Sep 2005 04:57:14 -0000 1.48.2.34.4.1 +++ squid/src/structs.h 27 Sep 2005 06:32:21 -0000 1.48.2.34.4.2 @@ -1,6 +1,6 @@ /* - * $Id: structs.h,v 1.48.2.34.4.1 2005/09/27 04:57:14 adri Exp $ + * $Id: structs.h,v 1.48.2.34.4.2 2005/09/27 06:32:21 adri Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -763,6 +763,15 @@ int weak; /* true if it is a weak validator */ }; +struct _CommWriteStateData { + char *buf; + size_t size; + size_t offset; + CWCB *handler; + void *handler_data; + FREE *free_func; +}; + struct _fde { unsigned int type; u_short local_port; @@ -806,7 +815,7 @@ close_handler *close_handler; /* linked list */ DEFER *defer_check; /* check if we should defer read */ void *defer_data; - CommWriteStateData *rwstate; /* State data for comm_write */ + CommWriteStateData rwstate; /* State data for comm_write */ READ_HANDLER *read_method; WRITE_HANDLER *write_method; #if USE_SSL @@ -1706,15 +1715,6 @@ } flags; }; -struct _CommWriteStateData { - char *buf; - size_t size; - size_t offset; - CWCB *handler; - void *handler_data; - FREE *free_func; -}; - struct _ErrorState { err_type type; int page_id;