Skip to content

Commit

Permalink
Cache the previous filename's entry.
Browse files Browse the repository at this point in the history
Doing strcmp is a lot faster than hashing and going through the table. And don't forget to reset the cache entry when coverage is disabled!
  • Loading branch information
eloraburns committed Jul 1, 2011
1 parent cd16e8d commit 8ca255f
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions xdebug_code_coverage.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_bw_xor,"^=",0)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_dim,"=",1)
XDEBUG_OPCODE_OVERRIDE_ASSIGN(assign_obj,"=",1)

/* File hash entry cache; faster than doing a hash lookup most of the time */
static char* previous_filename = "";
static void* previous_file = NULL;

void xdebug_count_line(char *filename, int lineno, int executable, int deadcode TSRMLS_DC)
{
xdebug_coverage_file *file;
Expand All @@ -280,15 +284,21 @@ void xdebug_count_line(char *filename, int lineno, int executable, int deadcode

sline = xdebug_sprintf("%d", lineno);

/* Check if the file already exists in the hash */
if (!xdebug_hash_find(XG(code_coverage), filename, strlen(filename), (void *) &file)) {
/* The file does not exist, so we add it to the hash, and
* add a line element to the file */
file = xdmalloc(sizeof(xdebug_coverage_file));
file->name = xdstrdup(filename);
file->lines = xdebug_hash_alloc(128, xdebug_coverage_line_dtor);
if (strcmp(previous_filename, filename) == 0) {
file = previous_file;
} else {
/* Check if the file already exists in the hash */
if (!xdebug_hash_find(XG(code_coverage), filename, strlen(filename), (void *) &file)) {
/* The file does not exist, so we add it to the hash, and
* add a line element to the file */
file = xdmalloc(sizeof(xdebug_coverage_file));
file->name = xdstrdup(filename);
file->lines = xdebug_hash_alloc(128, xdebug_coverage_line_dtor);

xdebug_hash_add(XG(code_coverage), filename, strlen(filename), file);
xdebug_hash_add(XG(code_coverage), filename, strlen(filename), file);
}
previous_filename = filename;
previous_file = file;
}

/* Check if the line already exists in the hash */
Expand Down Expand Up @@ -581,6 +591,8 @@ PHP_FUNCTION(xdebug_stop_code_coverage)
}
if (XG(do_code_coverage)) {
if (cleanup) {
previous_filename = "";
previous_file = NULL;
xdebug_hash_destroy(XG(code_coverage));
XG(code_coverage) = xdebug_hash_alloc(32, xdebug_coverage_file_dtor);
}
Expand Down

0 comments on commit 8ca255f

Please sign in to comment.