u3: refactors u3s_jam_fib to use slab api

This commit is contained in:
Joe Bryan 2020-09-28 12:26:04 -07:00
parent 9620fb1545
commit f026343713
6 changed files with 48 additions and 62 deletions

View File

@ -52,11 +52,11 @@ _jam_bench(void)
gettimeofday(&b4, 0);
{
c3_w* wor_w, bit_w;
u3i_slab sab_u;
for ( i_w = 0; i_w < max_w; i_w++ ) {
wor_w = u3s_jam_fib(wit, &bit_w);
u3a_wfree(wor_w);
u3s_jam_fib(&sab_u, wit);
u3i_slab_free(&sab_u);
}
}

View File

@ -15,8 +15,8 @@
** returns atom-suitable words, and *bit_w will have
** the length (in bits). return should be freed with u3a_wfree().
*/
c3_w*
u3s_jam_fib(u3_noun a, c3_w* bit_w);
c3_w
u3s_jam_fib(u3i_slab* sab_u, u3_noun a);
/* u3s_jam_xeno(): jam with off-loom buffer (re-)allocation.
*/

View File

@ -37,16 +37,9 @@ u3qe_jam(u3_atom a)
}
#endif
c3_w bit_w;
c3_w* wor_w = u3s_jam_fib(a, &bit_w);
c3_w len_w = (bit_w + 0x1f) >> 5;
u3i_slab sab_u;
u3i_slab_init(&sab_u, 5, len_w);
memcpy(sab_u.buf_y, wor_w, len_w * sizeof(c3_w));
u3a_wfree(wor_w);
return u3i_slab_moot(&sab_u);
u3s_jam_fib(&sab_u, a);
return u3i_slab_mint(&sab_u);
}
u3_noun

View File

@ -121,18 +121,17 @@ _cj_bash(u3_noun bat)
rod_u = u3to(u3_road, rod_u->par_p);
}
else {
c3_w bit_w, met_w;
c3_w* wor_w;
c3_y* fat_y;
c3_y dig_y[32];
u3i_slab sab_u;
c3_w met_w;
c3_y* fat_y;
c3_y dig_y[32];
u3s_jam_fib(&sab_u, bat);
// XX assumes little-endian
//
fat_y = sab_u.buf_y;
met_w = sab_u.byt_d;
wor_w = u3s_jam_fib(bat, &bit_w);
met_w = bit_w >> 3;
if ( bit_w != met_w << 3 ) {
++met_w;
}
// assume little-endian
fat_y = (c3_y*) wor_w;
#if defined(U3_OS_osx)
CC_SHA256_CTX ctx_h;
@ -146,9 +145,10 @@ _cj_bash(u3_noun bat)
SHA256_Update(&ctx_h, fat_y, met_w);
SHA256_Final(dig_y, &ctx_h);
#endif
pro = u3i_bytes(32, dig_y);
u3h_put(u3R->jed.bas_p, bat, u3k(pro));
u3a_wfree(wor_w);
u3i_slab_free(&sab_u);
break;
}
}

View File

