Changeset 2166

Show
Ignore:
Timestamp:
09/21/08 21:59:57 (4 months ago)
Author:
karpet
Message:

store codepoints instead of start_byte (since we no longer need the latter); optimize the context storage a little to reduce number of malloc/frees

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libswish3/trunk/src/libswish3/hash.c

    r2135 r2166  
    5858 
    5959    return ret; 
     60} 
     61 
     62int 
     63swish_hash_exists_or_add( 
     64    xmlHashTablePtr hash, 
     65    xmlChar *key, 
     66    xmlChar *value 
     67) 
     68{ 
     69    if (!swish_hash_exists(hash, key)) { 
     70        return swish_hash_add(hash, key, swish_xstrdup( value )); 
     71    } 
     72    else { 
     73        return 1; 
     74    } 
    6075} 
    6176 
  • libswish3/trunk/src/libswish3/libswish3.h

    r2165 r2166  
    284284    swish_MetaName     *meta; 
    285285    xmlChar            *value; 
    286     xmlChar            *context;        // TODO refactor this into array of ints 
    287     unsigned int        start_byte; 
     286    xmlChar            *context;         
     287    unsigned int        cpts;           // num of codepoints (characters)  
    288288    unsigned int        len; 
    289289    int                 ref_cnt; 
     
    295295    unsigned int        n; 
    296296    unsigned int        pos;            // track position in document 
     297    xmlHashTablePtr     contexts;       // cache contexts 
    297298    xmlBufferPtr        buf; 
    298299    swish_Token**       tokens; 
     
    404405int         swish_hash_delete( xmlHashTablePtr hash, xmlChar *key ); 
    405406boolean     swish_hash_exists( xmlHashTablePtr hash, xmlChar *key ); 
     407int         swish_hash_exists_or_add( xmlHashTablePtr hash, xmlChar *key, xmlChar *value ); 
    406408void        swish_hash_merge( xmlHashTablePtr hash1, xmlHashTablePtr hash2 ); 
    407409void *      swish_hash_fetch( xmlHashTablePtr hash, xmlChar *key ); 
  • libswish3/trunk/src/libswish3/tokenizer.c

    r2165 r2166  
    319319    tl->ref_cnt = 0; 
    320320    tl->tokens = swish_xmalloc(sizeof(swish_Token *) * SWISH_TOKEN_LIST_SIZE); 
     321    tl->contexts = swish_init_hash(8); 
    321322 
    322323    if (!ascii_init) 
     
    349350    swish_xfree(tl->tokens); 
    350351    xmlBufferFree(tl->buf); 
     352    swish_hash_free(tl->contexts); // MUST free **after** tokens 
    351353    swish_xfree(tl); 
    352354} 
     
    373375 
    374376    stoken = swish_init_token(); 
    375     stoken->start_byte = xmlBufferLength(tl->buf); 
    376377     
    377378    /* grab the current buffer and point ->value into the buffer */ 
    378379    buf = xmlBufferContent(tl->buf); 
    379     buf += stoken->start_byte; 
     380    buf += xmlBufferLength(tl->buf);    // point at last byte which till become first byte 
    380381    stoken->value = (xmlChar*)buf; 
    381      
     382    stoken->cpts = swish_utf8_num_chrs(token); 
    382383    stoken->len = token_len - 1;    // -1 to exlude the NULL 
    383384    stoken->pos = ++tl->pos; 
    384385    stoken->meta = meta; 
    385386    stoken->meta->ref_cnt++; 
    386     stoken->context = swish_xstrdup(context); 
     387     
     388    /* cache the context string and point at the cached value */ 
     389    swish_hash_exists_or_add( tl->contexts, context, context ); 
     390    stoken->context = swish_hash_fetch( tl->contexts, context ); 
     391     
    387392    stoken->ref_cnt++; 
    388393    swish_set_token_value(tl, token, token_len); 
     
    418423{ 
    419424    int ret; 
    420     ret = xmlBufferAdd(tl->buf, token, len); 
     425    ret = xmlBufferAdd(tl->buf, token, len);    // include the NULL 
    421426    if (ret != 0) { 
    422427        SWISH_CROAK("error appending token to buffer: %d", ret); 
     
    435440    t->context = NULL; 
    436441    t->value = NULL; 
    437     t->start_byte = 0; 
     442    t->cpts = 0; 
    438443    t->len = 0; 
    439444    t->ref_cnt = 0; 
     
    449454    if (t->ref_cnt != 0) { 
    450455        SWISH_WARN("freeing Token with ref_cnt != 0 (%d)", t->ref_cnt); 
    451     } 
    452     if (t->context != NULL) { 
    453         swish_xfree(t->context); 
    454456    } 
    455457 
     
    475477    t->context      = %s\n\ 
    476478    t->meta         = %d [%s]\n\ 
    477     t->start_byte   = %d\n\ 
     479    t->cpts         = %d\n\ 
    478480    t->len          = %d\n\ 
    479481    t->value        = %s\n\ 
    480     ", t->ref_cnt, t->pos, t->context, t->meta->id, t->meta->name, t->start_byte, t->len, t->value); 
     482    ", t->ref_cnt, t->pos, t->context, t->meta->id, t->meta->name, t->cpts, t->len, t->value); 
    481483 
    482484}