Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

u3: restore old-style, efficient reap to u3m_love() #538

Merged
merged 5 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions pkg/noun/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,31 @@ u3h_take(u3p(u3h_root) har_p)
return u3h_take_with(har_p, u3a_take);
}

/* _ch_take_uni_cb(): take a key/value pair, put into [dst_p].
*/
static void
_ch_take_uni_cb(u3_cell kev, void* wit)
{
u3a_cell* kev_u = u3a_to_ptr(kev);
u3_noun key = u3a_take(kev_u->hed);
u3_noun val = u3a_take(kev_u->tel);

{
u3p(u3h_root) dst_p = *(u3p(u3h_root)*)wit;
u3h_put(dst_p, key, val);
}

u3z(key);
}

/* u3h_take_uni(): take entries from [src_p], put into [dst_p].
*/
void
u3h_take_uni(u3p(u3h_root) dst_p, u3p(u3h_root) src_p)
{
u3h_walk_with(src_p, _ch_take_uni_cb, &dst_p);
}

/* _ch_mark_buck(): mark bucket for gc.
*/
c3_w
Expand Down
5 changes: 5 additions & 0 deletions pkg/noun/hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@
u3p(u3h_root)
u3h_take(u3p(u3h_root) har_p);

/* u3h_take_uni(): take entries from [src_p], put into [dst_p].
*/
void
u3h_take_uni(u3p(u3h_root) dst_p, u3p(u3h_root) src_p);

/* u3h_wyt(): number of entries
*/
c3_w
Expand Down
109 changes: 40 additions & 69 deletions pkg/noun/jets.c
Original file line number Diff line number Diff line change
Expand Up @@ -1990,94 +1990,65 @@ u3j_rite_mine(u3j_rite* rit_u, u3_noun clu, u3_noun cor)
u3t_off(glu_o);
}

/* _cj_take_hank_cb(): u3h_take_with cb for taking hanks
/* _cj_reap_hank(): promote call site.
*/
static u3p(_cj_hank)
_cj_take_hank_cb(u3p(_cj_hank) nah_p)
static void
_cj_reap_hank(u3_cell kev)
{
_cj_hank* nah_u = u3to(_cj_hank, nah_p);
_cj_hank* han_u = u3a_walloc(c3_wiseof(_cj_hank));
u3a_cell* kev_u = u3a_to_ptr(kev);
_cj_hank* nah_u = u3to(_cj_hank, kev_u->tel);
_cj_hank* han_u;
u3_weak got;
u3_noun key;

if ( u3_none == nah_u->hax ) {
han_u->hax = u3_none;
// han_u->sit_u left uninitialized, will be ignored
}
else {
han_u->hax = u3a_take(nah_u->hax);
u3j_site_take(&(han_u->sit_u), &(nah_u->sit_u));
return;
}

return u3of(_cj_hank, han_u);
}

/* u3j_take(): copy junior jet state.
*/
u3a_jets
u3j_take(u3a_jets jed_u)
{
jed_u.war_p = u3h_take(jed_u.war_p);
jed_u.cod_p = u3h_take(jed_u.cod_p);
jed_u.han_p = u3h_take_with(jed_u.han_p, _cj_take_hank_cb);
jed_u.bas_p = u3h_take(jed_u.bas_p);
return jed_u;
}

