Skip to content

Commit

Permalink
Speedup for term_expansion/2 processing, re issue #340
Browse files Browse the repository at this point in the history
  • Loading branch information
infradig committed Sep 20, 2023
1 parent 950ed01 commit 8857da5
Showing 1 changed file with 14 additions and 34 deletions.
48 changes: 14 additions & 34 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1678,53 +1678,33 @@ static bool term_expansion(parser *p)

query *q = query_create(p->m, true);
check_error(q);
char *dst = print_canonical_to_strbuf(q, p->cl->cells, 0, 0);
SB(s);
SB_sprintf(s, "term_expansion((%s),_TermOut), !.", dst);
free(dst);

parser *p2 = parser_create(p->m);
check_error(p2, query_destroy(q));
p2->line_nbr = p->line_nbr;
p2->skip = true;
p2->srcptr = SB_cstr(s);
tokenize(p2, false, false);
xref_rule(p2->m, p2->cl, NULL);
execute(q, p2->cl->cells, p2->cl->nbr_vars);
SB_free(s);
cell *c = p->cl->cells;
cell *tmp = alloc_on_heap(q, 1+c->nbr_cells+1+1);
make_struct(tmp, new_atom(p->pl, "term_expansion"), NULL, 2, c->nbr_cells+1);
safe_copy_cells(tmp+1, p->cl->cells, c->nbr_cells);
make_ref(tmp+1+c->nbr_cells, g_anon_s, p->cl->nbr_vars, 0);
make_end(tmp+1+c->nbr_cells+1);
execute(q, tmp, p->cl->nbr_vars+1);

if (q->retry != QUERY_OK) {
parser_destroy(p2);
query_destroy(q);
return false;
}

frame *f = GET_FIRST_FRAME();
char *src = NULL;

for (unsigned i = 0; i < p2->cl->nbr_vars; i++) {
if (strcmp(p2->vartab.var_name[i], "_TermOut"))
continue;

slot *e = GET_SLOT(f, i);

if (is_empty(&e->c))
continue;

cell *c = deref(q, &e->c, e->c.var_ctx);
src = print_canonical_to_strbuf(q, c, q->latest_ctx, 1);
strcat(src, ".");
break;
}
cell *arg1 = tmp + 1;
cell *arg2 = arg1 + arg1->nbr_cells;
c = deref(q, arg2, 0);
char *src = print_canonical_to_strbuf(q, c, q->latest_ctx, 1);

if (!src) {
parser_destroy(p2);
query_destroy(q);
p->error = true;
return false;
}

reset(p2);
strcat(src, ".");
parser *p2 = parser_create(p->m);
check_error(p2);
p2->srcptr = src;
tokenize(p2, false, false);
xref_rule(p2->m, p2->cl, NULL);
Expand Down

0 comments on commit 8857da5

Please sign in to comment.