Skip to content

Commit f3d401c

Browse files
jkbonfieldwhitwham
authored andcommitted
Speed up faidx.
- bgzf_getc is slow as it's a heavy function and not inlined. Most of the time though it's just an array fetch, so inline the basic form and revert to the function call for the complex form. - isgraph and all other ctype functions are slow. We assume ASCII and just do a naive implementation. The speed benefits are (seconds): Old New Index GRCh38 13.4 8.4 Query chr1 1.7 0.9 Given a significant speed change for a small localised modification it seems worth while having.
1 parent b8145e6 commit f3d401c

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

faidx.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,29 @@ DEALINGS IN THE SOFTWARE. */
4343
#include "htslib/kstring.h"
4444
#include "hts_internal.h"
4545

46+
// Faster isgraph; assumes ASCII
47+
static inline int isgraph_(unsigned char c) {
48+
return c > ' ' && c <= '~';
49+
}
50+
51+
#ifdef isgraph
52+
# undef isgraph
53+
#endif
54+
#define isgraph isgraph_
55+
56+
// An optimised bgzf_getc.
57+
// We could consider moving this to bgzf.h, but our own code uses it here only.
58+
static inline int bgzf_getc_(BGZF *fp) {
59+
if (fp->block_offset+1 < fp->block_length) {
60+
int c = ((unsigned char*)fp->uncompressed_block)[fp->block_offset++];
61+
fp->uncompressed_address++;
62+
return c;
63+
}
64+
65+
return bgzf_getc(fp);
66+
}
67+
#define bgzf_getc bgzf_getc_
68+
4669
typedef struct {
4770
int id; // faidx_t->name[id] is for this struct.
4871
uint32_t line_len, line_blen;
@@ -727,7 +750,8 @@ static char *fai_retrieve(const faidx_t *fai, const faidx1_t *val,
727750
return NULL;
728751
}
729752

730-
while ( l < end - beg && (c=bgzf_getc(fai->bgzf))>=0 )
753+
BGZF *fp = fai->bgzf;
754+
while ( l < end - beg && (c=bgzf_getc(fp))>=0 )
731755
if (isgraph(c)) s[l++] = c;
732756
if (c < 0) {
733757
hts_log_error("Failed to retrieve block: %s",

0 commit comments

Comments
 (0)