Flip sign of cached symbols: only search cache if symbol is cached.

* mes.c (cache_save): Flip sign when caching and invalidating.
  (cache_invalidate): Flip sign when invalidating.
This commit is contained in:
Jan Nieuwenhuizen 2016-10-21 20:44:18 +02:00
parent bca8061232
commit 1684564ea1

17
mes.c
View file

@ -295,7 +295,7 @@ assq (scm *x, scm *a)
} }
#define ENV_CACHE 1 #define ENV_CACHE 1
#define CACHE_SIZE 30 #define CACHE_SIZE 50
#define ENV_HEAD 15 #define ENV_HEAD 15
#if !ENV_CACHE #if !ENV_CACHE
@ -328,13 +328,16 @@ cache_save (scm *p)
break; break;
} }
if (env_cache_cars[i] == p->car) return &scm_unspecified; if (env_cache_cars[i] == p->car) return &scm_unspecified;
if (n > env_cache_cars[i]->value) { if (n > -1 * env_cache_cars[i]->value) {
n = env_cache_cars[i]->value; n = env_cache_cars[i]->value * -1;
j = i; j = i;
} }
} }
if (j >= 0) { if (j >= 0) {
cache_threshold = p->car->value; cache_threshold = p->car->value * -1;
p->car->value *= -1;
if (env_cache_cars[j]) env_cache_cars[j]->value *= -1;
//fprintf (stderr, "caching: %s [%d]\n", p->car->name, p->car->value);
env_cache_cars[j] = p->car; env_cache_cars[j] = p->car;
env_cache_cdrs[j] = p->cdr; env_cache_cdrs[j] = p->cdr;
} }
@ -356,6 +359,8 @@ cache_invalidate (scm *x)
{ {
for (int i=0; i < CACHE_SIZE; i++) { for (int i=0; i < CACHE_SIZE; i++) {
if (env_cache_cars[i] == x) { if (env_cache_cars[i] == x) {
x->value *= -1;
//fprintf (stderr, "dropping: %s [%d]\n", env_cache_cars[i]->name, env_cache_cars[i]->value);
env_cache_cars[i] = 0; env_cache_cars[i] = 0;
break; break;
} }
@ -376,13 +381,13 @@ cache_invalidate_range (scm *p, scm *a)
scm * scm *
assq_ref_cache (scm *x, scm *a) assq_ref_cache (scm *x, scm *a)
{ {
x->value++; x->value < 0 ? x->value-- : x->value++;
scm *c = cache_lookup (x); scm *c = cache_lookup (x);
if (c != &scm_undefined) return c; if (c != &scm_undefined) return c;
int i = 0; int i = 0;
while (a != &scm_nil && x != a->car->car) {i++;a = a->cdr;} while (a != &scm_nil && x != a->car->car) {i++;a = a->cdr;}
if (a == &scm_nil) return &scm_undefined; if (a == &scm_nil) return &scm_undefined;
if (i>ENV_HEAD) cache_save (a->car); if (i>ENV_HEAD /* && a->car->value > cache_threshold*/) cache_save (a->car);
return a->car->cdr; return a->car->cdr;
} }
#endif // ENV_CACHE #endif // ENV_CACHE