| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| #include "ppport.h" |
| 6 |
|
| #include <stdint.h> |
| 7 |
|
| |
| 8 |
|
| typedef struct { |
| 9 |
|
| int cols; |
| 10 |
|
| int max_rows; |
| 11 |
|
| uint32_t *offsets; // 4-byte offsets instead of 8-byte pointers |
| 12 |
|
| char *pool; // The packed string slab |
| 13 |
|
| size_t pool_size; |
| 14 |
|
| size_t pool_used; |
| 15 |
|
| } IndexedStore; |
| 16 |
|
| |
| 17 |
|
| MODULE = Store::Indexed::XS PACKAGE = Store::Indexed::XS |
| 18 |
|
| |
| 19 |
|
| PROTOTYPES: ENABLE |
| 20 |
|
| |
| 21 |
|
| SV * |
| 22 |
|
| _new(char *class, int cols) |
| 23 |
|
| CODE: |
| 24 |
0 |
| IndexedStore *self = (IndexedStore *)malloc(sizeof(IndexedStore)); |
| 25 |
0 |
| self->cols = cols; |
| 26 |
0 |
| self->max_rows = 1000; |
| 27 |
|
| // 4 bytes per entry |
| 28 |
0 |
| self->offsets = (uint32_t *)calloc(self->max_rows * self->cols, sizeof(uint32_t)); |
| 29 |
0 |
| self->pool_size = 1024 * 1024; // Start with 1MB arena |
| 30 |
0 |
| self->pool = (char *)malloc(self->pool_size); |
| 31 |
0 |
| self->pool_used = 1; // Start at 1, so 0 can mean "undef" |
| 32 |
0 |
| self->pool[0] = '\0'; |
| 33 |
|
| |
| 34 |
0 |
| SV *sv = newSV(0); |
| 35 |
0 |
| sv_setref_pv(sv, class, (void*)self); |
| 36 |
0 |
| RETVAL = sv; |
| 37 |
|
| OUTPUT: |
| 38 |
|
| RETVAL |
| 39 |
|
| |
| 40 |
|
| void |
| 41 |
|
| _set(SV *obj, int id, int col, SV *val) |
| 42 |
|
| CODE: |
| 43 |
0 |
| IndexedStore *self = INT2PTR(IndexedStore *, SvIV(SvRV(obj))); |
| 44 |
0 |
▶ | if (id >= self->max_rows) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (id >= self->max_rows) { |
0 |
0 |
0 |
|
| 45 |
0 |
| int old_max = self->max_rows; |
| 46 |
0 |
| self->max_rows = id + 1000; |
| 47 |
0 |
| self->offsets = (uint32_t *)realloc(self->offsets, self->max_rows * self->cols * sizeof(uint32_t)); |
| 48 |
0 |
| memset(self->offsets + (old_max * self->cols), 0, (self->max_rows - old_max) * self->cols * sizeof(uint32_t)); |
| 49 |
|
| } |
| 50 |
|
| |
| 51 |
|
| STRLEN len; |
| 52 |
0 |
| char *str = SvPV(val, len); |
| 53 |
|
| |
| 54 |
|
| // Ensure pool has space (len + 1 for null terminator) |
| 55 |
0 |
▶ | if (self->pool_used + len + 1 > self->pool_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->pool_used + len + 1 > self->pool_size) { |
0 |
0 |
0 |
|
| 56 |
0 |
| self->pool_size += (len + 1024); |
| 57 |
0 |
| self->pool = (char *)realloc(self->pool, self->pool_size); |
| 58 |
|
| } |
| 59 |
|
| |
| 60 |
0 |
| int idx = id * self->cols + col; |
| 61 |
0 |
| self->offsets[idx] = (uint32_t)self->pool_used; |
| 62 |
0 |
| memcpy(self->pool + self->pool_used, str, len + 1); |
| 63 |
0 |
| self->pool_used += (len + 1); |
| 64 |
|
| |
| 65 |
|
| SV * |
| 66 |
|
| _get(SV *obj, int id, int col) |
| 67 |
|
| CODE: |
| 68 |
0 |
| IndexedStore *self = INT2PTR(IndexedStore *, SvIV(SvRV(obj))); |
| 69 |
0 |
▶ | if (id < 0 || id >= self->max_rows || self->offsets[id * self->cols + col] == 0) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (id < 0 || id >= self->max_rows || self->offsets[id * self->cols + col] == 0) { |
0 |
0 |
0 |
| if (id < 0 || id >= self->max_rows || self->offsets[id * self->cols + col] == 0) { |
0 |
0 |
0 |
| if (id < 0 || id >= self->max_rows || self->offsets[id * self->cols + col] == 0) { |
0 |
0 |
0 |
|
| 70 |
0 |
| RETVAL = &PL_sv_undef; |
| 71 |
|
| } else { |
| 72 |
0 |
| uint32_t offset = self->offsets[id * self->cols + col]; |
| 73 |
0 |
| RETVAL = newSVpv(self->pool + offset, 0); |
| 74 |
|
| } |
| 75 |
|
| OUTPUT: |
| 76 |
|
| RETVAL |
| 77 |
|
| |
| 78 |
|
| bool |
| 79 |
|
| _exists(SV *obj, int id, int col) |
| 80 |
|
| CODE: |
| 81 |
0 |
| IndexedStore *self = INT2PTR(IndexedStore *, SvIV(SvRV(obj))); |
| 82 |
|
| // Check bounds and ensure the offset is not 0 (our "empty" marker) |
| 83 |
0 |
▶ | if (id < 0 || id >= self->max_rows) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (id < 0 || id >= self->max_rows) { |
0 |
0 |
0 |
| if (id < 0 || id >= self->max_rows) { |
0 |
0 |
0 |
|
| 84 |
0 |
| RETVAL = 0; |
| 85 |
|
| } else { |
| 86 |
0 |
| uint32_t offset = self->offsets[id * self->cols + col]; |
| 87 |
0 |
| RETVAL = (offset != 0); |
| 88 |
|
| } |
| 89 |
|
| OUTPUT: |
| 90 |
|
| RETVAL |
| 91 |
|
| |
| 92 |
|
| void |
| 93 |
|
| _delete(SV *obj, int id, int col) |
| 94 |
|
| CODE: |
| 95 |
0 |
| IndexedStore *self = INT2PTR(IndexedStore *, SvIV(SvRV(obj))); |
| 96 |
0 |
▶ | if (id >= 0 && id < self->max_rows) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (id >= 0 && id < self->max_rows) { |
0 |
0 |
0 |
| if (id >= 0 && id < self->max_rows) { |
0 |
0 |
0 |
|
| 97 |
|
| // Simply reset the offset to 0. |
| 98 |
|
| // The memory in the pool is still there, but it is now |
| 99 |
|
| // logically unreachable and thus "deleted". |
| 100 |
0 |
| self->offsets[id * self->cols + col] = 0; |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
|
| void |
| 104 |
|
| DESTROY(SV *obj) |
| 105 |
|
| CODE: |
| 106 |
0 |
| IndexedStore *self = INT2PTR(IndexedStore *, SvIV(SvRV(obj))); |
| 107 |
0 |
▶ | if (self) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self) { |
0 |
0 |
0 |
|
| 108 |
0 |
| free(self->offsets); |
| 109 |
0 |
| free(self->pool); |
| 110 |
0 |
| free(self); |
| 111 |
0 |
| sv_setiv(SvRV(obj), 0); |
| 112 |
|
| } |