RPA Toolkit
work on the new caching system
authorMartin Stoilov <martin@rpasearch.com>
Tue, 19 Apr 2011 03:28:33 +0000 (20:28 -0700)
committerMartin Stoilov <martin@rpasearch.com>
Tue, 19 Apr 2011 03:28:33 +0000 (20:28 -0700)
rpa2/rpacache.c
rpa2/rpacache.h
rpa2/rpacompiler.c
rpa2/rpaparser.c
rpa2/rpastat.c
rpa2/rpavm.c

index 73b3128..79f89ee 100644 (file)
@@ -7,34 +7,18 @@
 
 rpacache_t *rpa_cache_create()
 {
-       rint i;
        rpacache_t *cache = (rpacache_t*) r_zmalloc(sizeof(*cache));
 
        if (!cache)
                return NULL;
-       for (i = 0; i < RPA_MCACHE_SIZE; i++) {
-               cache->entry[i].records = r_array_create(sizeof(rparecord_t));
-               if (!cache->entry[i].records) {
-                       rpa_cache_destroy(cache);
-                       return NULL;
-               }
-       }
-
        return cache;
 }
 
 
 void rpa_cache_destroy(rpacache_t *cache)
 {
-       rint i;
-
        if (!cache)
                return;
-       for (i = 0; i < RPA_MCACHE_SIZE; i++) {
-               if (cache->entry[i].records) {
-                       r_array_destroy(cache->entry[i].records);
-               }
-       }
        r_free(cache);
 }
 
@@ -50,21 +34,19 @@ void rpa_cache_disable(rpacache_t *cache, rlong disable)
 }
 
 
-void rpa_cache_set(rpacache_t *cache, rlong top, rlong ruleid, rlong ret, rparecord_t* records, rsize_t nrecords)
+void rpa_cache_set(rpacache_t *cache, rlong top, rlong ruleid, rlong ret, rlong startrec, rlong endrec)
 {
-       rlong i;
        rulong bucket = RPA_MCACHE_BUCKET(top, ruleid);
 
        if (ret <= 0 || cache->disalbled)
                return;
-//     r_printf("Set the cache @ %ld for: top = %ld, ret = %d, nrecors = %ld, rulename = %s\n", bucket, top, ret, nrecords, records->rule);
        cache->entry[bucket].ruleid = ruleid;
        cache->entry[bucket].top = top;
        cache->entry[bucket].ret = ret;
+       cache->entry[bucket].startrec = startrec;
+       cache->entry[bucket].endrec = endrec;
        cache->entry[bucket].serial = cache->serial;
-       r_array_setlength(cache->entry[bucket].records, 0);
-       for (i = 0; i < nrecords; i++)
-               r_array_add(cache->entry[bucket].records, &records[i]);
+
 }
 
 
index ea0c974..c5df198 100644 (file)
@@ -18,8 +18,10 @@ typedef struct rpacachedentry_s {
        rlong ruleid;
        rlong top;
        rlong ret;
+       rlong startrec;
+       rlong endrec;
        rulong serial;
-       rarray_t *records;
+//     rarray_t *records;
 } rpacachedentry_t;
 
 typedef struct rpacache_s {
@@ -34,7 +36,7 @@ rpacache_t *rpa_cache_create();
 void rpa_cache_destroy(rpacache_t *cache);
 void rpa_cache_disable(rpacache_t *cache, rlong disable);
 void rpa_cache_invalidate(rpacache_t *cache);
-void rpa_cache_set(rpacache_t *cache, rlong top, rlong ruleid, rlong ret, rparecord_t* records, rsize_t nrecords);
+void rpa_cache_set(rpacache_t *cache, rlong top, rlong ruleid, rlong ret, rlong startrec, rlong endrec);
 rpacachedentry_t *rpa_cache_lookup(rpacache_t *cache, rlong top, rlong ruleid);
 #ifdef __cplusplus
 }
index 6fd5202..ed4ce8a 100644 (file)
@@ -384,6 +384,7 @@ rint rpa_compiler_rule_begin(rpa_compiler_t *co, const rchar *name, ruint namesi
        rvm_codegen_addins(co->cg, rvm_asm(RVM_BXGRE, LR, XX, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_PUSHM, DA, XX, XX, BIT(R_REC)|BIT(R_TOP)|BIT(LR)));
        rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_EMITSTART, DA, R_TOP, XX, 0));
+
        r_array_add(co->expressions, &exp);
        return 0;
 }
