diff options
author | Pierre Weis <Pierre.Weis@inria.fr> | 1999-01-27 18:21:14 +0000 |
---|---|---|
committer | Pierre Weis <Pierre.Weis@inria.fr> | 1999-01-27 18:21:14 +0000 |
commit | 708ea2cfac89a5d24cab4096965b148d15db6a86 (patch) | |
tree | c41e11b882cd6362f14f093019a6cca657df3c83 /otherlibs/graph/color.c | |
parent | d05683d3db6e4df790da681c8e80b7c470d5ab87 (diff) |
Cache plus grand (256->512).
Ajout d'une strategie de remplacement pseudo-random.
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@2254 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
Diffstat (limited to 'otherlibs/graph/color.c')
-rw-r--r-- | otherlibs/graph/color.c | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/otherlibs/graph/color.c b/otherlibs/graph/color.c index 9a9ed7b54..75addc608 100644 --- a/otherlibs/graph/color.c +++ b/otherlibs/graph/color.c @@ -20,11 +20,14 @@ struct color_cache_entry { unsigned long pixel; /* Pixel value */ }; -#define Color_cache_size 256 +#define Color_cache_size 512 static struct color_cache_entry color_cache[Color_cache_size]; #define Empty (-1) #define Hash_rgb(r,g,b) \ - (((r) & 0xE0) + (((g) & 0xE0) >> 3) + (((b) & 0xC0) >> 6)) + ((((r) & 0xE0) << 1) + (((g) & 0xE0) >> 2) + (((b) & 0xE0) >> 5)) +#define Color_cache_slack 16 + +static int num_overflows = 0; void gr_init_color_cache(void) { @@ -53,7 +56,15 @@ unsigned long gr_pixel_rgb(int rgb) if (color_cache[i].rgb == Empty) break; if (color_cache[i].rgb == rgb) return color_cache[i].pixel; i = (i + 1) & (Color_cache_size - 1); - if (i == h) break; + if (i == h) { + /* Cache is full. Instead of inserting at slot h, which causes + thrasing if many colors hash to the same value, + insert at h + n where n is pseudo-random and + smaller than Color_cache_slack */ + int slack = num_overflows++ & (Color_cache_slack - 1); + i = (i + slack) & (Color_cache_size - 1); + break; + } } color.red = r * 0x101; color.green = g * 0x101; |