Skip to content

Commit bb69d5e

Browse files
committed
WIP compress the ordering_table
Closes GH #10
1 parent 91bf36d commit bb69d5e

24 files changed

Lines changed: 440 additions & 347 deletions

README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,4 @@ Nivio Ziviani (nivio@dcc.ufmg.br)
385385

386386
Reini Urban (https://github.com/rurban/)
387387

388-
Last Updated: Sat Oct 25 12:03:46 2025
388+
Last Updated: Sun Oct 26 14:22:18 2025

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ <h2>License Stuff</h2>
609609
</p>
610610
<a href="http://sourceforge.net"><img src="http://sourceforge.net/sflogo.php?group_id=96251&type=1" width="88" height="31" border="0" alt="SourceForge.net Logo" /> </a>
611611
<p>
612-
Last Updated: Sat Oct 25 12:03:46 2025
612+
Last Updated: Sun Oct 26 14:22:18 2025
613613
</p>
614614
<script type="text/javascript">
615615
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");

src/bdz.c

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ void bdz_config_destroy(cmph_config_t *mph)
248248
{
249249
bdz_config_data_t *data = (bdz_config_data_t *)mph->data;
250250
DEBUGP("Destroying algorithm dependent data\n");
251+
free(mph->ordering_table);
251252
free(data);
252253
}
253254

@@ -277,7 +278,8 @@ cmph_t *bdz_new(cmph_config_t *mph, double c)
277278
bdz_queue_t edges;
278279
bdz_graph3_t graph3;
279280
bdz_config_data_t *bdz = (bdz_config_data_t *)mph->data;
280-
cmph_uint32 *ordering_table = NULL;
281+
cmph_uint32 packed_co_size = 0;
282+
cmph_uint8 *packed_co = NULL;
281283
#ifdef CMPH_TIMING
282284
double construction_time_begin = 0.0;
283285
double construction_time = 0.0;
@@ -353,9 +355,11 @@ cmph_t *bdz_new(cmph_config_t *mph, double c)
353355
bdz->m, bdz->n);
354356
ranking(bdz);
355357

358+
mphf = (cmph_t *)xmalloc(sizeof(cmph_t));
356359
if (mph->do_ordering_table) {
357-
ordering_table = (cmph_uint32 *)xmalloc(bdz->m * sizeof(cmph_uint32));
358-
assert(ordering_table);
360+
compressed_seq_t co;
361+
mph->ordering_table = (cmph_uint32 *)xmalloc(bdz->m * sizeof(cmph_uint32));
362+
assert(mph->ordering_table);
359363
DEBUGP("Create ordering table\n");
360364
mph->key_source->rewind(mph->key_source->data);
361365
for (cmph_uint32 i = 0; i < bdz->m; i++) {
@@ -369,17 +373,24 @@ cmph_t *bdz_new(cmph_config_t *mph, double c)
369373
hl[2] = hl[2] % bdz->r + (bdz->r << 1);
370374
vertex = hl[(GETVALUE(bdz->g, hl[0]) + GETVALUE(bdz->g, hl[1]) + GETVALUE(bdz->g, hl[2])) % 3];
371375
h = rank(bdz->b, bdz->ranktable, bdz->g, vertex);
372-
ordering_table[h] = i;
376+
mph->ordering_table[h] = i;
373377
mph->key_source->dispose(key);
374378
}
379+
compressed_seq_init(&co);
380+
compressed_seq_generate(&co, mph->ordering_table, bdz->m);
381+
packed_co_size = compressed_seq_packed_size(&co);
382+
packed_co = (cmph_uint8 *)xcalloc(packed_co_size, sizeof(cmph_uint8));
383+
compressed_seq_pack(&co, packed_co);
384+
compressed_seq_destroy(&co);
375385
}
376386

377387
#ifdef CMPH_TIMING
378388
ELAPSED_TIME_IN_SECONDS(&construction_time);
379389
#endif
380-
mphf = (cmph_t *)xmalloc(sizeof(cmph_t));
381390
mphf->algo = mph->algo;
382-
mphf->o = ordering_table;
391+
mphf->packed_co = packed_co;
392+
packed_co = NULL;
393+
mphf->packed_co_size = packed_co_size;
383394
bdzf = (bdz_data_t *)xmalloc(sizeof(bdz_data_t));
384395
bdzf->g = bdz->g;
385396
bdz->g = NULL; //transfer memory ownership
@@ -505,6 +516,8 @@ static void ranking(bdz_config_data_t *bdz)
505516
}
506517
}
507518

