keep cache of blocks of isl_ints
authorSven Verdoolaege <skimo@kotnet.org>
Wed, 13 Aug 2008 13:01:57 +0000 (15:01 +0200)
committerSven Verdoolaege <skimo@kotnet.org>
Mon, 25 Aug 2008 08:15:07 +0000 (10:15 +0200)
include/isl_blk.h
include/isl_ctx.h.in
isl_blk.c
isl_ctx.c
isl_map.c
isl_mat.c
isl_vec.c

index dd9e2ca..a0a01e0 100644 (file)
@@ -2,7 +2,6 @@
 #define ISL_BLK_H
 
 #include <isl_int.h>
-#include <isl_ctx.h>
 
 #if defined(__cplusplus)
 extern "C" {
@@ -13,11 +12,17 @@ struct isl_blk {
        isl_int *data;
 };
 
+#define ISL_BLK_CACHE_SIZE     20
+
+struct isl_ctx;
+
 struct isl_blk isl_blk_alloc(struct isl_ctx *ctx, size_t n);
 struct isl_blk isl_blk_empty();
+int isl_blk_is_error(struct isl_blk block);
 struct isl_blk isl_blk_extend(struct isl_ctx *ctx, struct isl_blk block,
                                size_t new_n);
 void isl_blk_free(struct isl_ctx *ctx, struct isl_blk block);
+void isl_blk_clear_cache(struct isl_ctx *ctx);
 
 #if defined(__cplusplus)
 }
index 585d2ff..d1d5b7a 100644 (file)
@@ -4,7 +4,8 @@
 #include <assert.h>
 #include <stdlib.h>
 
-#include "isl_int.h"
+#include <isl_int.h>
+#include <isl_blk.h>
 
 #undef ISL_POLYLIB
 #undef ISL_PIPLIB
