Skip to content

Commit

Permalink
array_values: cache the db results
Browse files Browse the repository at this point in the history
Caching the DB results can result in a large speedup.  In the kernel it's
especially noticeable when parsing the RMDA code.

Signed-off-by: Dan Carpenter <[email protected]>
  • Loading branch information
Dan Carpenter committed Nov 26, 2021
1 parent 91652fd commit 686115b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions smatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ int get_db_type_rl(struct expression *expr, struct range_list **rl);
/* smatch_data_val.c */
int get_mtag_rl(struct expression *expr, struct range_list **rl);
/* smatch_array_values.c */
void clear_array_values_cache(void);
int get_array_rl(struct expression *expr, struct range_list **rl);

/* smatch_states.c */
Expand Down
43 changes: 42 additions & 1 deletion smatch_array_values.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,41 @@ static char *get_array_name(struct expression *array)
return NULL;
}

static struct {
const char *name;
struct range_list *rl;
} cached_results[4];

static bool get_cached_select(const char *name, struct range_list **rl)
{
int i;

for (i = 0; i < ARRAY_SIZE(cached_results); i++) {
if (!cached_results[i].name)
continue;
if (strcmp(name, cached_results[i].name) == 0) {
*rl = cached_results[i].rl;
return true;
}
}
return false;
}

static void store_result(const char *name, struct range_list *rl)
{
static int idx;

idx = (idx + 1) % ARRAY_SIZE(cached_results);

cached_results[idx].name = name;
cached_results[idx].rl = rl;
}

void clear_array_values_cache(void)
{
memset(cached_results, 0, sizeof(cached_results));
}

int get_array_rl(struct expression *expr, struct range_list **rl)
{
struct expression *array;
Expand All @@ -123,6 +158,9 @@ int get_array_rl(struct expression *expr, struct range_list **rl)
if (!name)
return 0;

if (get_cached_select(name, rl))
return 1;

if (is_file_local(array)) {
run_sql(&get_vals, &db_info,
"select value from sink_info where file = '%s' and static = 1 and sink_name = '%s' and type = %d;",
Expand All @@ -132,7 +170,10 @@ int get_array_rl(struct expression *expr, struct range_list **rl)
"select value from sink_info where sink_name = '%s' and type = %d limit 10;",
name, DATA_VALUE);
}
if (!db_info.rl || db_info.count >= 10)
if (db_info.count >= 10)
db_info.rl = NULL;
store_result(name, db_info.rl);
if (!db_info.rl)
return 0;

*rl = db_info.rl;
Expand Down
1 change: 1 addition & 0 deletions smatch_ranges.c
Original file line number Diff line number Diff line change
Expand Up @@ -2143,6 +2143,7 @@ void free_data_info_allocs(void)
blob_free(blob, desc->chunking);
blob = next;
}
clear_array_values_cache();
clear_type_value_cache();
clear_data_range_alloc();
}
Expand Down
1 change: 1 addition & 0 deletions smatch_states.c
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,7 @@ void clear_all_states(void)

free_goto_stack();

clear_array_values_cache();
free_every_single_sm_state();
free_tmp_expressions();
}
Expand Down

0 comments on commit 686115b

Please sign in to comment.