From 6244022fc260d3f35804ed690b1b2a4a466d0e51 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 7 May 2010 14:53:49 +0200 Subject: [PATCH] Use shared WRBUF for returned record strings By sharing a WRBUF for a whole resultset for all records the number of allocations is reduced a lot. --- src/zoom-c.c | 108 ++++++++++++++++++++++++++++++++++++++-------------------- src/zoom-p.h | 10 ++++++ 2 files changed, 81 insertions(+), 37 deletions(-) diff --git a/src/zoom-c.c b/src/zoom-c.c index b95ba00..e662615 100644 --- a/src/zoom-c.c +++ b/src/zoom-c.c @@ -28,6 +28,12 @@ #include #include +#include + +#if SHPTR +YAZ_SHPTR_TYPE(WRBUF) +#endif + static int log_api = 0; static int log_details = 0; @@ -870,6 +876,12 @@ ZOOM_resultset ZOOM_resultset_create(void) r->num_databaseNames = 0; r->mutex = 0; yaz_mutex_create(&r->mutex); +#if SHPTR + { + WRBUF w = wrbuf_alloc(); + YAZ_SHPTR_INIT(r->record_wrbuf, w); + } +#endif return r; } @@ -1027,8 +1039,15 @@ static void ZOOM_record_release(ZOOM_record rec) { if (!rec) return; + +#if SHPTR + if (rec->record_wrbuf) + YAZ_SHPTR_DEC(rec->record_wrbuf, wrbuf_destroy); +#else if (rec->wrbuf) wrbuf_destroy(rec->wrbuf); +#endif + #if YAZ_HAVE_XML2 if (rec->xml_mem) xmlFree(rec->xml_mem); @@ -1069,6 +1088,7 @@ static void resultset_destroy(ZOOM_resultset r) if (r->refcount == 0) { yaz_mutex_leave(r->mutex); + yaz_log(log_details, "%p ZOOM_connection resultset_destroy: Deleting resultset (%p) ", r->connection, r); ZOOM_resultset_cache_reset(r); ZOOM_query_destroy(r->query); @@ -1077,6 +1097,9 @@ static void resultset_destroy(ZOOM_resultset r) xfree(r->setname); xfree(r->schema); yaz_mutex_destroy(&r->mutex); +#if SHPTR + YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy); +#endif xfree(r); } else @@ -1807,7 +1830,11 @@ ZOOM_API(ZOOM_record) nrec = (ZOOM_record) xmalloc(sizeof(*nrec)); nrec->odr = odr_createmem(ODR_DECODE); +#if SHPTR + nrec->record_wrbuf = 0; +#else nrec->wrbuf = 0; +#endif #if YAZ_HAVE_XML2 nrec->xml_mem = 0; nrec->xml_size = 0; @@ -1895,7 +1922,8 @@ static yaz_iconv_t iconv_create_charset(const char *record_charset) return cd; } -static const char *return_marc_record(ZOOM_record rec, int marc_type, +static const char *return_marc_record(ZOOM_record rec, WRBUF wrbuf, + int marc_type, int *len, const char *buf, int sz, const char *record_charset) @@ -1907,14 +1935,11 @@ static const char *return_marc_record(ZOOM_record rec, int marc_type, if (cd) yaz_marc_iconv(mt, cd); yaz_marc_xml(mt, marc_type); - if (!rec->wrbuf) - rec->wrbuf = wrbuf_alloc(); - wrbuf_rewind(rec->wrbuf); - if (yaz_marc_decode_wrbuf(mt, buf, sz, rec->wrbuf) > 0) + if (yaz_marc_decode_wrbuf(mt, buf, sz, wrbuf) > 0) { if (len) - *len = wrbuf_len(rec->wrbuf); - ret_string = wrbuf_cstr(rec->wrbuf); + *len = wrbuf_len(wrbuf); + ret_string = wrbuf_cstr(wrbuf); } yaz_marc_destroy(mt); if (cd) @@ -1922,7 +1947,8 @@ static const char *return_marc_record(ZOOM_record rec, int marc_type, return ret_string; } -static const char *return_opac_record(ZOOM_record rec, int marc_type, +static const char *return_opac_record(ZOOM_record rec, WRBUF wrbuf, + int marc_type, int *len, Z_OPACRecord *opac_rec, const char *record_charset) @@ -1934,21 +1960,18 @@ static const char *return_opac_record(ZOOM_record rec, int marc_type, yaz_marc_iconv(mt, cd); yaz_marc_xml(mt, marc_type); - if (!rec->wrbuf) - rec->wrbuf = wrbuf_alloc(); - wrbuf_rewind(rec->wrbuf); - - yaz_opac_decode_wrbuf(mt, opac_rec, rec->wrbuf); + yaz_opac_decode_wrbuf(mt, opac_rec, wrbuf); yaz_marc_destroy(mt); if (cd) yaz_iconv_close(cd); if (len) - *len = wrbuf_len(rec->wrbuf); - return wrbuf_cstr(rec->wrbuf); + *len = wrbuf_len(wrbuf); + return wrbuf_cstr(wrbuf); } -static const char *return_string_record(ZOOM_record rec, int *len, +static const char *return_string_record(ZOOM_record rec, WRBUF wrbuf, + int *len, const char *buf, int sz, const char *record_charset) { @@ -1956,16 +1979,11 @@ static const char *return_string_record(ZOOM_record rec, int *len, if (cd) { - if (!rec->wrbuf) - rec->wrbuf = wrbuf_alloc(); - - wrbuf_rewind(rec->wrbuf); + wrbuf_iconv_write(wrbuf, cd, buf, sz); + wrbuf_iconv_reset(wrbuf, cd); - wrbuf_iconv_write(rec->wrbuf, cd, buf, sz); - wrbuf_iconv_reset(rec->wrbuf, cd); - - buf = wrbuf_cstr(rec->wrbuf); - sz = wrbuf_len(rec->wrbuf); + buf = wrbuf_cstr(wrbuf); + sz = wrbuf_len(wrbuf); yaz_iconv_close(cd); } if (len) @@ -1979,15 +1997,29 @@ static const char *return_record(ZOOM_record rec, int *len, { Z_External *r = (Z_External *) npr->u.databaseRecord; const Odr_oid *oid = r->direct_reference; - + WRBUF wrbuf; + +#if SHPTR + if (!rec->record_wrbuf) + { + WRBUF w = wrbuf_alloc(); + YAZ_SHPTR_INIT(rec->record_wrbuf, w); + } + wrbuf = rec->record_wrbuf->ptr; +#else + if (!rec->wrbuf) + rec->wrbuf = wrbuf_alloc(); + wrbuf = rec->wrbuf; +#endif + wrbuf_rewind(wrbuf); /* render bibliographic record .. */ if (r->which == Z_External_OPAC) { - return return_opac_record(rec, marctype, len, + return return_opac_record(rec, wrbuf, marctype, len, r->u.opac, charset); } if (r->which == Z_External_sutrs) - return return_string_record(rec, len, + return return_string_record(rec, wrbuf, len, (char*) r->u.sutrs->buf, r->u.sutrs->len, charset); @@ -1996,7 +2028,7 @@ static const char *return_record(ZOOM_record rec, int *len, if (yaz_oid_is_iso2709(oid)) { const char *ret_buf = return_marc_record( - rec, marctype, len, + rec, wrbuf, marctype, len, (const char *) r->u.octet_aligned->buf, r->u.octet_aligned->len, charset); @@ -2006,20 +2038,17 @@ static const char *return_record(ZOOM_record rec, int *len, if (marctype != YAZ_MARC_ISO2709) return 0; } - return return_string_record(rec, len, + return return_string_record(rec, wrbuf, len, (const char *) r->u.octet_aligned->buf, r->u.octet_aligned->len, charset); } else if (r->which == Z_External_grs1) { - if (!rec->wrbuf) - rec->wrbuf = wrbuf_alloc(); - wrbuf_rewind(rec->wrbuf); - yaz_display_grs1(rec->wrbuf, r->u.grs1, 0); - return return_string_record(rec, len, - wrbuf_buf(rec->wrbuf), - wrbuf_len(rec->wrbuf), + yaz_display_grs1(wrbuf, r->u.grs1, 0); + return return_string_record(rec, wrbuf, len, + wrbuf_buf(wrbuf), + wrbuf_len(wrbuf), charset); } return 0; @@ -2274,7 +2303,12 @@ static void record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr, { rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc)); rc->rec.odr = 0; +#if SHPTR + YAZ_SHPTR_INC(r->record_wrbuf); + rc->rec.record_wrbuf = r->record_wrbuf; +#else rc->rec.wrbuf = 0; +#endif #if YAZ_HAVE_XML2 rc->rec.xml_mem = 0; #endif diff --git a/src/zoom-p.h b/src/zoom-p.h index 3535221..05f5503 100644 --- a/src/zoom-p.h +++ b/src/zoom-p.h @@ -37,6 +37,8 @@ #include #include +#define SHPTR 1 + typedef struct ZOOM_Event_p *ZOOM_Event; struct ZOOM_query_p { @@ -152,11 +154,19 @@ struct ZOOM_resultset_p { char **databaseNames; int num_databaseNames; YAZ_MUTEX mutex; +#if SHPTR + struct WRBUF_shptr *record_wrbuf; +#endif }; struct ZOOM_record_p { ODR odr; +#if SHPTR + struct WRBUF_shptr *record_wrbuf; +#else WRBUF wrbuf; +#endif + Z_NamePlusRecord *npr; const char *schema; -- 1.7.10.4