summaryrefslogtreecommitdiff
path: root/meme/mesh.c
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2014-09-12 03:13:54 (GMT)
committerWerner Almesberger <werner@almesberger.net>2014-09-12 03:26:13 (GMT)
commite1931049c7fc1d23e93bd2ae4783ec03cd9adfed (patch)
tree4d3f5b59579fd915fd3724ec24256bb42e760ad3 /meme/mesh.c
parent2bc5b2f3ab23a4f75bbb8c23cf437519bb332808 (diff)
downloadmisc-e1931049c7fc1d23e93bd2ae4783ec03cd9adfed.zip
misc-e1931049c7fc1d23e93bd2ae4783ec03cd9adfed.tar.gz
misc-e1931049c7fc1d23e93bd2ae4783ec03cd9adfed.tar.bz2
meme/mesh.c: allocate memory in large chunks
Timing is a bit odd: with individual allocations we get something like this: real 0m37.632s user 0m36.327s allocating vertices in chunks yields a dramatic improvement: real 0m16.755s user 0m16.085s but doing the same with edges real 0m16.686s user 0m15.969s or then with facets real 0m16.122s user 0m15.518s hardly changes anything.
Diffstat (limited to 'meme/mesh.c')
-rw-r--r--meme/mesh.c36
1 files changed, 33 insertions, 3 deletions
diff --git a/meme/mesh.c b/meme/mesh.c
index 7f1d37c..39ee10c 100644
--- a/meme/mesh.c
+++ b/meme/mesh.c
@@ -18,6 +18,8 @@
#define VHASH_TABLE (1 << 20)
#define EHASH_TABLE (1 << 20)
+#define CHUNK 65536
+
struct v {
struct vertex v;
@@ -42,6 +44,16 @@ struct facet *facets = NULL;
#define EHASH(a, b) ((long) (a) % EHASH_TABLE)
+static int vused = 0;
+static struct v *vchunk;
+
+static int eused = 0;
+static struct e *echunk;
+
+static int fused = 0;
+static struct facet *fchunk;
+
+
const struct vertex *vertex_add(int x, int y, int z)
{
struct v **h = &vhash[VHASH(x, y, z)];
@@ -51,7 +63,13 @@ const struct vertex *vertex_add(int x, int y, int z)
if (v->v.x == x && v->v.y == y && v->v.z == z)
return &v->v;
- v = alloc_type(struct v);
+ if (vused == CHUNK)
+ vused= 0;
+ if (!vused) {
+ vchunk = calloc(CHUNK, sizeof(struct v));
+ }
+ v = &vchunk[vused++];
+// v = alloc_type(struct v);
v->v.x = x;
v->v.y = y;
v->v.z = z;
@@ -102,7 +120,13 @@ static void edge_add(const struct vertex *a, const struct vertex *b)
if (e->e.a == a && e->e.b == b)
return;
- e = alloc_type(struct e);
+ if (eused == CHUNK)
+ eused = 0;
+ if (!eused) {
+ echunk = calloc(CHUNK, sizeof(struct e));
+ }
+ e = &echunk[eused++];
+// e = alloc_type(struct e);
e->e.a = a;
e->e.b = b;
e->e.next = edges;
@@ -117,7 +141,13 @@ const struct facet *facet_add(const struct vertex *a, const struct vertex *b,
{
struct facet *f;
- f = alloc_type(struct facet);
+ if (fused == CHUNK)
+ fused= 0;
+ if (!fused) {
+ fchunk = calloc(CHUNK, sizeof(struct facet));
+ }
+ f = &fchunk[fused++];
+// f = alloc_type(struct facet);
f->v[0] = a;
f->v[1] = b;
f->v[2] = c;