Skip to content
Myr

Make builtin binding fast again by binding only referenced symbols

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Avoid doing the internal binding of top-level symbols in the parser, leaving that work to be done in a post-processing step. For builtins, this lets us do a reference-aware bind step (block_bind_incremental) *after* generating builtins/0. Libraries are a bit trickier since they may be bound multiple times, so instead of thinking through the implications I added (block_bind_self) to resolve all internal symbols immediately.

authored by Muh Muhten · committed by Nico Williams · parent b2b0bd3 · Browse files · 6 files+54 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+54 added−9 removed

  • other:+54 added−9 removed

6 files+54 added−9 removed

modified:src/builtin.c+2 added-2 removed
1741 unmodified lines
1742 builtins = BLOCK(builtins, gen_function("range",1743 BLOCK(gen_param("start"), gen_param("end")),1744 range));1745 }1746 return block_bind(builtins, b, OP_IS_CALL_PSEUDO);1746 return BLOCK(builtins, b);1747}17481749static const char jq_builtins[] =1750/* Include jq-coded builtins */
41 unmodified lines
1792 builtins = bind_bytecoded_builtins(builtins);1793 builtins = gen_cbinding(function_list, sizeof(function_list)/sizeof(function_list[0]), builtins);1794 builtins = gen_builtin_list(builtins);17951796 *bb = block_bind(builtins, *bb, OP_IS_CALL_PSEUDO);1796 *bb = block_bind_incremental(builtins, *bb, OP_IS_CALL_PSEUDO);1797 *bb = block_drop_unreferenced(*bb);1798 return nerrors;1799}
modified:src/compile.c+47 added-4 removed
220 unmodified lines
221}222223block gen_op_var_fresh(opcode op, const char* name) {224 assert(opcode_describe(op)->flags & OP_HAS_VARIABLE);225 return block_bind(gen_op_unbound(op, name),226 gen_noop(), OP_HAS_VARIABLE);225 block b = gen_op_unbound(op, name);226 b.first->bound_by = b.first;227 return b;228}229230block gen_op_bound(opcode op, block binder) {231 assert(block_is_single(binder));
150 unmodified lines
382 }383 return nrefs;384}385385block block_bind(block binder, block body, int bindflags) {386static block block_bind(block binder, block body, int bindflags) {387 block_bind_each(binder, body, bindflags);388 return block_join(binder, body);389}390
43 unmodified lines
434 }435 return body;436}437438static inst* block_take_last(block* b) {439 inst* i = b->last;440 if (i == 0)441 return 0;442 if (i->prev) {443 i->prev->next = i->next;444 b->last = i->prev;445 i->prev = 0;446 } else {447 b->first = 0;448 b->last = 0;449 }450 return i;451}452453// Binds a sequence of binders, which *must not* alrady be bound to each other,454// to body, throwing away unreferenced defs455block block_bind_incremental(block binder, block body, int bindflags) {456 assert(block_has_only_binders(binder, bindflags));457 bindflags |= OP_HAS_BINDING;458459 inst* curr;460 while ((curr = block_take_last(&binder))) {461 body = block_bind_referenced(inst_block(curr), body, bindflags);462 }463 return body;464}465466block block_bind_self(block binder, int bindflags) {467 assert(block_has_only_binders(binder, bindflags));468 bindflags |= OP_HAS_BINDING;469 block body = gen_noop();470471 inst* curr;472 while ((curr = block_take_last(&binder))) {473 block b = inst_block(curr);474 block_bind_subblock(b, body, bindflags, 0);475 body = BLOCK(b, body);476 }477 return body;478}479480static void block_mark_referenced(block body) {481 int saw_top = 0;482 for (inst* i = body.last; i; i = i->prev) {483 if (saw_top && i->bound_by == i && !i->referenced)
632 unmodified lines
1116 inst* i = inst_new(CLOSURE_CREATE_C);1117 i->imm.cfunc = &cfunctions[cfunc];1118 i->symbol = strdup(i->imm.cfunc->name);1119 i->any_unbound = 0;1077 code = block_bind(inst_block(i), code, OP_IS_CALL_PSEUDO);1120 code = BLOCK(inst_block(i), code);1121 }1122 return code;1123}1124
298 unmodified lines
modified:src/compile.h+2 added-1 removed
70 unmodified lines
71int block_has_only_binders(block, int bindflags);72int block_has_main(block);73int block_is_funcdef(block b);74int block_is_single(block b);75block block_bind(block binder, block body, int bindflags);75block block_bind_library(block binder, block body, int bindflags, const char* libname);76block block_bind_referenced(block binder, block body, int bindflags);77block block_bind_incremental(block binder, block body, int bindflags);78block block_bind_self(block binder, int bindflags);79block block_drop_unreferenced(block body);8081jv block_take_imports(block* body);82jv block_list_funcs(block body, int omit_underscores);
25 unmodified lines
modified:src/linker.c+1 added
334 unmodified lines
335 nerrors += process_dependencies(jq, jq_get_jq_origin(jq),336 jv_string(dirname(lib_origin)),337 &program, lib_state);338 free(lib_origin);339 program = block_bind_self(program, OP_IS_CALL_PSEUDO);340 }341 }342 state_idx = lib_state->ct++;343 lib_state->names = jv_mem_realloc(lib_state->names, lib_state->ct * sizeof(const char *));
75 unmodified lines
modified:src/parser.c+1 added-1 removed
2421 unmodified lines
24222423 case 9:2424#line 333 "src/parser.y" /* yacc.c:1646 */2425 {2426 (yyval.blk) = block_bind((yyvsp[-1].blk), (yyvsp[0].blk), OP_IS_CALL_PSEUDO);2426 (yyval.blk) = block_join((yyvsp[-1].blk), (yyvsp[0].blk));2427}2428#line 2429 "src/parser.c" /* yacc.c:1646 */2429 break;2430
1571 unmodified lines
modified:src/parser.y+1 added-1 removed
329 unmodified lines
330%empty {331 $$ = gen_noop();332} |333FuncDef FuncDefs {334 $$ = block_bind($1, $2, OP_IS_CALL_PSEUDO);334 $$ = block_join($1, $2);335}336337Exp:338FuncDef Exp %prec FUNCDEF {
645 unmodified lines