Skip to content

Commit

Permalink
lighw: Speed up ghw_read_cycle_cont without null-type signals
Browse files Browse the repository at this point in the history
This seems to be the regular case and provides a massive speedup (about
6x to 20x in a quick test). Also add a small sanity check.
  • Loading branch information
stefanlippuner committed Jan 13, 2024
1 parent 0e64856 commit 1728760
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
44 changes: 37 additions & 7 deletions ghw/libghw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,7 @@ ghw_read_hie (struct ghw_handler *h)
h->nbr_sigs++;
h->skip_sigs = NULL;
h->flag_full_names = 0;
h->sigs_no_null = 0;
h->sigs = (struct ghw_sig *) malloc (h->nbr_sigs * sizeof (struct ghw_sig));
memset (h->sigs, 0, h->nbr_sigs * sizeof (struct ghw_sig));

Expand Down Expand Up @@ -1212,10 +1213,20 @@ ghw_read_hie (struct ghw_handler *h)
}
}

/* Allocate values. */
/* Allocate values. Store indication if we have NULL-type signals with index
i > 0. Index i=0 */
int sigs_no_null = 1;
for (i = 0; i < h->nbr_sigs; i++)
if (h->sigs[i].type != NULL)
h->sigs[i].val = (union ghw_val *) malloc (sizeof (union ghw_val));
else if (i > 0)
{
printf ("Warning: ghw_read_hie: NULL type signal %d.", i);
printf ("Loading this file may take a long time.\n");
sigs_no_null = 0;
}

h->sigs_no_null = sigs_no_null;
return 0;
}

Expand Down Expand Up @@ -1505,8 +1516,9 @@ ghw_read_cycle_start (struct ghw_handler *h)
int
ghw_read_cycle_cont (struct ghw_handler *h, int *list)
{
int i;
uint32_t i;
int *list_p;
int ret = -1;

i = 0;
list_p = list;
Expand All @@ -1524,11 +1536,25 @@ ghw_read_cycle_cont (struct ghw_handler *h, int *list)
}

/* Find next signal. */
while (d > 0)
if (h->sigs_no_null)
{
i++;
if (h->sigs[i].type != NULL)
d--;
/* Fast version. */
i = i + d;
if (i >= h->nbr_sigs)
goto err;
}
else
{
/* Slow version: Linear search through all signals. Find d-th
element with non-NULL type. Note: Type of sigs[0] is ignored. */
while (d > 0)
{
i++;
if (i >= h->nbr_sigs)
goto err;
if (h->sigs[i].type != NULL)
d--;
}
}

if (ghw_read_signal_value (h, &h->sigs[i]) < 0)
Expand All @@ -1539,7 +1565,11 @@ ghw_read_cycle_cont (struct ghw_handler *h, int *list)

if (list_p)
*list_p = 0;
return 0;

err:
if (ret != 0)
fprintf(stderr, "Error: ghw_read_cycle_cont: Invalid entry in GHW file.\n");
return ret;
}

int
Expand Down
2 changes: 2 additions & 0 deletions ghw/libghw.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ struct ghw_handler
char *skip_sigs;
int flag_full_names;
struct ghw_sig *sigs;
/* 1: sigs does not contain any signals with type = NULL and index > 0 */
int sigs_no_null;

/* Hierarchy. */
struct ghw_hie *hie;
Expand Down

0 comments on commit 1728760

Please sign in to comment.