@@ -40,6 +41,9 @@ extern "C" {
 struct isl_vec;
 struct isl_ctx {
        isl_int         one;
+
+       int             n_cached;
+       struct isl_blk  cache[ISL_BLK_CACHE_SIZE];
 #ifdef ISL_POLYLIB
        unsigned        MaxRays;
 #endif
index 0cb833c..246b40d 100644 (file)
--- a/isl_blk.c
+++ b/isl_blk.c
@@ -1,49 +1,80 @@
 #include "isl_blk.h"
+#include "isl_ctx.h"
 
 struct isl_blk isl_blk_empty()
 {
        struct isl_blk block;
+       block.size = 0;
+       block.data = NULL;
+       return block;
+}
+
+static int isl_blk_is_empty(struct isl_blk block)
+{
+       return block.size == 0 && block.data == NULL;
+}
+
+static struct isl_blk isl_blk_error()
+{
+       struct isl_blk block;
        block.size = -1;
        block.data = NULL;
        return block;
 }
 
+int isl_blk_is_error(struct isl_blk block)
+{
+       return block.size == -1 && block.data == NULL;
+}
+
 struct isl_blk isl_blk_alloc(struct isl_ctx *ctx, size_t n)
 {
+       int i;
        struct isl_blk block;
 
-       block.data = isl_alloc_array(ctx, isl_int, n);
-       if (!block.data)
-               block.size = -1;
-       else {
-               int i;
-               block.size = n;
-               for (i = 0; i < n; ++i)
-                       isl_int_init(block.data[i]);
-       }
+       if (ctx->n_cached) {
+               int best = 0;
+               for (i = 1; ctx->cache[best].size != n && i < ctx->n_cached; ++i) {
+                       if (ctx->cache[best].size < n) {
+                               if (ctx->cache[i].size > ctx->cache[best].size)
+                                       best = i;
+                       } else if (ctx->cache[i].size >= n &&
+                                  ctx->cache[i].size < ctx->cache[best].size)
+                                       best = i;
+               }
+               block = ctx->cache[best];
+               if (--ctx->n_cached != best)
+                       ctx->cache[best] = ctx->cache[ctx->n_cached];
+       } else
+               block = isl_blk_empty();
 
-       return block;
+       return isl_blk_extend(ctx, block, n);
 }
 
 struct isl_blk isl_blk_extend(struct isl_ctx *ctx, struct isl_blk block,
                                size_t new_n)
 {
+       int i;
+       isl_int *p;
+
        if (block.size >= new_n)
                return block;
+
+       p = block.data;
        block.data = isl_realloc_array(ctx, block.data, isl_int, new_n);
-       if (!block.data)
-               block.size = -1;
-       else {
-               int i;
-               for (i = block.size; i < new_n; ++i)
-                       isl_int_init(block.data[i]);
-               block.size = new_n;
+       if (!block.data) {
+               free(p);
+               return isl_blk_error();
        }
 
+       for (i = block.size; i < new_n; ++i)
+               isl_int_init(block.data[i]);
+       block.size = new_n;
+
        return block;
 }
 
-void isl_blk_free(struct isl_ctx *ctx, struct isl_blk block)
+static void isl_blk_free_force(struct isl_ctx *ctx, struct isl_blk block)
 {
        int i;
 
@@ -51,3 +82,23 @@ void isl_blk_free(struct isl_ctx *ctx, struct isl_blk block)
                isl_int_clear(block.data[i]);
        free(block.data);
 }
+
+void isl_blk_free(struct isl_ctx *ctx, struct isl_blk block)
+{
+       if (isl_blk_is_empty(block) || isl_blk_is_error(block))
+               return;
+
+       if (ctx->n_cached < ISL_BLK_CACHE_SIZE)
+               ctx->cache[ctx->n_cached++] = block;
+       else
+               isl_blk_free_force(ctx, block);
+}
+
+void isl_blk_clear_cache(struct isl_ctx *ctx)
+{
+       int i;
+
+       for (i = 0; i < ctx->n_cached; ++i)
+               isl_blk_free_force(ctx, ctx->cache[i]);
+       ctx->n_cached = 0;
+}
index fb491a0..b8fda0c 100644 (file)
--- a/isl_ctx.c
+++ b/isl_ctx.c
@@ -15,6 +15,8 @@ struct isl_ctx *isl_ctx_alloc()
        isl_int_init(ctx->one);
        isl_int_set_si(ctx->one, 1);
 
+       ctx->n_cached = 0;
+
 #ifdef ISL_POLYLIB
        ctx->MaxRays = POL_NO_DUAL | POL_INTEGER;
 #endif
@@ -29,6 +31,7 @@ void isl_ctx_free(struct isl_ctx *ctx)
 {
        if (!ctx)
                return;
+       isl_blk_clear_cache(ctx);
        isl_int_clear(ctx->one);
        free(ctx);
 }
index 84b9b99..8e5781c 100644 (file)
--- a/isl_map.c
+++ b/isl_map.c
@@ -19,7 +19,7 @@ static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
        size_t row_size = 1 + nparam + n_in + n_out + extra;
 
        bmap->block = isl_blk_alloc(ctx, (n_eq + n_ineq) * row_size);
-       if (!bmap->block.data) {
+       if (isl_blk_is_error(bmap->block)) {
                free(bmap);
                return NULL;
        }
@@ -32,12 +32,11 @@ static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
        }
 
        if (extra == 0) {
-               bmap->block2.size = 0;
-               bmap->block2.data = NULL;
+               bmap->block2 = isl_blk_empty();
                bmap->div = NULL;
        } else {
                bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
-               if (!bmap->block2.data) {
+               if (isl_blk_is_error(bmap->block2)) {
                        free(bmap->eq);
                        isl_blk_free(ctx, bmap->block);
                        free(bmap);
@@ -526,7 +525,7 @@ struct isl_basic_set *isl_basic_set_swap_vars(struct isl_ctx *ctx,
                return NULL;
 
        blk = isl_blk_alloc(ctx, bset->dim);
-       if (!blk.data)
+       if (isl_blk_is_error(blk))
                goto error;
 
        for (i = 0; i < bset->n_eq; ++i)
@@ -957,7 +956,7 @@ static struct isl_basic_map *remove_duplicate_divs(struct isl_ctx *ctx,
        if (!index)
                return bmap;
        eq = isl_blk_alloc(ctx, 1+total);
-       if (!eq.data)
+       if (isl_blk_is_error(eq))
                goto out;
 
        isl_seq_clr(eq.data, 1+total);
index 15c6b49..fb7c915 100644 (file)
--- a/isl_mat.c
+++ b/isl_mat.c
@@ -12,10 +12,9 @@ struct isl_mat *isl_mat_alloc(struct isl_ctx *ctx,
        if (!mat)
                return NULL;
 
-       mat->block.size = 0;
        mat->row = NULL;
        mat->block = isl_blk_alloc(ctx, n_row * n_col);
-       if (!mat->block.data)
+       if (isl_blk_is_error(mat->block))
                goto error;
        mat->row = isl_alloc_array(ctx, isl_int *, n_row);
        if (!mat->row)
index e4ae06b..c27dcc8 100644 (file)
--- a/isl_vec.c
+++ b/isl_vec.c
@@ -9,7 +9,7 @@ struct isl_vec *isl_vec_alloc(struct isl_ctx *ctx, unsigned size)
                return NULL;
 
        vec->block = isl_blk_alloc(ctx, size);
-       if (!vec->block.data)
+       if (isl_blk_is_error(vec->block))
                goto error;
 
        vec->ref = 1;