/* _cj_merge_hank_cb(): u3h_uni_with cb for integrating taken hanks
** NB "transfers" or frees hanks in jed_u.han_p
*/
static void
_cj_merge_hank_cb(u3_noun kev, void* wit)
{
u3p(u3h_root) han_p = *(u3p(u3h_root)*)wit;
_cj_hank* nah_u;
u3_noun key;
u3p(_cj_hank) nah_p;
u3x_cell(kev, &key, &nah_p);

nah_u = u3to(_cj_hank, nah_p);
// you have to keep what you take
//
key = u3a_take(kev_u->hed);
got = u3h_git(u3R->jed.han_p, key);

if ( u3_none == nah_u->hax ) {
u3a_wfree(nah_u);
// promote
//
if ( u3_none == got ) {
han_u = u3a_walloc(c3_wiseof(_cj_hank));
han_u->hax = u3a_take(nah_u->hax);
u3j_site_take(&(han_u->sit_u), &(nah_u->sit_u));
}
// integrate
//
else {
_cj_hank* han_u;
u3_weak got = u3h_git(u3R->jed.han_p, key);
u3_weak old;

if ( u3_none == got ) {
han_u = nah_u;
}
else {
han_u = u3to(_cj_hank, got);

if ( u3_none != han_u->hax ) {
u3z(han_u->hax);
}
han_u->hax = nah_u->hax;
han_u = u3to(_cj_hank, got);
old = han_u->hax;
han_u->hax = u3a_take(nah_u->hax);
u3j_site_take(&(nah_u->sit_u), &(nah_u->sit_u));
u3j_site_merge(&(han_u->sit_u), &(nah_u->sit_u));

u3j_site_merge(&(han_u->sit_u), &(nah_u->sit_u));
u3a_wfree(nah_u);
if ( u3_none != old ) {
u3z(old);
}

u3h_put(han_p, key, u3of(_cj_hank, han_u));
}

u3h_put(u3R->jed.han_p, key, u3of(_cj_hank, han_u));
u3z(key);
}

/* u3j_reap(): promote jet state.
*/
void
u3j_reap(u3a_jets jed_u)
u3j_reap(u3a_jets* jed_u)
{
u3h_uni(u3R->jed.war_p, jed_u.war_p);
u3h_free(jed_u.war_p);

u3h_uni(u3R->jed.cod_p, jed_u.cod_p);
u3h_free(jed_u.cod_p);

u3h_walk_with(jed_u.han_p, _cj_merge_hank_cb, &u3R->jed.han_p);
u3h_free(jed_u.han_p);

u3h_uni(u3R->jed.bas_p, jed_u.bas_p);
u3h_free(jed_u.bas_p);
u3h_take_uni(u3R->jed.cod_p, jed_u->cod_p);
// call sites must be reaped before the warm dashboard;
// they may contain references to labels on this road
//
u3h_walk(jed_u->han_p, _cj_reap_hank);
u3h_take_uni(u3R->jed.war_p, jed_u->war_p);
u3h_take_uni(u3R->jed.bas_p, jed_u->bas_p);
}

/* _cj_ream(): ream list of battery [bash registry] pairs. RETAIN.
Expand Down
7 changes: 1 addition & 6 deletions pkg/noun/jets.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,7 @@
/* u3j_reap(): promote jet state.
*/
void
u3j_reap(u3a_jets jed_u);

/* u3j_take(): copy junior jet state.
*/
u3a_jets
u3j_take(u3a_jets jed_u);
u3j_reap(u3a_jets* jed_u);

/* u3j_rite_mine(): mine cor with clu, using u3j_rite for caching
*/
Expand Down
16 changes: 7 additions & 9 deletions pkg/noun/manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -1001,22 +1001,20 @@ u3m_love(u3_noun pro)
//
u3m_fall();

// copy product and caches off our stack
// copy product off our stack
//
pro = u3a_take(pro);
jed_u = u3j_take(jed_u);
byc_p = u3n_take(byc_p);
pro = u3a_take(pro);

// integrate junior caches
//
u3j_reap(&jed_u);
u3n_reap(byc_p);

// pop the stack
//
u3R->cap_p = u3R->ear_p;
u3R->ear_p = 0;

// integrate junior caches
//
u3j_reap(jed_u);
u3n_reap(byc_p);

return pro;
}

Expand Down
57 changes: 26 additions & 31 deletions pkg/noun/nock.c
Original file line number Diff line number Diff line change
Expand Up @@ -2866,12 +2866,11 @@ _cn_take_prog_dat(u3n_prog* dst_u, u3n_prog* src_u)
}
}

