Skip to content

Commit

Permalink
CLEANUP: Clause indexing code.
Browse files Browse the repository at this point in the history
Split into smaller functions, use `bool` type, avoid `goto`,
add more comments, etc. This is a preparation before improvements.
  • Loading branch information
JanWielemaker committed Jan 1, 2025
1 parent 04f1f56 commit f8fdcb3
Show file tree
Hide file tree
Showing 7 changed files with 475 additions and 289 deletions.
14 changes: 8 additions & 6 deletions src/pl-comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4812,9 +4812,11 @@ machine code.
NOTE: this function must be kept consistent with indexOfWord() in
pl-index.c.
@return `true` when argument can be indexed
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

int
bool
argKey(Code PC, int skip, word *key)
{ if ( skip > 0 )
PC = skipArgs(PC, skip);
Expand Down Expand Up @@ -4887,7 +4889,7 @@ argKey(Code PC, int skip, word *key)
case I_SSU_COMMIT:
case I_SSU_CHOICE:
*key = 0;
fail;
return false;
case I_NOP:
case I_CHP:
continue;
Expand All @@ -4900,7 +4902,7 @@ argKey(Code PC, int skip, word *key)
Sdprintf("Unexpected VM code %" PRIuPTR " at %p\n", c, PC);
Sdprintf("\topcode=%s\n", codeTable[c].name);
assert(0);
fail;
return false;
}
}
}
Expand All @@ -4912,7 +4914,7 @@ first argument. This is used by listSupervisor(). This used to share
with argKey(), but argKey() is time critical and merging complicates it.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

int
bool
arg1Key(Code PC, word *key)
{ for(;;)
{ code c = decode(*PC++);
Expand Down Expand Up @@ -4964,7 +4966,7 @@ arg1Key(Code PC, word *key)
case I_SSU_CHOICE:
case I_SSU_COMMIT:
*key = 0;
fail;
return false;
case I_NOP:
case I_CHP:
continue;
Expand All @@ -4975,7 +4977,7 @@ arg1Key(Code PC, word *key)
#endif
default:
assert(0);
fail;
return false;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/pl-comp.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ bool decompileHead(Clause clause, term_t head);
int det_goal_error(LocalFrame fr, Code PC,
atom_t found);
Code skipArgs(Code PC, int skip);
int argKey(Code PC, int skip, word *key);
int arg1Key(Code PC, word *key);
bool argKey(Code PC, int skip, word *key);
bool arg1Key(Code PC, word *key);
bool decompile(Clause clause, term_t term, term_t bindings);
word pl_nth_clause(term_t p, term_t n, term_t ref,
control_t h);
Expand Down
15 changes: 6 additions & 9 deletions src/pl-incl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,8 @@ We assume the compiler will optimise this properly.
} \
}

typedef unsigned char iarg_t; /* index argument */

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Structure declarations that must be shared across multiple files.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
Expand Down Expand Up @@ -1449,7 +1451,6 @@ typedef struct impl_local
LocalDefinitions local; /* P_THREAD_LOCAL predicates */
} impl_local, *ImplLocal;


typedef struct clause_list
{ arg_info *args; /* Meta and indexing info */
ClauseRef first_clause; /* clause list of procedure */
Expand All @@ -1458,7 +1459,8 @@ typedef struct clause_list
unsigned int number_of_clauses; /* number of associated clauses */
unsigned int erased_clauses; /* number of erased clauses in set */
unsigned int number_of_rules; /* number of real rules */
unsigned int jiti_tried; /* number of times we tried to find */
iarg_t jiti_tried; /* number of times we tried to find */
iarg_t primary_index; /* Index used to link clauses */
} clause_list, *ClauseList;

typedef struct clause_ref
Expand Down Expand Up @@ -1610,12 +1612,10 @@ struct clause_bucket
};

#define MAX_MULTI_INDEX 4
#define MAXINDEXARG 254
#define MAXINDEXARG 254 /* must fit iarg_t */
#define MAXINDEXDEPTH 7
#define END_INDEX_POS 255

typedef unsigned char iarg_t; /* index argument */

struct clause_index
{ unsigned int buckets; /* # entries */
unsigned int size; /* # clauses */
Expand All @@ -1625,6 +1625,7 @@ struct clause_index
unsigned is_list : 1; /* Index with lists */
unsigned incomplete : 1; /* Index is incomplete */
unsigned invalid : 1; /* Index is invalid */
unsigned good : 1; /* Index is (near) perfect */
iarg_t args[MAX_MULTI_INDEX]; /* Indexed arguments */
iarg_t position[MAXINDEXDEPTH+1]; /* Deep index position */
float speedup; /* Estimated speedup */
Expand Down Expand Up @@ -1658,10 +1659,6 @@ struct definition
#if defined(__SANITIZE_ADDRESS__)
char *name; /* Name for debugging */
#endif
#ifdef O_PROF_PENTIUM
int prof_index; /* index in profiling */
char *prof_name; /* name in profiling */
#endif
};

struct definition_chain
Expand Down
Loading

0 comments on commit f8fdcb3

Please sign in to comment.