sas Sun Dec 1 01:30:02 2002 EDT Modified files: /php4/ext/ircg ircg.c ircg_scanner.c ircg_scanner.re php_ircg_alloc.h Log: expand define's make the msg scanner use shared memory, if necessary replace the msg cache with a more predictable algorithm
Index: php4/ext/ircg/ircg.c diff -u php4/ext/ircg/ircg.c:1.148 php4/ext/ircg/ircg.c:1.149 --- php4/ext/ircg/ircg.c:1.148 Sat Nov 30 23:53:59 2002 +++ php4/ext/ircg/ircg.c Sun Dec 1 01:30:01 2002 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: ircg.c,v 1.148 2002/12/01 04:53:59 sas Exp $ */ +/* $Id: ircg.c,v 1.149 2002/12/01 06:30:01 sas Exp $ */ /* {{{ includes */ @@ -28,10 +28,6 @@ #include "config.h" #endif -/* for smart_strs */ -#define realloc(a,b) IRCG_SHARED_REALLOC(a,b) -#define free(a) IRCG_SHARED_FREE(a) - #include "php.h" #include "php_ini.h" #include "php_ircg.h" @@ -76,43 +72,35 @@ /* provide dummy definitions */ #include "php_ircg_hash.h" -#include "php_ircg_alloc.h" #include "php_ircg_lock.h" #else #include <ircg_resource.h> -#include <ircg_alloc.h> #include <ircg_hash.h> #include <ircg_lock.h> #endif +#include "php_ircg_alloc.h" + #include "ext/standard/php_smart_str.h" static struct { ircg_hash_table h_fmt_msgs; IRCG_LOCK(fmt_msgs_lock); -#define h_fmt_msgs php_ircg->h_fmt_msgs -#define fmt_msgs_lock php_ircg->fmt_msgs_lock /* these just serve statistical/entertainment purposes */ unsigned long irc_connects, irc_set_currents, irc_quit_handlers, exec_fmt_msgs, exec_token_compiler; -#define irc_connects php_ircg->irc_connects -#define irc_set_currents php_ircg->irc_set_currents -#define irc_quit_handlers php_ircg->irc_quit_handlers -#define exec_fmt_msgs php_ircg->exec_fmt_msgs -#define exec_token_compiler php_ircg->exec_token_compiler + unsigned long cache_hits, cache_misses; + time_t next_gc; -#define next_gc php_ircg->next_gc struct errormsg *error_msgs; -#define error_msgs php_ircg->error_msgs IRCG_LOCK(error_msgs_lock); -#define error_msgs_lock php_ircg->error_msgs_lock } *php_ircg; /* }}} */ @@ -373,7 +361,7 @@ { php_fmt_msgs_t *ret; - if (ircg_hash_find(&h_fmt_msgs, Z_STRVAL_PP(id), Z_STRLEN_PP(id), (void **) &ret) == FAILURE) + if (ircg_hash_find(&php_ircg->h_fmt_msgs, Z_STRVAL_PP(id), Z_STRLEN_PP(id), +(void **) &ret) == FAILURE) return NULL; return ret; } @@ -442,7 +430,7 @@ { php_irconn_t *conn = dummy; - irc_quit_handlers++; + php_ircg->irc_quit_handlers++; if (conn->fd > -1) { #ifdef USE_FD2IRCONN ircg_hash_index_del(&h_fd2irconn, conn->fd); @@ -562,56 +550,85 @@ are sent to channels (and hence used multiple times). */ void ircg_mirc_color(const char *, smart_str *, size_t, int, int); -#define NR_CACHE_ENTRIES 983 -static unsigned long cache_hits, cache_misses, cache_collisions; +#undef MIN +#define MIN(a,b) (a<b?a:b) -struct { - smart_str src; - smart_str result; -} static cache_entries[NR_CACHE_ENTRIES]; - -static inline php_uint32 ghash(smart_str *str) +static inline php_uint32 ghash(smart_str *str, int auto_links, int gen_br) { php_uint32 h; - const char *data = str->c, *e = str->c + str->len; + const char *data = str->c, *e = str->c + MIN(3, str->len); for (h = 2166136261U; data < e; ) { h *= 16777619; h ^= *data++; } + h *= 16777619; + h ^= auto_links ? 1 : 2; + h *= 16777619; + h ^= gen_br ? 1 : 2; return h; } /* }}} */ +struct cache_entry { + php_uint32 h; + char mask; + smart_str src; + smart_str result; +}; + +static struct cache_entry *cache_entries; +static int last_ce_write; + +#define NR_CACHE_ENTRIES 8 + + /* {{{ ircg_mirc_color_cache */ void ircg_mirc_color_cache(smart_str *src, smart_str *result, smart_str *channel, int auto_links, int gen_br) { - channel = (void *) 1; /* We only cache messages in the context of a channel */ - if (channel) { + if (channel && cache_entries) { int hash; + int i; + char mask = (auto_links ? 1 : 0) | (gen_br ? 2 : 0); + struct cache_entry *ce = cache_entries + last_ce_write; + struct cache_entry *cee = cache_entries + NR_CACHE_ENTRIES; - hash = ghash(src) % NR_CACHE_ENTRIES; + hash = ghash(src, auto_links, gen_br); - if (cache_entries[hash].src.len == src->len && - memcmp(cache_entries[hash].src.c, src->c, src->len) == 0) { - cache_hits++; - } else { - cache_misses++; - - if (cache_entries[hash].src.len != 0) { - cache_collisions++; - cache_entries[hash].src.len = 0; + /* we search forward .. */ + for (i = 0; i < NR_CACHE_ENTRIES; i++) { + if (ce->h == hash + && ce->mask == mask + && ce->src.len == src->len + && memcmp(ce->src.c, src->c, src->len) == 0) { + php_ircg->cache_hits++; + goto found; } - cache_entries[hash].result.len = 0; - ircg_mirc_color(src->c, &cache_entries[hash].result, src->len, auto_links, gen_br); - - smart_str_append_ex(&cache_entries[hash].src, src, 1); + if (++ce >= cee) + ce = cache_entries; } - smart_str_append_ex(result, &cache_entries[hash].result, 1); + + php_ircg->cache_misses++; + /* .. and insert in the reverse direction */ + + if (--last_ce_write < 0) + last_ce_write = NR_CACHE_ENTRIES - 1; + + ce = cache_entries + last_ce_write; + ce->h = hash; + ce->mask = mask; + ce->src.len = 0; + ce->result.len = 0; + + smart_str_append_ex(&ce->src, src, 1); + ircg_mirc_color(ce->src.c, &ce->result, ce->src.len, auto_links, +gen_br); + +found: + smart_str_append_ex(result, &ce->result, 1); } else { /* No channel message, no caching */ ircg_mirc_color(src->c, result, src->len, auto_links, gen_br); @@ -634,7 +651,7 @@ char c; smart_str *s; - exec_token_compiler++; + php_ircg->exec_token_compiler++; if (fmt[0] == '\0') { f->t = NULL; @@ -709,7 +726,7 @@ const token_t *t = fmt_msg->t; int ntoken = fmt_msg->ntoken; - exec_fmt_msgs++; + php_ircg->exec_fmt_msgs++; #define IRCG_APPEND(what) \ if (t[i].para.v & P_COND_STOP) { \ @@ -1164,11 +1181,11 @@ struct errormsg *m, *prev = NULL, *next; time_t lim; - IRCG_LOCK_GET(error_msgs_lock); + IRCG_LOCK_GET(php_ircg->error_msgs_lock); lim = now - GC_INTVL; - next_gc = now + GC_INTVL; + php_ircg->next_gc = now + GC_INTVL; - for (m = error_msgs; m; prev = m, m = m->next) { + for (m = php_ircg->error_msgs; m; prev = m, m = m->next) { if (m->when < lim) { struct errormsg *to; /* Check whether we have subsequent outdated records */ @@ -1184,21 +1201,21 @@ if (prev) prev->next = to; else - error_msgs = to; + php_ircg->error_msgs = to; if (!to) break; m = to; } } - IRCG_LOCK_PUT(error_msgs_lock); + IRCG_LOCK_PUT(php_ircg->error_msgs_lock); } static void add_error_msg(smart_str *msg, int msgid, php_irconn_t *conn) { struct errormsg *m; - IRCG_LOCK_GET(error_msgs_lock); - for (m = error_msgs; m; m = m->next) { + IRCG_LOCK_GET(php_ircg->error_msgs_lock); + for (m = php_ircg->error_msgs; m; m = m->next) { if (m->id == conn->irconn_id) break; } @@ -1212,27 +1229,27 @@ m->msg.len = 0; smart_str_append_ex(&m->msg, msg, 1); m->msgid = msgid; - m->next = error_msgs; - error_msgs = m; - IRCG_LOCK_PUT(error_msgs_lock); + m->next = php_ircg->error_msgs; + php_ircg->error_msgs = m; + IRCG_LOCK_PUT(php_ircg->error_msgs_lock); } static struct errormsg *lookup_and_remove_error_msg(int id) { struct errormsg *m, *prev = NULL; - IRCG_LOCK_GET(error_msgs_lock); - for (m = error_msgs; m; prev = m, m = m->next) { + IRCG_LOCK_GET(php_ircg->error_msgs_lock); + for (m = php_ircg->error_msgs; m; prev = m, m = m->next) { if (m->id == id) { if (prev) prev->next = m->next; else - error_msgs = m->next; + php_ircg->error_msgs = m->next; break; } } - IRCG_LOCK_PUT(error_msgs_lock); + IRCG_LOCK_PUT(php_ircg->error_msgs_lock); return m; } @@ -1572,7 +1589,7 @@ conn->fd = -1; } - irc_set_currents++; + php_ircg->irc_set_currents++; if (php_ircg_register_with_sapi(&conn->fd TSRMLS_CC) #ifdef USE_FD2IRCONN @@ -2051,11 +2068,11 @@ token_compiler("", &fmt_msgs.fmt_msgs[i]); } - IRCG_LOCK_GET(fmt_msgs_lock); - ircg_hash_update(&h_fmt_msgs, Z_STRVAL_PP(p1), Z_STRLEN_PP(p1), + IRCG_LOCK_GET(php_ircg->fmt_msgs_lock); + ircg_hash_update(&php_ircg->h_fmt_msgs, Z_STRVAL_PP(p1), Z_STRLEN_PP(p1), &fmt_msgs, sizeof(fmt_msgs), NULL); - IRCG_LOCK_PUT(fmt_msgs_lock); + IRCG_LOCK_PUT(php_ircg->fmt_msgs_lock); RETVAL_TRUE; } @@ -2254,7 +2271,7 @@ IRCG_SHARED_FREE(conn); RETURN_FALSE; } - irc_connects++; + php_ircg->irc_connects++; ircg_hash_init(&conn->ctcp_msgs, 10, NULL, (dtor_func_t) ctcp_msgs_dtor, 1); if (p5 && Z_TYPE_PP(p5) == IS_ARRAY) { ircg_copy_ctcp_msgs(p5, conn); @@ -2280,7 +2297,7 @@ conn->login = php_ircg_now(); /* XXX: we take chances by assuming that wordsize read/writes are atomic */ - if (conn->login >= next_gc) + if (conn->login >= php_ircg->next_gc) error_msg_gc(conn->login); RETVAL_LONG(conn->irconn_id); @@ -2596,12 +2613,15 @@ php_ircg = IRCG_SHARED_ALLOC(sizeof *php_ircg); memset(php_ircg, 0, sizeof *php_ircg); - IRCG_LOCK_INIT(fmt_msgs_lock); - IRCG_LOCK_INIT(error_msgs_lock); + IRCG_LOCK_INIT(php_ircg->fmt_msgs_lock); + IRCG_LOCK_INIT(php_ircg->error_msgs_lock); for (i = 0; i < NO_FMTS; i++) { token_compiler(fmt_msgs_default[i], &fmt_msgs_default_compiled.fmt_msgs[i]); } + } else if (stage == 1) { + cache_entries = malloc(sizeof(struct cache_entry) * NR_CACHE_ENTRIES); + memset(cache_entries, 0, sizeof(struct cache_entry) * +NR_CACHE_ENTRIES); } } @@ -2632,7 +2652,7 @@ setup(1); #endif - ircg_hash_init(&h_fmt_msgs, 0, NULL, fmt_msgs_dtor, 1); + ircg_hash_init(&php_ircg->h_fmt_msgs, 0, NULL, fmt_msgs_dtor, 1); #ifdef USE_FD2IRCONN ircg_hash_init(&h_fd2irconn, 0, NULL, NULL, 1); #endif @@ -2647,7 +2667,7 @@ */ PHP_MSHUTDOWN_FUNCTION(ircg) { - ircg_hash_destroy(&h_fmt_msgs); + ircg_hash_destroy(&php_ircg->h_fmt_msgs); #ifdef USE_IRCONN_MANAGEMENT ircg_hash_destroy(&h_irconn); #endif @@ -2655,8 +2675,8 @@ ircg_hash_destroy(&h_fd2irconn); #endif - IRCG_LOCK_DESTROY(fmt_msgs_lock); - IRCG_LOCK_DESTROY(error_msgs_lock); + IRCG_LOCK_DESTROY(php_ircg->fmt_msgs_lock); + IRCG_LOCK_DESTROY(php_ircg->error_msgs_lock); ircg_shutdown_global(); @@ -2672,22 +2692,20 @@ php_info_print_table_start(); php_info_print_table_header(2, "ircg support", "enabled"); - sprintf(buf, "%lu", cache_hits); - php_info_print_table_row(2, "scanner result cache hits", buf); - sprintf(buf, "%lu", cache_misses); - php_info_print_table_row(2, "scanner result cache misses", buf); - sprintf(buf, "%lu", cache_collisions); - php_info_print_table_row(2, "scanner result cache collisions", buf); - sprintf(buf, "%lu", exec_fmt_msgs); - php_info_print_table_row(2, "executed format messages", buf); - sprintf(buf, "%lu", exec_token_compiler); - php_info_print_table_row(2, "number of tokenizer invocations", buf); - sprintf(buf, "%lu", irc_connects); - php_info_print_table_row(2, "irc_connects", buf); - sprintf(buf, "%lu", irc_set_currents); - php_info_print_table_row(2, "irc_set_currents", buf); - sprintf(buf, "%lu", irc_quit_handlers); - php_info_print_table_row(2, "irc_quit_handlers", buf); + sprintf(buf, "%lu", php_ircg->cache_hits); + php_info_print_table_row(2, "Scanner result cache hits", buf); + sprintf(buf, "%lu", php_ircg->cache_misses); + php_info_print_table_row(2, "Scanner result cache misses", buf); + sprintf(buf, "%lu", php_ircg->exec_fmt_msgs); + php_info_print_table_row(2, "Executed format messages", buf); + sprintf(buf, "%lu", php_ircg->exec_token_compiler); + php_info_print_table_row(2, "Tokenizer invocations", buf); + sprintf(buf, "%lu", php_ircg->irc_connects); + php_info_print_table_row(2, "IRC connects", buf); + sprintf(buf, "%lu", php_ircg->irc_set_currents); + php_info_print_table_row(2, "Persistent HTTP connections", buf); + sprintf(buf, "%lu", php_ircg->irc_quit_handlers); + php_info_print_table_row(2, "Terminated IRC connections", buf); php_info_print_table_end(); } /* }}} */ Index: php4/ext/ircg/ircg_scanner.c diff -u php4/ext/ircg/ircg_scanner.c:1.19 php4/ext/ircg/ircg_scanner.c:1.20 --- php4/ext/ircg/ircg_scanner.c:1.19 Thu Feb 28 03:26:18 2002 +++ php4/ext/ircg/ircg_scanner.c Sun Dec 1 01:30:01 2002 @@ -1,5 +1,5 @@ -/* Generated by re2c 0.5 on Thu Oct 4 11:24:04 2001 */ -#line 1 "ircg_scanner.re" +/* Generated by re2c 0.5 on Sun Dec 1 07:27:15 2002 */ +#line 1 "/lrg2/php4/ext/ircg/ircg_scanner.re" /* +----------------------------------------------------------------------+ | PHP Version 4 | @@ -18,7 +18,9 @@ +----------------------------------------------------------------------+ */ -/* $Id: ircg_scanner.c,v 1.19 2002/02/28 08:26:18 sebastian Exp $ */ +/* $Id: ircg_scanner.c,v 1.20 2002/12/01 06:30:01 sas Exp $ */ + +#include "php_ircg_alloc.h" #include <ext/standard/php_smart_str.h> #include <stdio.h> @@ -64,7 +66,7 @@ smart_str *result; } ircg_msg_scanner; -#line 79 +#line 81 #define YYFILL(n) do { } while (0) @@ -308,43 +310,43 @@ if(yych <= 'z') goto yy25; } yy4: -#line 232 +#line 234 { passthru(STD_ARGS); goto state_plain; } yy5: yych = *++YYCURSOR; yy6: -#line 223 +#line 225 { mctx.fg_code = mctx.bg_code = -1; goto state_color_fg; } yy7: yych = *++YYCURSOR; yy8: -#line 224 +#line 226 { add_entity(STD_ARGS, "<"); goto state_plain; } yy9: yych = *++YYCURSOR; yy10: -#line 225 +#line 227 { add_entity(STD_ARGS, ">"); goto state_plain; } yy11: yych = *++YYCURSOR; yy12: -#line 226 +#line 228 { add_entity(STD_ARGS, "&"); goto state_plain; } yy13: yych = *++YYCURSOR; yy14: -#line 227 +#line 229 { add_entity(STD_ARGS, """); goto state_plain; } yy15: yych = *++YYCURSOR; yy16: -#line 228 - { if (gen_br) smart_str_appendl_ex(ctx->result, "<br>", 4, 1); goto state_plain; } +#line 230 + { if (gen_br) smart_str_appendl_ex(result, "<br>", 4, 1); goto state_plain; } yy17: yych = *++YYCURSOR; yy18: -#line 229 +#line 231 { handle_bold(STD_ARGS, 0); goto state_plain; } yy19: yych = *++YYCURSOR; yy20: -#line 230 +#line 232 { handle_underline(STD_ARGS, 0); goto state_plain; } yy21: yych = *++YYCURSOR; yy22: -#line 231 +#line 233 { handle_italic(STD_ARGS, 0); goto state_plain; } yy23: yych = *++YYCURSOR; goto yy4; @@ -359,10 +361,10 @@ if(yych != '/') goto yy2; yy28: yych = *++YYCURSOR; yy29: -#line 222 +#line 224 { if (auto_links) { handle_scheme(STD_ARGS); goto state_url; } else { passthru(STD_ARGS); goto state_plain; } } } -#line 233 +#line 235 state_url: @@ -428,11 +430,11 @@ yy32: yych = *++YYCURSOR; goto yy37; yy33: -#line 238 +#line 240 { handle_url(STD_ARGS); goto state_plain; } yy34: yych = *++YYCURSOR; yy35: -#line 239 +#line 241 { passthru(STD_ARGS); goto state_plain; } yy36: ++YYCURSOR; if(YYLIMIT == YYCURSOR) YYFILL(1); @@ -440,7 +442,7 @@ yy37: if(yybm[0+yych] & 128) goto yy36; goto yy33; } -#line 240 +#line 242 @@ -460,16 +462,16 @@ if(yych <= '/') goto yy41; if(yych <= '9') goto yy44; yy41: -#line 246 +#line 248 { handle_color_digit(STD_ARGS, 0); goto state_color_comma; } yy42: yych = *++YYCURSOR; yy43: -#line 247 +#line 249 { finish_color_stuff(STD_ARGS); passthru(STD_ARGS); goto state_plain; } yy44: yych = *++YYCURSOR; goto yy41; } -#line 248 +#line 250 @@ -486,14 +488,14 @@ if(yych != ',') goto yy49; yy47: yych = *++YYCURSOR; yy48: -#line 254 +#line 256 { goto state_color_bg; } yy49: yych = *++YYCURSOR; yy50: -#line 255 +#line 257 { YYCURSOR--; commit_color_stuff(STD_ARGS); goto state_plain; } } -#line 256 +#line 258 @@ -513,20 +515,20 @@ if(yych <= '/') goto yy54; if(yych <= '9') goto yy57; yy54: -#line 262 +#line 264 { handle_color_digit(STD_ARGS, 1); commit_color_stuff(STD_ARGS); goto state_plain; } yy55: yych = *++YYCURSOR; yy56: -#line 263 +#line 265 { commit_color_stuff(STD_ARGS); goto state_plain; } yy57: yych = *++YYCURSOR; goto yy54; } -#line 264 +#line 266 stop: - smart_str_free_ex(&ctx->scheme, 1); + smart_str_free_ex(&mctx.scheme, 1); finish_color_stuff(STD_ARGS); handle_bold(STD_ARGS, 1); Index: php4/ext/ircg/ircg_scanner.re diff -u php4/ext/ircg/ircg_scanner.re:1.18 php4/ext/ircg/ircg_scanner.re:1.19 --- php4/ext/ircg/ircg_scanner.re:1.18 Thu Feb 28 03:26:18 2002 +++ php4/ext/ircg/ircg_scanner.re Sun Dec 1 01:30:01 2002 @@ -16,7 +16,9 @@ +----------------------------------------------------------------------+ */ -/* $Id: ircg_scanner.re,v 1.18 2002/02/28 08:26:18 sebastian Exp $ */ +/* $Id: ircg_scanner.re,v 1.19 2002/12/01 06:30:01 sas Exp $ */ + +#include "php_ircg_alloc.h" #include <ext/standard/php_smart_str.h> #include <stdio.h> @@ -225,7 +227,7 @@ ">" { add_entity(STD_ARGS, ">"); goto state_plain; } "&" { add_entity(STD_ARGS, "&"); goto state_plain; } winquotes { add_entity(STD_ARGS, """); goto state_plain; } - ircnl { if (gen_br) smart_str_appendl_ex(ctx->result, "<br>", 4, 1); goto state_plain; } + ircnl { if (gen_br) smart_str_appendl_ex(result, "<br>", 4, +1); goto state_plain; } bold { handle_bold(STD_ARGS, 0); goto state_plain; } underline { handle_underline(STD_ARGS, 0); goto state_plain; } italic { handle_italic(STD_ARGS, 0); goto state_plain; } @@ -264,7 +266,7 @@ */ stop: - smart_str_free_ex(&ctx->scheme, 1); + smart_str_free_ex(&mctx.scheme, 1); finish_color_stuff(STD_ARGS); handle_bold(STD_ARGS, 1); Index: php4/ext/ircg/php_ircg_alloc.h diff -u php4/ext/ircg/php_ircg_alloc.h:1.1 php4/ext/ircg/php_ircg_alloc.h:1.2 --- php4/ext/ircg/php_ircg_alloc.h:1.1 Sat Nov 30 22:47:49 2002 +++ php4/ext/ircg/php_ircg_alloc.h Sun Dec 1 01:30:01 2002 @@ -1,5 +1,21 @@ +#ifndef PHP_IRCG_ALLOC_H +#define PHP_IRCG_ALLOC_H +#include <if_irc.h> + +#if IRCG_API_VERSION < 20021127 #define IRCG_SHARED_ALLOC(a) malloc((a)) #define IRCG_SHARED_REALLOC(a,b) realloc((a),(b)) #define IRCG_SHARED_FREE(a) free((a)) +#else + +/* for smart_strs */ +#define realloc(a,b) IRCG_SHARED_REALLOC(a,b) +#define free(a) IRCG_SHARED_FREE(a) + +#include <ircg_alloc.h> + + +#endif +#endif
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php