Skip to content

Commit

Permalink
u3: avoids unnecessary stores to memory in melt
Browse files Browse the repository at this point in the history
  • Loading branch information
joemfb committed Apr 11, 2024
1 parent ddca8bc commit f20cfa3
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions pkg/noun/urth.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,11 @@ _cj_warm_tap(u3_noun kev, void* wit)
}

static inline u3_weak
_get(u3_noun som, u3p(u3h_root) set_p)
_cu_melt_get(u3p(u3h_root) set_p, u3_noun som)
{
u3_post hav_p = u3h_git(set_p, som);

if (u3_none == hav_p)
{
if ( u3_none == hav_p ) {
return u3_none;
}

Expand All @@ -489,52 +488,47 @@ _get(u3_noun som, u3p(u3h_root) set_p)
}

static inline void
_put(u3_noun som, u3p(u3h_root) set_p)
_cu_melt_put(u3p(u3h_root) set_p, u3_noun som)
{
// strip tag bits from [som] to skip refcounts
//
u3_post hav_p = u3a_to_off(som);
u3h_put(set_p, som, hav_p);
}

static u3_noun
_traverse(u3_noun som, u3p(u3h_root) set_p)
static void
_cu_melt_noun(u3p(u3h_root) set_p, u3_noun* mos)
{
u3_noun som = *mos;
u3_weak hav;

// skip direct atoms
//
if (c3y == u3a_is_cat(som))
{
return som;
if ( c3y == u3a_is_cat(som) ) {
return;
}

// [som] equals [hav], and [hav] is canonical
//
if (u3_none != (hav = _get(som, set_p)))
{
if ( som == hav ) {
return som;
if ( u3_none != (hav = _cu_melt_get(set_p, som)) ) {
if ( hav != som ) {
u3z(som);
*mos = u3k(hav);
}

u3z(som);
return u3k(hav);
return;
}

// traverse subtrees
//
if (c3y == u3a_is_cell(som))
{
if ( c3y == u3a_is_cell(som) ) {
u3a_cell *cel_u = u3a_to_ptr(som);

cel_u->hed = _traverse(cel_u->hed, set_p);
cel_u->tel = _traverse(cel_u->tel, set_p);
_cu_melt_noun(set_p, &cel_u->hed);
_cu_melt_noun(set_p, &cel_u->tel);
}

// [som] is canonical
//
_put(som, set_p);
return som;
_cu_melt_put(set_p, som);
}

/* u3u_melt(): globally deduplicate memory and pack in-place.
Expand Down Expand Up @@ -570,8 +564,8 @@ u3u_melt(void)

u3p(u3h_root) set_p = u3h_new(); // temp hashtable

cod = _traverse(cod, set_p); // melt the jets
u3A->roc = _traverse(u3A->roc, set_p); // melt the kernel
_cu_melt_noun(set_p, &cod); // melt the jets
_cu_melt_noun(set_p, &u3A->roc); // melt the kernel

u3h_free(set_p); // release the temp hashtable

Expand Down

0 comments on commit f20cfa3

Please sign in to comment.