10000 vere: adds melt, rewrites meld for efficiency by joemfb · Pull Request #632 · urbit/vere · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

vere: adds melt, rewrites meld for efficiency #632

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

Open
wants to merge 22 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d2cc948
fix up an outdated comment
ngzax Jul 15, 2022
86ed375
basic `melt` runner as a copy of `meld`
ngzax Jul 15, 2022
2c4a084
pseudocode impl of u3U_melt()
ngzax Jul 21, 2022
bd329e2
initial jfb implementation
ngzax Jul 21, 2022
5c22909
re-pave all the necessary road parts
ngzax Jul 22, 2022
c0dbd99
refactor/extract-method
ngzax Jul 22, 2022
09b850a
fix a refcounting issue. reorganize a bit.
ngzax Aug 19, 2022
b7d2088
improve comments
ngzax Oct 12, 2022
20103bf
u3: revive melt, add cli args
joemfb Apr 11, 2024
9d2efcb
u3: updates melt to properly handle compressed noun references
joemfb Apr 11, 2024
0ae7e1d
u3: clear persistent memo cache in melt
joemfb Apr 11, 2024
8a4d083
u3: more efficient tag-bits restoration in melt
joemfb Apr 11, 2024
ddca8bc
u3: avoid unnecessary refcount churn in melt
joemfb Apr 11, 2024
f20cfa3
u3: avoids unnecessary stores to memory in melt
joemfb Apr 11, 2024
a4aa77e
Merge branch 'master' into jb/melt
joemfb Oct 17, 2024
5959b62
M 10000 erge branch 'develop' into jb/melt
joemfb Dec 11, 2024
8d4796f
Merge branch 'develop' into jb/melt
joemfb May 16, 2025
8ea72c2
u3: refactors melt implementation, reverting unneeded u3 changes
joemfb May 16, 2025
3ae02ed
vere: adds new, verstable-based implementation of melt and meld
joemfb May 16, 2025
3622a4d
vere: switches to new meld/melt, deprecates old implementations
joemfb May 16, 2025
2f9d8f3
vere: melt: assume cached mugs are present in cmp functions
joemfb May 16, 2025
5c39b5a
vere: melt: remove confusing use of u3a_is_cat()
joemfb May 16, 2025
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
131 changes: 131 additions & 0 deletions pkg/noun/urth.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "imprison.h"
#include "jets.h"
#include "manage.h"
#include "options.h"
#include "retrieve.h"
#include "serial.h"
#include "ur/ur.h"
Expand Down Expand Up @@ -460,6 +461,136 @@ u3u_meld(void)
}
#endif

/* BEGIN helper functions for u3u_melt
-------------------------------------------------------------------
*/
/* _cj_warm_tap(): tap war_p to rel
*/
static void
_cj_warm_tap(u3_noun kev, void* wit)
{
u3_noun* rel = wit;
*rel = u3nc(u3k(kev), *rel);
}

static inline u3_weak
_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 ) {
return u3_none;
}

// restore tag bits from [som]
//
return (hav_p >> u3a_vits) | (som & 0xc0000000);
}

static inline void
_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 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] equals [hav], and [hav] is canonical
//
if ( u3_none != (hav = _cu_melt_get(set_p, som)) ) {
if ( hav != som ) {
u3z(som);
*mos = u3k(hav);
}
return;
}

// traverse subtrees
//
if ( c3y == u3a_is_cell(som) ) {
u3a_cell *cel_u = u3a_to_ptr(som);
_cu_melt_noun(set_p, &cel_u->hed);
_cu_melt_noun(set_p, &cel_u->tel);
}

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

/* u3u_melt(): globally deduplicate memory and pack in-place.
*/
c3_w
u3u_melt(void)
{
c3_w pre_w = u3a_open(u3R);

// Verify that we're on the main road.
//
u3_assert( &(u3H->rod_u) == u3R );

// Store a cons list of the cold jet registrations in `cod`
//
u3_noun cod = u3_nul;
u3h_walk_with(u3R->jed.cod_p, _cj_warm_tap, &cod);

u3m_reclaim(); // refresh the byte-code interpreter.

u3h_free(u3R->cax.per_p);
u3R->cax.per_p = u3h_new_cache(u3C.per_w);

u3h_free(u3R->jed.cod_p);
u3R->jed.cod_p = u3h_new();

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

_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
}

// re-initialize the jets
//
u3j_boot(c3y);

// Put the jet registrations back. Loop over cod putting them back into the cold jet
// dashboard. Then re-run the garbage collector.
//
{
u3_noun codc = cod;

while(u3_nul != cod) {
u3_noun kev = u3h(cod);
u3h_put(u3R->jed.cod_p, u3h(kev), u3k(u3t(kev)));
cod = u3t(cod);
}

u3z(codc);
}

// remove free space
//
u3j_ream();
u3m_pack();

return (u3a_open(u3R) - pre_w);
}

/* _cu_rock_path(): format rock path.
*/
static c3_o
Expand Down
7 changes: 6 additions & 1 deletion pkg/noun/urth.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
/* u3u_meld(): globally deduplicate memory, returns u3a_open delta.
*/
c3_w
u3u_meld(void);
u3u_meld(void) __attribute__ ((deprecated));

/* u3u_melt(): globally deduplicate memory and pack in-place.
*/
c3_w
u3u_melt(void) __attribute__ ((deprecated));

