From: Brian Aker Date: Sat, 16 Jan 2010 02:47:17 +0000 (-0800) Subject: First pass for performance improvement on _clone() operations. X-Git-Tag: 0.40~92 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=720028f73260fa1ff8a1d49c8572de04deadea57;p=m6w6%2Flibmemcached First pass for performance improvement on _clone() operations. --- diff --git a/ChangeLog b/ChangeLog index 50f7cd57..e4c731b9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ 0.38 + * memcached_clone() now runs 5 times faster. + * Functions used for callbacks are now given const memcached_st. * Added MEMCACHED_BEHAVIOR_CORK. diff --git a/libmemcached/server.c b/libmemcached/server.c index a3426769..fe62b4e9 100644 --- a/libmemcached/server.c +++ b/libmemcached/server.c @@ -14,98 +14,129 @@ */ #include "common.h" -static memcached_server_st *_server_create(const memcached_st *memc, memcached_server_st *ptr) +static inline void _server_init(memcached_server_st *self, const memcached_st *root, + const char *hostname, in_port_t port, + uint32_t weight, memcached_connection_t type) { - if (ptr == NULL) + self->options.sockaddr_inited= false; + self->number_of_hosts= 0; + self->cursor_active= 0; + self->port= port; + self->cached_errno= 0; + self->fd= -1; + self->io_bytes_sent= 0; + self->server_failure_counter= 0; + self->weight= weight; + self->state.is_corked= false; + self->major_version= 0; + self->micro_version= 0; + self->minor_version= 0; + self->type= type; + self->read_ptr= self->read_buffer; + self->cached_server_error= NULL; + self->read_buffer_length= 0; + self->read_data_length= 0; + self->write_buffer_offset= 0; + self->address_info= NULL; + + if (root) { - ptr= (memcached_server_st *)memc->call_calloc(memc, 1, sizeof(memcached_server_st)); + self->next_retry= root->retry_timeout; + } + else + { + self->next_retry= 0; + } + + self->root= root; + self->limit_maxbytes= 0; + strncpy(self->hostname, hostname, MEMCACHED_MAX_HOST_LENGTH - 1); +} + +static memcached_server_st *_server_create(memcached_server_st *self, const memcached_st *memc) +{ + if (self == NULL) + { + self= (memcached_server_st *)memc->call_malloc(memc, sizeof(memcached_server_st)); - if (!ptr) + if (! self) return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */ - ptr->options.is_allocated= true; + self->options.is_allocated= true; } else { - memset(ptr, 0, sizeof(memcached_server_st)); + self->options.is_allocated= false; } - ptr->root= memc; + self->options.is_initialized= true; - return ptr; + return self; } -memcached_server_st *memcached_server_create_with(const memcached_st *memc, memcached_server_st *host, +memcached_server_st *memcached_server_create_with(const memcached_st *memc, memcached_server_st *self, const char *hostname, in_port_t port, uint32_t weight, memcached_connection_t type) { - host= _server_create(memc, host); + self= _server_create(self, memc); - if (host == NULL) + if (self == NULL) return NULL; - strncpy(host->hostname, hostname, MEMCACHED_MAX_HOST_LENGTH - 1); - host->root= memc ? memc : NULL; - host->port= port; - host->weight= weight; - host->fd= -1; - host->type= type; - host->read_ptr= host->read_buffer; - host->state.is_corked= 0; - if (memc) - host->next_retry= memc->retry_timeout; + _server_init(self, memc, hostname, port, weight, type); + + if (type == MEMCACHED_CONNECTION_UDP) { - host->write_buffer_offset= UDP_DATAGRAM_HEADER_LENGTH; - memcached_io_init_udp_header(host, 0); + self->write_buffer_offset= UDP_DATAGRAM_HEADER_LENGTH; + memcached_io_init_udp_header(self, 0); } - return host; + return self; } -void memcached_server_free(memcached_server_st *ptr) +void memcached_server_free(memcached_server_st *self) { - memcached_quit_server(ptr, 0); + memcached_quit_server(self, 0); - if (ptr->cached_server_error) - free(ptr->cached_server_error); + if (self->cached_server_error) + free(self->cached_server_error); - if (ptr->address_info) - freeaddrinfo(ptr->address_info); + if (self->address_info) + freeaddrinfo(self->address_info); - - if (memcached_is_allocated(ptr)) + if (memcached_is_allocated(self)) { - ptr->root->call_free(ptr->root, ptr); + self->root->call_free(self->root, self); } else { - memset(ptr, 0, sizeof(memcached_server_st)); + self->options.is_initialized= false; } } /* If we do not have a valid object to clone from, we toss an error. */ -memcached_server_st *memcached_server_clone(memcached_server_st *clone, memcached_server_st *ptr) +memcached_server_st *memcached_server_clone(memcached_server_st *destination, + const memcached_server_st *source) { - memcached_server_st *rv= NULL; - - /* We just do a normal create if ptr is missing */ - if (ptr == NULL) + /* We just do a normal create if source is missing */ + if (source == NULL) return NULL; - rv= memcached_server_create_with(ptr->root, clone, - ptr->hostname, ptr->port, ptr->weight, - ptr->type); - if (rv != NULL) + destination= memcached_server_create_with(source->root, destination, + source->hostname, source->port, source->weight, + source->type); + if (destination != NULL) { - rv->cached_errno= ptr->cached_errno; - if (ptr->cached_server_error) - rv->cached_server_error= strdup(ptr->cached_server_error); + destination->cached_errno= source->cached_errno; + + if (source->cached_server_error) + destination->cached_server_error= strdup(source->cached_server_error); } - return rv; + return destination; } diff --git a/libmemcached/server.h b/libmemcached/server.h index 10cf50fd..d1a1cdac 100644 --- a/libmemcached/server.h +++ b/libmemcached/server.h @@ -19,6 +19,7 @@ extern "C" { struct memcached_server_st { struct { bool is_allocated:1; + bool is_initialized:1; bool sockaddr_inited:1; } options; uint32_t number_of_hosts; @@ -105,8 +106,8 @@ LIBMEMCACHED_API void memcached_server_free(memcached_server_st *ptr); LIBMEMCACHED_LOCAL -memcached_server_st *memcached_server_clone(memcached_server_st *clone, - memcached_server_st *ptr); +memcached_server_st *memcached_server_clone(memcached_server_st *destination, + const memcached_server_st *source); LIBMEMCACHED_API memcached_return_t memcached_server_remove(memcached_server_st *st_ptr); diff --git a/libmemcached/string.h b/libmemcached/string.h index 39a86773..4fc67cfc 100644 --- a/libmemcached/string.h +++ b/libmemcached/string.h @@ -27,10 +27,10 @@ extern "C" { */ struct memcached_string_st { - memcached_st *root; char *end; char *string; size_t current_size; + const memcached_st *root; struct { bool is_allocated:1; bool is_initialized:1;