bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: What is the purpose of wdcache and wlcache?


From: Peng Yu
Subject: Re: What is the purpose of wdcache and wlcache?
Date: Fri, 8 Feb 2019 09:52:52 -0600

On Fri, Feb 8, 2019 at 9:42 AM Chet Ramey <chet.ramey@case.edu> wrote:
>
> On 2/8/19 10:39 AM, Peng Yu wrote:
> >> Yes: ocache_free.
> >
> > Could you please help explain what wdcache and wlcache actually do.
> > Why is it essential to have them? Why not just alloc and free them
> > without the caches? Thanks.
>
> To avoid potentially-expensive calls to malloc and free, the same as
> any cache.

There are already many mallocs and frees used in other places in the
program, why it is essential to use cache here. Is this decision based
on profiling or it is just based on some abstract concept but without
actual runtime data?

Where are these parameters coming from?

#define WDCACHESIZE 128
if ((nbytes) <= 32) {

I made the following test program. I don't understand why a longer
word "xyzabc" can still use the cache. What is actually stored in
"data"?

$ cat main.c
/* vim: set noexpandtab tabstop=2: */

/* A structure which represents a word. */
typedef struct word_desc {
    char *word;   /* Zero terminated string. */
    int flags;    /* Flags associated with this word. */
} WORD_DESC;

WORD_DESC *make_word(const char*);
void dispose_word(WORD_DESC *w);

#include <stdio.h>

#define PTR_T char*

typedef struct objcache {
    PTR_T data;
    int cs;   /* cache size, number of objects */
    int nc;   /* number of cache entries */
} sh_obj_cache_t;

extern sh_obj_cache_t wlcache;
extern sh_obj_cache_t wdcache;

void print_WORD_DESC(const WORD_DESC* w) {
    printf("{ word: %s, flags: %d }\n", w->word, w->flags);
}

void print_sh_obj_cache_t(const sh_obj_cache_t cache) {
    printf("{ data: %s, cs: %d, nc: %d }\n", cache.data, cache.cs, cache.nc);
}

void cmd_init(void);

int main(int argc, char **argv, char **env) {
    cmd_init ();
    WORD_DESC *w = make_word("abc");
    print_WORD_DESC(w);
    print_sh_obj_cache_t(wdcache);
    dispose_word(w);
    print_sh_obj_cache_t(wdcache);
    WORD_DESC* w1 = make_word("abc");
    print_WORD_DESC(w1);
    print_sh_obj_cache_t(wdcache);
    WORD_DESC* w2 = make_word("abc");
    print_WORD_DESC(w2);
    print_sh_obj_cache_t(wdcache);
    dispose_word(w1);
    print_sh_obj_cache_t(wdcache);
    dispose_word(w2);
    print_sh_obj_cache_t(wdcache);
    WORD_DESC* w3 = make_word("xyzabc");
    print_sh_obj_cache_t(wdcache);
}
$./main.exe
{ word: abc, flags: 0 }
{ data: 
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????,
cs: 128, nc: 0 }
{ data:?s, cs: 128, nc: 1 }
{ word: abc, flags: 0 }
{ data:?s, cs: 128, nc: 0 }
{ word: abc, flags: 0 }
{ data:?s, cs: 128, nc: 0 }
{ data:?s, cs: 128, nc: 1 }
{ data:?s, cs: 128, nc: 2 }
{ data:?s, cs: 128, nc: 1 }

-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]