/* u3u_cram(): globably deduplicate memory, and write a rock to disk.
*/
Expand Down
1 change: 1 addition & 0 deletions pkg/vere/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ const c_source_files = [_][]const u8{
"lord.c",
"mars.c",
"mdns.c",
"melt.c",
"newt.c",
"pier.c",
"save.c",
Expand Down
96 changes: 93 additions & 3 deletions pkg/vere/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,8 @@ _cw_usage(c3_c* bin_c)
" %s dock %.*s copy binary:\n",
" %s grab %.*s measure memory usage:\n",
" %s info %.*s print pier info:\n",
" %s meld %.*s deduplicate snapshot:\n",
" %s meld %.*s deduplicate and minimize snapshot:\n",
" %s melt %.*s deduplicate snapshot:\n",
" %s pack %.*s defragment snapshot:\n",
" %s play %.*s recompute events:\n",
" %s prep %.*s prepare for upgrade:\n",
Expand Down Expand Up @@ -2005,7 +2006,7 @@ _cw_queu(c3_i argc, c3_c* argv[])
}
}

/* _cw_uniq(): deduplicate persistent nouns
/* _cw_meld(): canonicalize persistent nouns and compact state.
*/
static void
_cw_meld(c3_i argc, c3_c* argv[])
Expand Down Expand Up @@ -2094,7 +2095,95 @@ _cw_meld(c3_i argc, c3_c* argv[])
u3_Host.eve_d = u3m_boot(u3_Host.dir_c, (size_t)1 << u3_Host.ops_u.lom_y);
u3_disk* log_u = _cw_disk_init(u3_Host.dir_c); // XX s/b try_aquire lock

u3a_print_memory(stderr, "urbit: meld: gained", u3u_meld());
u3a_print_memory(stderr, "urbit: meld: gained", u3_meld_all(stderr));

u3m_save();
u3_disk_exit(log_u);
u3m_stop();
}

/* _cw_melt(): canonicalize persistent nouns.
*/
static void
_cw_melt(c3_i argc, c3_c* argv[])
{
c3_i ch_i, lid_i;
c3_w arg_w;

static struct option lop_u[] = {
{ "loom", required_argument, NULL, c3__loom },
{ "no-demand", no_argument, NULL, 6 },
{ "swap", no_argument, NULL, 7 },
{ "swap-to", required_argument, NULL, 8 },
{ "gc-early", no_argument, NULL, 9 },
{ NULL, 0, NULL, 0 }
};

u3_Host.dir_c = _main_pier_run(argv[0]);

while ( -1 != (ch_i=getopt_long(argc, argv, "", lop_u, &lid_i)) ) {
switch ( ch_i ) {
case c3__loom: {
if (_main_readw_loom("loom", &u3_Host.ops_u.lom_y)) {
exit(1);
}
} break;

case 6: { // no-demand
u3_Host.ops_u.map = c3n;
u3C.wag_w |= u3o_no_demand;
} break;

case 7: { // swap
u3_Host.ops_u.eph = c3y;
u3C.wag_w |= u3o_swap;
} break;

case 8: { // swap-to
u3_Host.ops_u.eph = c3y;
u3C.wag_w |= u3o_swap;
u3C.eph_c = strdup(optarg);
break;
}

case 9: { // gc-early
u3C.wag_w |= u3o_check_corrupt;
break;
}

case '?': {
fprintf(stderr, "invalid argument\r\n");
exit(1);
} break;
}
}

// argv[optind] is always "melt"
//

if ( !u3_Host.dir_c ) {
if ( optind + 1 < argc ) {
u3_Host.dir_c = argv[optind + 1];
}
else {
fprintf(stderr, "invalid command, pier required\r\n");
exit(1);
}

optind++;
}

if ( optind + 1 != argc ) {
fprintf(stderr, "invalid command\r\n");
exit(1);
}

u3C.wag_w |= u3o_hashless;

u3_Host.eve_d = u3m_boot(u3_Host.dir_c, (size_t)1 << u3_Host.ops_u.lom_y);
u3_disk* log_u = _cw_disk_init(u3_Host.dir_c); // XX s/b try_aquire lock

u3a_print_memory(stderr, "urbit: melt: gained", u3_melt_all(stderr));

u3m_save();
u3_disk_exit(log_u);
Expand Down Expand Up @@ -3147,6 +3236,7 @@ _cw_utils(c3_i argc, c3_c* argv[])

case c3__info: _cw_info(argc, argv); return 1;
case c3__meld: _cw_meld(argc, argv); return 1;
case c3__melt: _cw_melt(argc, argv); return 1;
case c3__next: _cw_next(argc, argv); return 2; // continue on
case c3__pack: _cw_pack(argc, argv); return 1;
case c3__play: _cw_play(argc, argv); return 1;
Expand Down
2 changes: 1 addition & 1 deletion pkg/vere/mars.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ u3_mars_play(u3_mars* mar_u, c3_d eve_d, c3_d sap_d)
// XX pack before meld?
//
if ( u3C.wag_w & u3o_auto_meld ) {
u3a_print_memory(stderr, "mars: meld: gained", u3u_meld());
u3a_print_memory(stderr, "mars: meld: gained", u3_meld_all(stderr));
}
else {
u3a_print_memory(stderr, "mars: pack: gained", u3m_pack());
Expand Down
Loading
0