519+
#define COMPRESSED_O
520+
508521
int bdz_compile(cmph_t *mphf, cmph_config_t *mph, FILE *out)
509522
{
510523
bdz_data_t *bdz = (bdz_data_t *)mphf->data;
@@ -570,12 +583,22 @@ int bdz_compile(cmph_t *mphf, cmph_config_t *mph, FILE *out)
570583
#endif
571584
fprintf(out, " return rank(vertex);\n");
572585
fprintf(out, "};\n");
573-
if (mphf->o) {
574-
uint32_compile(out, "ordering_table", mphf->o, bdz->m);
575-
fprintf(out, "uint32_t %s_order(uint32_t id) {\n", mph->c_prefix);
576-
fprintf(out, " assert(id < %u);\n", bdz->m);
577-
fprintf(out, " return ordering_table[id];\n");
578-
fprintf(out, "}\n");
586+
if (mph->ordering_table) {
587+
#ifdef COMPRESSED_O
588+
compressed_seq_t co = {0};
589+
compressed_seq_unpack((uint32_t *)mphf->packed_co, &co);
590+
compressed_seq_data_compile(out, "ordering_seq", &co, 0);
591+
compressed_seq_query_compile(out, &co, 0); // the function
592+
fprintf(out, "uint32_t %s_order(uint32_t id) {\n", mph->c_prefix);
593+
fprintf(out, " assert(id < %u);\n", bdz->m);
594+
fprintf(out, " return compressed_seq_query(&ordering_seq, id);\n");
595+
#else
596+
uint32_compile(out, "ordering_table", mph->ordering_table, data->m);
597+
fprintf(out, "uint32_t %s_order(uint32_t id) {\n", mph->c_prefix);
598+
fprintf(out, " assert(id < %u);\n", bdz->m);
599+
fprintf(out, " return ordering_table[id];\n");
600+
#endif
601+
fprintf(out, "}\n");
579602
}
580603
fprintf(out, "\nuint32_t %s_size(void) {\n", mph->c_prefix);
581604
fprintf(out, " return %u;\n}\n", bdz->m);
@@ -620,14 +643,11 @@ int bdz_dump(cmph_t *mphf, FILE *fd)
620643
fprintf(stderr, "%u ", data->ranktable[i]);
621644
fprintf(stderr, "\n");
622645
#endif
623-
if (mphf->o) {
624-
DEBUGP("Dumping ordering_table\n");
625-
CHK_FWRITE(mphf->o, sizeof(cmph_uint32), data->m, fd);
626-
#ifdef DEBUG
627-
fprintf(stderr, "O: ");
628-
for (cmph_uint32 i = 0; i < data->m; ++i) fprintf(stderr, "%u ", mphf->o[i]);
629-
fprintf(stderr, "\n");
630-
#endif
646+
// version 2.1 extension
647+
if (mphf->packed_co_size) {
648+
DEBUGP("Dumping packed ordering table\n");
649+
CHK_FWRITE(&(mphf->packed_co_size), sizeof(cmph_uint32), (size_t)1, fd);
650+
CHK_FWRITE(mphf->packed_co, mphf->packed_co_size,(size_t)1, fd);
631651
}
632652
return 1;
633653
}
@@ -669,22 +689,15 @@ void bdz_load(FILE *f, cmph_t *mphf)
669689
fprintf(stderr, "%u ", GETVALUE(bdz->g, i));
670690
fprintf(stderr, "\n");
671691
#endif
672-
//loading the optional ordering table.
673-
mphf->o = (cmph_uint32 *)xmalloc(sizeof(cmph_uint32) * bdz->m);
692+
//loading the optional ordering table. since version 2.1
674693
cmph_uint32 nread =
675-
fread(mphf->o, sizeof(cmph_uint32), (size_t)bdz->m, f);
676-
if (nread != bdz->m) {
677-
free(mphf->o);
678-
mphf->o = NULL;
679-
}
680-
#ifdef DEBUG
681-
if (mphf->o) {
682-
fprintf(stderr, "O: ");
683-
for (cmph_uint32 i = 0; i < bdz->m; ++i)
684-
fprintf(stderr, "%u ", mphf->o[i]);
685-
fprintf(stderr, "\n");
694+
fread(&(mphf->packed_co_size), sizeof(cmph_uint32), (size_t)1, f);
695+
if (nread == 1) {
696+
mphf->packed_co = (cmph_uint8 *)xmalloc(mphf->packed_co_size);
697+
CHK_FREAD(mphf->packed_co, mphf->packed_co_size, (size_t)1, f);
698+
} else {
699+
mphf->packed_co_size = 0;
686700
}
687-
#endif
688701
return;
689702
}
690703

@@ -730,11 +743,11 @@ cmph_uint32 bdz_search(cmph_t *mphf, const char *key, cmph_uint32 keylen)
730743
void bdz_destroy(cmph_t *mphf)
731744
{
732745
bdz_data_t *data = (bdz_data_t *)mphf->data;
733-
free(mphf->o);
734746
free(data->g);
735747
hash_state_destroy(data->hl);
736748
free(data->ranktable);
737749
free(data);
750+
738751
free(mphf);
739752
}
740753

src/bdz_ph.c

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ void bdz_ph_config_destroy(cmph_config_t *mph)
223223
{
224224
bdz_ph_config_data_t *data = (bdz_ph_config_data_t *)mph->data;
225225
DEBUGP("Destroying algorithm dependent data\n");
226+
free(mph->ordering_table);
226227
free(data);
227228
}
228229

@@ -246,10 +247,11 @@ cmph_t *bdz_ph_new(cmph_config_t *mph, double c)
246247
cmph_t *mphf = NULL;
247248
bdz_ph_data_t *bdz_phf = NULL;
248249
cmph_uint32 iterations;
249-
cmph_uint32 *ordering_table = NULL;
250250
bdz_ph_queue_t edges;
251251
bdz_ph_graph3_t graph3;
252252
bdz_ph_config_data_t *bdz_ph = (bdz_ph_config_data_t *)mph->data;
253+
cmph_uint32 packed_co_size = 0;
254+
cmph_uint8 *packed_co = NULL;
253255
#ifdef CMPH_TIMING
254256
double construction_time_begin = 0.0;
255257
double construction_time = 0.0;
@@ -320,10 +322,10 @@ cmph_t *bdz_ph_new(cmph_config_t *mph, double c)
320322
bdz_ph_optimization(bdz_ph);
321323

322324
if (mph->do_ordering_table) {
325+
compressed_seq_t co;
323326
// nvertices
324-
ordering_table = (cmph_uint32 *)xmalloc(bdz_ph->n * sizeof(cmph_uint32));
325-
assert(ordering_table);
326-
memset(ordering_table, 0xFF, bdz_ph->n * sizeof(cmph_uint32));
327+
mph->ordering_table = (cmph_uint32 *)xmalloc(bdz_ph->n * sizeof(cmph_uint32));
328+
memset(mph->ordering_table, 0xFF, bdz_ph->n * sizeof(cmph_uint32));
327329
DEBUGP("Create ordering table\n");
328330
mph->key_source->rewind(mph->key_source->data);
329331

@@ -348,18 +350,26 @@ cmph_t *bdz_ph_new(cmph_config_t *mph, double c)
348350
byte2 = lookup_table[hl[2] % 5][byte2];
349351
h = hl[(byte0 + byte1 + byte2)%3];
350352

351-
ordering_table[h] = i;
353+
mph->ordering_table[h] = i;
352354
mph->key_source->dispose(key);
353355
//DEBUGP("O: %u => %i\n", h, i);
354356
}
357+
compressed_seq_init(&co);
358+
compressed_seq_generate(&co, mph->ordering_table, bdz_ph->n);
359+
packed_co_size = compressed_seq_packed_size(&co);
360+
packed_co = (cmph_uint8 *)xcalloc(packed_co_size, sizeof(cmph_uint8));
361+
compressed_seq_pack(&co, packed_co);
362+
compressed_seq_destroy(&co);
355363
}
356364

357365
#ifdef CMPH_TIMING
358366
ELAPSED_TIME_IN_SECONDS(&construction_time);
359367
#endif
360368
mphf = (cmph_t *)xmalloc(sizeof(cmph_t));
361369
mphf->algo = mph->algo;
362-
mphf->o = ordering_table;
370+
mphf->packed_co = packed_co;
371+
packed_co = NULL;
372+
mphf->packed_co_size = packed_co_size;
363373
bdz_phf = (bdz_ph_data_t *)xmalloc(sizeof(bdz_ph_data_t));
364374
bdz_phf->g = bdz_ph->g;
365375
bdz_ph->g = NULL; //transfer memory ownership
@@ -532,12 +542,16 @@ int bdz_ph_compile(cmph_t *mphf, cmph_config_t *mph, FILE *out)
532542
#endif
533543
fprintf(out, " return vertex;\n");
534544
fprintf(out, "};\n");
535-
if (mphf->o) {
536-
uint32_compile(out, "ordering_table", mphf->o, bdz->n);
537-
fprintf(out, "uint32_t %s_order(uint32_t id) {\n", mph->c_prefix);
538-
fprintf(out, " assert(id < %u);\n", bdz->n);
539-
fprintf(out, " return ordering_table[id];\n");
540-
fprintf(out, "}\n");
545+
if (mph->ordering_table) {
546+
compressed_seq_t co = {0};
547+
compressed_seq_unpack((uint32_t *)mphf->packed_co, &co);
548+
compressed_seq_data_compile(out, "ordering_seq", &co, 0);
549+
compressed_seq_query_compile(out, &co, 0); // the function
550+
//uint32_compile(out, "ordering_table", mph->ordering_table, data->m);
551+
fprintf(out, "uint32_t %s_order(uint32_t id) {\n", mph->c_prefix);
552+
fprintf(out, " assert(id < %u);\n", bdz->n);
553+
fprintf(out, " return compressed_seq_query(&ordering_seq, id);\n");
554+
fprintf(out, "}\n");
541555
}
542556
fprintf(out, "uint32_t %s_size(void) {\n", mph->c_prefix);
543557
fprintf(out, " return %u;\n}\n", bdz->n);
@@ -571,16 +585,11 @@ int bdz_ph_dump(cmph_t *mphf, FILE *fd)
571585
fprintf(stderr, "%u ", GETVALUE(data->g, i));
572586
fprintf(stderr, "\n");
573587
#endif
574-
if (mphf->o) {
575-
DEBUGP("Dumping ordering_table\n");
576-
CHK_FWRITE(mphf->o, sizeof(cmph_uint32), data->n, fd);
577-
#ifdef DEBUG
578-
fprintf(stderr, "O: ");
579-
for (cmph_uint32 i = 0; i < data->n; ++i) fprintf(stderr, "%d ", mphf->o[i]);
580-
fprintf(stderr, "\n");
581-
#endif
588+
if (mphf->packed_co_size) {
589+
DEBUGP("Dumping packed ordering table\n");
590+
CHK_FWRITE(&(mphf->packed_co_size), sizeof(cmph_uint32), (size_t)1, fd);
591+
CHK_FWRITE(mphf->packed_co, mphf->packed_co_size,(size_t)1, fd);
582592
}
583-
584593
return 1;
585594
}
586595

@@ -609,23 +618,15 @@ void bdz_ph_load(FILE *f, cmph_t *mphf)
609618
bdz_ph->g = (cmph_uint8 *)xcalloc((size_t)sizeg, sizeof(cmph_uint8));
610619
CHK_FREAD(bdz_ph->g, sizeg*sizeof(cmph_uint8), (size_t)1, f);
611620

612-
//loading the optional ordering table.
613-
mphf->o = (cmph_uint32 *)xmalloc(sizeof(cmph_uint32) * bdz_ph->n);
621+
//loading the optional ordering table. since version 2.1
614622
cmph_uint32 nread =
615-
fread(mphf->o, sizeof(cmph_uint32), (size_t)bdz_ph->n, f);
616-
if (nread != bdz_ph->n) {
617-
free(mphf->o);
618-
mphf->o = NULL;
623+
fread(&(mphf->packed_co_size), sizeof(cmph_uint32), (size_t)1, f);
624+
if (nread == 1) {
625+
mphf->packed_co = (cmph_uint8 *)xmalloc(mphf->packed_co_size);
626+
CHK_FREAD(mphf->packed_co, mphf->packed_co_size, (size_t)1, f);
627+
} else {
628+
mphf->packed_co_size = 0;
619629
}
620-
#ifdef DEBUG
621-
if (mphf->o) {
622-
fprintf(stderr, "O: ");
623-
for (cmph_uint32 i = 0; i < bdz_ph->n; ++i)
624-
fprintf(stderr, "%d ", mphf->o[i]);
625-
fprintf(stderr, "\n");
626-
}
627-
#endif
628-
629630
return;
630631
}
631632

@@ -661,8 +662,6 @@ void bdz_ph_destroy(cmph_t *mphf)
661662
free(data->g);
662663
hash_state_destroy(data->hl);
663664
free(data);
664-
if (mphf->o)
665-
free(mphf->o);
666665
free(mphf);
667666
}
668667

0 commit comments

Comments
 (0)