@@ -404,7 +405,7 @@ rint rpa_compiler_rule_end(rpa_compiler_t *co)
        rvm_codegen_addins(co->cg, rvm_asm(RVM_BEQ, DA, XX, XX, 5));
        rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BLOB, exp.dataidx, rvm_asm(RPA_EMITEND, DA, R2, R0, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RPA_GETNEXTREC, R2, R1, XX, 0));
-       rvm_codegen_addins(co->cg, rvm_asml(RPA_SETCACHE, DA, R0, R2, exp.start));
+       rvm_codegen_addins(co->cg, rvm_asml(RPA_SETCACHE, DA, R2, R_REC, exp.start));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_BX, LR, XX, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_MOV, R_REC, R1, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_MOVS, R0, DA, XX, -1));
index d723ced..1d3ca64 100644 (file)
@@ -39,7 +39,7 @@ rlong rpa_parser_load(rpa_parser_t *pa, const rchar *prods, rsize_t size)
        rpastat_t *stat = pa->stat;
 
        rpa_stat_init(stat, prods, prods, prods + size);
-       rpa_stat_cachedisable(stat, 0);
+       rpa_stat_cachedisable(stat, 1);
 
        if (rpa_stat_exec(stat, rvm_codegen_getcode(co->cg, 0), pa->main) < 0)
                return -1;
index c0d1964..e5ddd21 100644 (file)
@@ -103,7 +103,7 @@ static rparecord_t *rpa_stat_nextrecord(rpastat_t *stat, rparecord_t *cur)
        }
        if (cur >= (rparecord_t *)r_array_lastslot(stat->records) || cur < (rparecord_t *)r_array_slot(stat->records, 0))
                return NULL;
-       ++cur;
+       cur = (rparecord_t *)r_array_slot(stat->records, cur->next);
        if (cur->type == RPA_RECORD_TAIL)
                return NULL;
        return cur;
index 5b09d3f..0089132 100644 (file)
@@ -234,7 +234,7 @@ static void rpavm_swi_emithead(rvmcpu_t *cpu, rvm_asmins_t *ins)
        rpastat_t *stat = (rpastat_t *)cpu->userdata1;
        rlong index = RVM_CPUREG_GETL(cpu, R_REC);
 
-       index = r_array_replace(stat->records, index + 1, NULL);
+       index = r_array_add(stat->records, NULL);
        RVM_CPUREG_SETL(cpu, R_REC, index);
        rec = (rparecord_t *)r_array_slot(stat->records, index);
        rec->type = RPA_RECORD_HEAD;
@@ -247,7 +247,7 @@ static void rpavm_swi_emittail(rvmcpu_t *cpu, rvm_asmins_t *ins)
        rpastat_t *stat = (rpastat_t *)cpu->userdata1;
        rlong index = RVM_CPUREG_GETL(cpu, R_REC);
 
-       index = r_array_replace(stat->records, index + 1, NULL);
+       index = r_array_add(stat->records, NULL);
        crec = (rparecord_t *)r_array_slot(stat->records, RVM_CPUREG_GETL(cpu, R_REC));
        RVM_CPUREG_SETL(cpu, R_REC, index);
        rec = (rparecord_t *)r_array_slot(stat->records, index);
@@ -269,7 +269,9 @@ static void rpavm_swi_emitstart(rvmcpu_t *cpu, rvm_asmins_t *ins)
                return;
 //     r_printf("%ld: %s, %s, tp = %ld\n", RVM_CPUREG_GETU(cpu, FP), "START", name.str, tp);
        R_ASSERT(RVM_CPUREG_GETL(cpu, R_REC) >= 0);
