u3: refactors u3s_cue_xeno, declares u3s_cue_xeno_unsafe()

This commit is contained in:
Joe Bryan 2020-08-31 19:40:32 -07:00
parent b01d57eb30
commit f0dd6dc7a6
5 changed files with 67 additions and 51 deletions

View File

@ -2,14 +2,6 @@
#include "vere/vere.h"
#include "ur/ur.h"
// XX not declared in serial.h due to presence of ur_* types
//
ur_cue_res_e
u3s_cue_xeno_unsafe(ur_dict32_t* dic_u,
c3_d len_d,
const c3_y* byt_y,
u3_noun* out);
/* _setup(): prepare for tests.
*/
static void
@ -188,12 +180,11 @@ _cue_bench(void)
fprintf(stderr, " cue atom: %u ms\r\n", mil_w);
}
// NB: runs 1/8th the number of times
//
{
gettimeofday(&b4, 0);
{
u3_noun out;
c3_w len_w = u3r_met(3, vat);
// XX assumes little-endian
//
@ -201,15 +192,16 @@ _cue_bench(void)
? (c3_y*)&vat
: (c3_y*)((u3a_atom*)u3a_to_ptr(vat))->buf_w;
for ( i_w = 0; i_w < max_w / 8; i_w++ ) {
u3z(u3s_cue_xeno(len_w, byt_y));
for ( i_w = 0; i_w < max_w; i_w++ ) {
u3s_cue_xeno(len_w, byt_y, &out);
u3z(out);
}
}
gettimeofday(&f2, 0);
timersub(&f2, &b4, &d0);
mil_w = (d0.tv_sec * 1000) + (d0.tv_usec / 1000);
fprintf(stderr, " cue xeno: %u ms (estimated)\r\n", mil_w * 8);
fprintf(stderr, " cue xeno: %u ms\r\n", mil_w);
}
{

View File

@ -1,6 +1,12 @@
/* i/n/serial.h
**
*/
/* forward declarations
*/
/* ur_dict32_s: off-loom 32-bit dictionary.
*/
struct ur_dict32_s;
/* Noun serialization. All noun arguments RETAINED.
*/
@ -40,10 +46,18 @@
u3_noun
u3s_cue(u3_atom a);
/* u3s_cue_xeno_unsafe(): cue onto the loom, all bookkeeping off-loom.
*/
c3_o
u3s_cue_xeno_unsafe(struct ur_dict32_s* dic_u,
c3_d len_d,
const c3_y* byt_y,
u3_noun* out);
/* u3s_cue_xeno(): cue onto the loom, bookkeeping off the loom.
*/
u3_noun
u3s_cue_xeno(c3_d len_d, const c3_y* byt_y);
c3_o
u3s_cue_xeno(c3_d len_d, const c3_y* byt_y, u3_noun* out);
/* u3s_cue_bytes(): cue bytes onto the loom.
*/

View File

@ -963,8 +963,10 @@ _cs_cue_xeno_next(_cue_stack_t* tac_u,
}
/* u3s_cue_xeno_unsafe(): cue onto the loom, all bookkeeping off-loom.
**
** NB: unsafe wrt to [dic_u], which must be empty.
*/
ur_cue_res_e
c3_o
u3s_cue_xeno_unsafe(ur_dict32_t* dic_u,
c3_d len_d,
const c3_y* byt_y,
@ -978,12 +980,12 @@ u3s_cue_xeno_unsafe(ur_dict32_t* dic_u,
// init bitstream-reader
//
if ( ur_cue_good != (res_e = ur_bsr_init(&red_u, len_d, byt_y)) ) {
return res_e;
return c3n;
}
// bit-cursor (and backreferences) must fit in 62-bit direct atoms
//
else if ( 0x7ffffffffffffffULL < len_d ) {
return ur_cue_meme;
return c3n;
}
// setup stack
@ -1020,43 +1022,46 @@ u3s_cue_xeno_unsafe(ur_dict32_t* dic_u,
}
}
c3_free(tac_u.fam_u);
if ( ur_cue_good == res_e ) {
*out = ref;
c3_free(tac_u.fam_u);
return c3y;
}
else {
// unwind the stack, disposing intermediate nouns
//
while ( tac_u.fil_w ) {
_cue_frame_t* fam_u = &(tac_u.fam_u[--tac_u.fil_w]);
if ( u3_none != fam_u->ref ) {
u3z(fam_u->ref);
}
}
c3_free(tac_u.fam_u);
return c3n;
}
return res_e;
}
/* u3s_cue_xeno(): cue onto the loom, bookkeeping off the loom.
*/
u3_noun
u3s_cue_xeno(c3_d len_d, const c3_y* byt_y)
c3_o
u3s_cue_xeno(c3_d len_d, const c3_y* byt_y, u3_noun* out)
{
ur_dict32_t dic_u = {0};
ur_cue_res_e res_e;
u3_noun pro;
c3_o ret_o;
c3_assert( &(u3H->rod_u) == u3R );
// XX tune the initial dictionary size for less reallocation
//
{
ur_root_t* rot_u = 0;
ur_dict32_grow(rot_u, &dic_u, ur_fib33, ur_fib34);
}
ur_dict32_grow((ur_root_t*)0, &dic_u, ur_fib10, ur_fib11);
// errors are fatal
//
if ( ur_cue_good !=
(res_e = u3s_cue_xeno_unsafe(&dic_u, len_d, byt_y, &pro)) )
{
fprintf(stderr, "cue xeno: failed\r\n");
exit(1);
}
ret_o = u3s_cue_xeno_unsafe(&dic_u, len_d, byt_y, out);
ur_dict_free((ur_dict_t*)&dic_u);
return pro;
return ret_o;
}
/* _cs_cue_need(): bail on ur_cue_* read failures.

View File

@ -754,12 +754,25 @@ u3u_uncram(c3_c* dir_c, c3_d eve_d)
// XX errors are fatal, barring a full "u3m_reboot"-type operation.
//
{
u3_noun roc, cod, ref = u3s_cue_xeno(len_d, byt_y);
ur_dict32_t dic_u = {0};
u3_noun roc, cod, ref;
if ( u3r_pq(ref, c3__fast, &roc, &cod) ) {
u3z(ref);
// XX tune the initial dictionary size for less reallocation
//
ur_dict32_grow((ur_root_t*)0, &dic_u, ur_fib33, ur_fib34);
if ( c3n == u3s_cue_xeno_unsafe(&dic_u, len_d, byt_y, &ref) ) {
fprintf(stderr, "uncram: failed to cue rock\r\n");
ur_dict_free((ur_dict_t*)&dic_u);
return c3n;
}
ur_dict_free((ur_dict_t*)&dic_u);
if ( c3n == u3r_pq(ref, c3__fast, &roc, &cod) ) {
fprintf(stderr, "uncram: failed: invalid rock format\r\n");
exit(1);
u3z(ref);
return c3n;
}
u3A->roc = u3k(roc);

View File

@ -1,14 +1,6 @@
#include "all.h"
#include "ur/ur.h"
// XX not declared in serial.h due to presence of ur_* types
//
ur_cue_res_e
u3s_cue_xeno_unsafe(ur_dict32_t* dic_u,
c3_d len_d,
const c3_y* byt_y,
u3_noun* out);
/* _setup(): prepare for tests.
*/
static void
@ -380,7 +372,7 @@ _test_cue_spec(const c3_c* cap_c,
ur_dict32_grow((ur_root_t*)0, &dic_u, ur_fib10, ur_fib11);
if ( ur_cue_good != u3s_cue_xeno_unsafe(&dic_u, len_w, byt_y, &out) ) {
if ( c3n == u3s_cue_xeno_unsafe(&dic_u, len_w, byt_y, &out) ) {
fprintf(stderr, "\033[31mcue %s fail 1\033[0m\r\n", cap_c);
ret_i = 0;
}