/* _cn_take_prog_cb(): u3h_take_with cb for taking junior u3n_prog's.
/* _cn_take_prog(): take junior u3n_prog.
*/
static u3p(u3n_prog)
_cn_take_prog_cb(u3p(u3n_prog) pog_p)
static u3n_prog*
_cn_take_prog(u3n_prog* pog_u)
{
u3n_prog* pog_u = u3to(u3n_prog, pog_p);
u3n_prog* gop_u;

if ( c3y == pog_u->byc_u.own_o ) {
Expand All @@ -2887,17 +2886,8 @@ _cn_take_prog_cb(u3p(u3n_prog) pog_p)
}

_cn_take_prog_dat(gop_u, pog_u);
// _n_prog_take_dat(gop_u, pog_u, c3n);

return u3of(u3n_prog, gop_u);
}

/* u3n_take(): copy junior bytecode state.
*/
u3p(u3h_root)
u3n_take(u3p(u3h_root) har_p)
{
return u3h_take_with(har_p, _cn_take_prog_cb);
return gop_u;
}

/* _cn_merge_prog_dat(): copy references from src_u u3n_prog to dst_u.
Expand Down Expand Up @@ -2931,39 +2921,44 @@ _cn_merge_prog_dat(u3n_prog* dst_u, u3n_prog* src_u)
}
}

/* _cn_merge_prog_cb(): u3h_walk_with cb for integrating taken u3n_prog's.
/* _cn_reap_prog_cb(): promote junior bytecode entry.
*/
static void
_cn_merge_prog_cb(u3_noun kev, void* wit)
_cn_reap_prog_cb(u3_cell kev)
{
u3p(u3h_root) har_p = *(u3p(u3h_root)*)wit;
u3n_prog* pog_u;
u3_weak got;
u3_noun key;
u3p(u3n_prog) pog_p;
u3x_cell(kev, &key, &pog_p);

pog_u = u3to(u3n_prog, pog_p);
got = u3h_git(har_p, key);
u3a_cell* kev_u = u3a_to_ptr(kev);
u3_noun key = u3a_take(kev_u->hed);
u3n_prog* pog_u = u3to(u3n_prog, kev_u->tel);
u3_weak got = u3h_git(u3R->byc.har_p, key);

if ( u3_none != got ) {
// promote
//
if ( u3_none == got ) {
pog_u = _cn_take_prog(pog_u);
}
// integrate
//
else {
u3n_prog* sep_u = u3to(u3n_prog, got);
_cn_take_prog_dat(pog_u, pog_u);
_cn_merge_prog_dat(sep_u, pog_u);
u3a_free(pog_u);
pog_u = sep_u;
}

u3h_put(har_p, key, u3of(u3n_prog, pog_u));
// we must always put, because we have taken.
// we must always keep what we have taken,
// or we can break relocation pointers.
//
u3h_put(u3R->byc.har_p, key, u3of(u3n_prog, pog_u));
u3z(key);
}

/* u3n_reap(): promote bytecode state.
*/
void
u3n_reap(u3p(u3h_root) har_p)
{
u3h_walk_with(har_p, _cn_merge_prog_cb, &u3R->byc.har_p);
// NB *not* u3n_free, _cn_merge_prog_cb() transfers u3n_prog's
u3h_free(har_p);
u3h_walk(har_p, _cn_reap_prog_cb);
}

/* _n_ream(): ream program call sites
Expand Down
5 changes: 0 additions & 5 deletions pkg/noun/nock.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,6 @@
void
u3n_reap(u3p(u3h_root) har_p);

/* u3n_take(): copy junior bytecode state.
*/
u3p(u3h_root)
u3n_take(u3p(u3h_root) har_p);

/* u3n_mark(): mark bytecode cache.
*/
c3_w
Expand Down