@ -18,11 +18,11 @@ _cs_met0_w(c3_w wid_w) {
/* _cs_jam_buf: struct for tracking the fibonacci-allocated jam of a noun
*/
struct _cs_jam_fib {
u3i_slab* sab_u;
u3p(u3h_root) har_p;
c3_w a_w;
c3_w b_w;
c3_w bit_w;
c3_w* buf_w;
};
/* _cs_jam_fib_grow(): reallocate buffer with fibonacci growth
@ -39,12 +39,8 @@ _cs_jam_fib_grow(struct _cs_jam_fib* fib_u, c3_w mor_w)
}
if ( wan_w > fib_u->a_w ) {
c3_w old_w, new_w, c_w = 0;
old_w = fib_u->a_w >> 5;
if ( (old_w << 5) != fib_u->a_w ) {
++old_w;
}
c3_w old_w = fib_u->sab_u->wor_w;
c3_w c_w = 0;
// fibonacci growth
//
@ -54,13 +50,13 @@ _cs_jam_fib_grow(struct _cs_jam_fib* fib_u, c3_w mor_w)
fib_u->a_w = c_w;
}
new_w = c_w >> 5;
if ( (new_w << 5) != c_w ) {
++new_w;
}
u3i_slab_grow(fib_u->sab_u, 0, c_w);
fib_u->buf_w = u3a_wealloc(fib_u->buf_w, new_w);
memset(fib_u->buf_w + old_w, 0, (new_w - old_w) * sizeof(c3_w));
{
c3_w dif_w = fib_u->sab_u->wor_w - old_w;
c3_w* buf_w = fib_u->sab_u->buf_w + old_w;
memset(buf_w, 0, dif_w * sizeof(c3_w));
}
}
}
@ -71,8 +67,12 @@ _cs_jam_fib_chop(struct _cs_jam_fib* fib_u, c3_w met_w, u3_noun a)
{
c3_w bit_w = fib_u->bit_w;
_cs_jam_fib_grow(fib_u, met_w);
u3r_chop(0, 0, met_w, bit_w, fib_u->buf_w, a);
fib_u->bit_w += met_w;
{
c3_w* buf_w = fib_u->sab_u->buf_w;
u3r_chop(0, 0, met_w, bit_w, buf_w, a);
}
}
/* _cs_jam_fib_mat(): length-prefixed encode (mat) [a] into [fib_u]
@ -156,28 +156,21 @@ _cs_jam_fib_cell_cb(u3_noun a, void* ptr_v)
** returns atom-suitable words, and *bit_w will have
** the length (in bits). return should be freed with u3a_wfree().
*/
c3_w*
u3s_jam_fib(u3_noun a, c3_w* bit_w)
c3_w
u3s_jam_fib(u3i_slab* sab_u, u3_noun a)
{
struct _cs_jam_fib fib_u;
fib_u.har_p = u3h_new();
fib_u.sab_u = sab_u;
// fib(12) is small enough to be reasonably fast to allocate.
//
fib_u.a_w = 144;
// fib(11) is needed to get fib(13).
//
fib_u.b_w = 89;
//
fib_u.a_w = ur_fib12;
fib_u.b_w = ur_fib11;
fib_u.bit_w = 0;
{
c3_w len_w = fib_u.a_w >> 5;
if ( (len_w << 5) != fib_u.a_w ) {
++len_w;
}
fib_u.buf_w = u3a_walloc(len_w);
memset(fib_u.buf_w, 0, len_w * sizeof(c3_w));
}
u3i_slab_init(sab_u, 0, fib_u.a_w);
// as this is a hot path, we unsafely elide overflow checks
//
@ -186,9 +179,9 @@ u3s_jam_fib(u3_noun a, c3_w* bit_w)
u3a_walk_fore_unsafe(a, &fib_u, _cs_jam_fib_atom_cb,
_cs_jam_fib_cell_cb);
*bit_w = fib_u.bit_w;
u3h_free(fib_u.har_p);
return fib_u.buf_w;
return fib_u.bit_w;
}
typedef struct _jam_xeno_s {

View File

@ -338,13 +338,13 @@ _test_jam_spec(const c3_c* cap_c,
}
{
c3_w bit_w;
c3_w* wor_w = u3s_jam_fib(ref, &bit_w);
u3i_slab sab_u;
c3_w bit_w = u3s_jam_fib(&sab_u, ref);
out_d = (bit_w >> 3) + !!ur_mask_3(bit_w);
out_d = sab_u.byt_d;
// XX assumes little-endian
//
out_y = (c3_y*)wor_w;
out_y = sab_u.buf_y;
if ( 0 != memcmp(out_y, byt_y, len_w) ) {
fprintf(stderr, "\033[31mjam fib %s fail\033[0m\r\n", cap_c);
@ -352,7 +352,7 @@ _test_jam_spec(const c3_c* cap_c,
ret_i = 0;
}
u3a_wfree(wor_w);
u3i_slab_free(&sab_u);
}
return ret_i;