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
Provenance
- Commit
e843a4f- Tree
55ad290- Author
Muh Muhten <muh.muhten@gmail.com>- Committer
Nico Williams <nico@cryptonector.com>- Parent
b2b0bd3
6 files changed+54 added−9 removed
modified:src/builtin.c+2 added-2 removed
1741 unmodified lines1742 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 }1748 1749 static const char jq_builtins[] =1750 /* Include jq-coded builtins */41 unmodified lines1792 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);1795 1796- *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 lines221 }222 223 block 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 }229 230 block gen_op_bound(opcode op, block binder) {231 assert(block_is_single(binder));150 unmodified lines382 }383 return nrefs;384 }385 385-block block_bind(block binder, block body, int bindflags) {386+static 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 lines434 }435 return body;436 }437 438+static 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+}452+453+// Binds a sequence of binders, which *must not* alrady be bound to each other,454+// to body, throwing away unreferenced defs455+block block_bind_incremental(block binder, block body, int bindflags) {456+ assert(block_has_only_binders(binder, bindflags));457+ bindflags |= OP_HAS_BINDING;458+459+ inst* curr;460+ while ((curr = block_take_last(&binder))) {461+ body = block_bind_referenced(inst_block(curr), body, bindflags);462+ }463+ return body;464+}465+466+block 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();470+471+ 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+}479+480 static 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 lines1116 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 linesmodified:src/compile.h+2 added-1 removed
70 unmodified lines71 int block_has_only_binders(block, int bindflags);72 int block_has_main(block);73 int block_is_funcdef(block b);74 int block_is_single(block b);75-block block_bind(block binder, block body, int bindflags);75 block block_bind_library(block binder, block body, int bindflags, const char* libname);76 block block_bind_referenced(block binder, block body, int bindflags);77+block block_bind_incremental(block binder, block body, int bindflags);78+block block_bind_self(block binder, int bindflags);79 block block_drop_unreferenced(block body);80 81 jv block_take_imports(block* body);82 jv block_list_funcs(block body, int omit_underscores);25 unmodified linesmodified:src/linker.c+1 added
334 unmodified lines335 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 linesmodified:src/parser.c+1 added-1 removed
2421 unmodified lines2422 2423 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 linesmodified:src/parser.y+1 added-1 removed
329 unmodified lines330 %empty {331 $$ = gen_noop();332 } |333 FuncDef FuncDefs {334- $$ = block_bind($1, $2, OP_IS_CALL_PSEUDO);334+ $$ = block_join($1, $2);335 }336 337 Exp:338 FuncDef Exp %prec FUNCDEF {645 unmodified lines