-       index = r_array_replace(stat->records, index + 1, NULL);
+//     index = r_array_replace(stat->records, index + 1, NULL);
+       index = r_array_add(stat->records, NULL);
+
        /*
         * Important: get the pointer to crec after modifying the array, because if
         * it gets reallocated the pointer will be invalid.
@@ -303,7 +305,9 @@ static void rpavm_swi_emitend(rvmcpu_t *cpu, rvm_asmins_t *ins)
                return;
 //     r_printf("%ld: %s, %s, tp = %ld, tplen = %ld\n", RVM_CPUREG_GETU(cpu, FP), "END ", name.str, tp, tplen);
        R_ASSERT(RVM_CPUREG_GETL(cpu, R_REC) >= 0);
-       index = r_array_replace(stat->records, index + 1, NULL);
+//     index = r_array_replace(stat->records, index + 1, NULL);
+       index = r_array_add(stat->records, NULL);
+
        /*
         * Important: get the pointer to crec after modifying the array, because if
         * it gets reallocated the pointer will be invalid.
@@ -341,9 +345,13 @@ static void rpavm_swi_prninfo(rvmcpu_t *cpu, rvm_asmins_t *ins)
 
 static void rpavm_swi_getnextrec(rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
+       rpastat_t *stat = (rpastat_t *)cpu->userdata1;
        rlong rec = RVM_CPUREG_GETL(cpu, ins->op2);
 
-       RVM_CPUREG_SETL(cpu, ins->op1, rec + 1);
+       rparecord_t *prec = (rparecord_t *)r_array_slot(stat->records, rec);
+
+//     r_printf("%s, rec = %ld, next = %ld\n", __FUNCTION__, rec, prec->next);
+       RVM_CPUREG_SETL(cpu, ins->op1, prec->next);
 }
 
 
@@ -379,46 +387,43 @@ static void rpavm_swi_setcache(rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
        rparecord_t *prec;
        rpastat_t *stat = (rpastat_t *)cpu->userdata1;
+       rlong r0 = RVM_CPUREG_GETL(cpu, R0);
        rlong ruleid = RVM_CPUREG_GETL(cpu, ins->op1);
-       rlong r0 = RVM_CPUREG_GETL(cpu, ins->op2);
-       rlong rec = RVM_CPUREG_GETL(cpu, ins->op3);
-       rlong nrecords = RVM_CPUREG_GETL(cpu, R_REC) - rec + 1;
+       rlong startrec = RVM_CPUREG_GETL(cpu, ins->op2);
+       rlong endrec = RVM_CPUREG_GETL(cpu, ins->op3);
 
-       /*
-        * If the record set is too big, don't use the cache
-        */
-       return;
-       if (nrecords > 100)
+
+       if (stat->cache->disalbled)
                return;
 
-       if (!RVM_STATUS_GETBIT(cpu, RVM_STATUS_N) && !RVM_STATUS_GETBIT(cpu, RVM_STATUS_Z)) {
-               prec = (rparecord_t *)r_array_slot(stat->records, rec);
-//             r_printf("Set the cache for: %s (%ld)\n", prec->rule, nrecords);
-               rpa_cache_set(stat->cache, prec->top, ruleid, r0, prec, nrecords);
+       if (!RVM_STATUS_GETBIT(cpu, RVM_STATUS_N) && !RVM_STATUS_GETBIT(cpu, RVM_STATUS_Z) && r0 && startrec && endrec > startrec) {
+               prec = (rparecord_t *)r_array_slot(stat->records, startrec);
+//             r_printf("Set the cache for: %s (%ld, %ld), top = %ld, ret = %ld, ruleid=%ld\n", prec->rule, startrec, endrec, prec->top, r0, ruleid);
+               rpa_cache_set(stat->cache, prec->top, ruleid, r0, startrec, endrec);
        }
 }
 
 
 static void rpavm_swi_checkcache(rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
-       rlong i;
        rpastat_t *stat = (rpastat_t *)cpu->userdata1;
        rpacachedentry_t *entry;
        rlong ruleid = RVM_CPUREG_GETL(cpu, ins->op1);
        rlong top = RVM_CPUREG_GETL(cpu, ins->op2);
        rlong r0 = 0;
        entry = rpa_cache_lookup(stat->cache, top, ruleid);
-       if (0 && entry) {
-//             rparecord_t *prec = (rparecord_t *)r_array_slot(entry->records, 0);
-//             r_printf("Hit the cache for: %s (%ld), r0 = %ld\n", prec->rule, r_array_length(entry->records), entry->ret);
-               r_array_setlength(stat->records, RVM_CPUREG_GETL(cpu, R_REC) + 1);
-               for (i = 0; i < r_array_length(entry->records); i++) {
-                       r_array_add(stat->records, r_array_slot(entry->records, i));
+       if (entry) {
+               rparecord_t *prec = (rparecord_t *)r_array_slot(stat->records, entry->startrec);
+//             r_printf("Hit the cache for: %s (%ld, %ld), r0 = %ld\n", prec->rule, entry->startrec, entry->endrec, entry->ret);
+               rparecord_t *crec = (rparecord_t *)r_array_slot(stat->records, RVM_CPUREG_GETL(cpu, R_REC));
+
+               if (1) {
+                       crec->next = entry->startrec;
+                       RVM_CPUREG_SETL(cpu, R_REC, entry->endrec);
+                       r0 = entry->ret;
+                       top += r0;
+                       RVM_CPUREG_SETU(cpu, R_TOP, top);
                }
-               RVM_CPUREG_SETL(cpu, R_REC,  r_array_length(stat->records) - 1);
-               r0 = entry->ret;
-               top += r0;
-               RVM_CPUREG_SETU(cpu, R_TOP, top);
        }
 
        RVM_STATUS_CLRALL(cpu);