Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up the VCF parser #1644

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion bgzf.c
Original file line number Diff line number Diff line change
Expand Up @@ -2280,7 +2280,13 @@ int bgzf_getline(BGZF *fp, int delim, kstring_t *str)
if (fp->block_length == 0) { state = -1; break; }
}
unsigned char *buf = fp->uncompressed_block;
for (l = fp->block_offset; l < fp->block_length && buf[l] != delim; ++l);

// Equivalent to a naive byte by byte search from
// buf + block_offset to buf + block_length.
void *e = memchr(&buf[fp->block_offset], delim,
fp->block_length - fp->block_offset);
l = e ? (unsigned char *)e - buf : fp->block_length;

if (l < fp->block_length) state = 1;
l -= fp->block_offset;
if (ks_expand(str, l + 2) < 0) { state = -3; break; }
Expand Down
85 changes: 52 additions & 33 deletions htslib/vcf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1522,26 +1522,37 @@ static inline int bcf_format_gt(bcf_fmt_t *fmt, int isample, kstring_t *str)

static inline int bcf_enc_size(kstring_t *s, int size, int type)
{
uint32_t e = 0;
uint8_t x[4];
if (size >= 15) {
e |= kputc(15<<4|type, s) < 0;
if (size >= 128) {
if (size >= 32768) {
i32_to_le(size, x);
e |= kputc(1<<4|BCF_BT_INT32, s) < 0;
e |= kputsn((char*)&x, 4, s) < 0;
} else {
i16_to_le(size, x);
e |= kputc(1<<4|BCF_BT_INT16, s) < 0;
e |= kputsn((char*)&x, 2, s) < 0;
}
// Most common case is first
if (size < 15) {
if (ks_resize(s, s->l + 1) < 0)
return -1;
uint8_t *p = (uint8_t *)s->s + s->l;
*p++ = (size<<4) | type;
s->l++;
return 0;
}

if (ks_resize(s, s->l + 6) < 0)
return -1;
uint8_t *p = (uint8_t *)s->s + s->l;
*p++ = 15<<4|type;

if (size < 128) {
*p++ = 1<<4|BCF_BT_INT8;
*p++ = size;
s->l += 3;
} else {
if (size < 32768) {
*p++ = 1<<4|BCF_BT_INT16;
i16_to_le(size, p);
s->l += 4;
} else {
e |= kputc(1<<4|BCF_BT_INT8, s) < 0;
e |= kputc(size, s) < 0;
*p++ = 1<<4|BCF_BT_INT32;
i32_to_le(size, p);
s->l += 6;
}
} else e |= kputc(size<<4|type, s) < 0;
return e == 0 ? 0 : -1;
}
return 0;
}

static inline int bcf_enc_inttype(long x)
Expand All @@ -1553,27 +1564,35 @@ static inline int bcf_enc_inttype(long x)

static inline int bcf_enc_int1(kstring_t *s, int32_t x)
{
uint32_t e = 0;
uint8_t z[4];
if (ks_resize(s, s->l + 5) < 0)
return -1;
uint8_t *p = (uint8_t *)s->s + s->l;

if (x == bcf_int32_vector_end) {
e |= bcf_enc_size(s, 1, BCF_BT_INT8);
e |= kputc(bcf_int8_vector_end, s) < 0;
// An inline implementation of bcf_enc_size with size==1 and
// memory allocation already accounted for.
*p = (1<<4) | BCF_BT_INT8;
p[1] = bcf_int8_vector_end;
s->l+=2;
} else if (x == bcf_int32_missing) {
e |= bcf_enc_size(s, 1, BCF_BT_INT8);
e |= kputc(bcf_int8_missing, s) < 0;
*p = (1<<4) | BCF_BT_INT8;
p[1] = bcf_int8_missing;
s->l+=2;
} else if (x <= BCF_MAX_BT_INT8 && x >= BCF_MIN_BT_INT8) {
e |= bcf_enc_size(s, 1, BCF_BT_INT8);
e |= kputc(x, s) < 0;
*p = (1<<4) | BCF_BT_INT8;
p[1] = x;
s->l+=2;
} else if (x <= BCF_MAX_BT_INT16 && x >= BCF_MIN_BT_INT16) {
i16_to_le(x, z);
e |= bcf_enc_size(s, 1, BCF_BT_INT16);
e |= kputsn((char*)&z, 2, s) < 0;
*p = (1<<4) | BCF_BT_INT16;
i16_to_le(x, p+1);
s->l+=3;
} else {
i32_to_le(x, z);
e |= bcf_enc_size(s, 1, BCF_BT_INT32);
e |= kputsn((char*)&z, 4, s) < 0;
*p = (1<<4) | BCF_BT_INT32;
i32_to_le(x, p+1);
s->l+=5;
}
return e == 0 ? 0 : -1;

return 0;
}

/// Return the value of a single typed integer.
Expand Down
13 changes: 11 additions & 2 deletions kstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,17 @@ char *kstrtok(const char *str, const char *sep_in, ks_tokaux_t *aux)
for (p = start; *p; ++p)
if (aux->tab[*p>>6]>>(*p&0x3f)&1) break;
} else {
for (p = start; *p; ++p)
if (*p == aux->sep) break;
// Using strchr is fast for next token, but slower for
// last token due to extra pass from strlen. Overall
// on a VCF parse this func was 146% faster with // strchr.
// Equiv to:
// for (p = start; *p; ++p) if (*p == aux->sep) break;

// NB: We could use strchrnul() here from glibc if detected,
// which is ~40% faster again, but it's not so portable.
// i.e. p = (uint8_t *)strchrnul((char *)start, aux->sep);
uint8_t *p2 = (uint8_t *)strchr((char *)start, aux->sep);
p = p2 ? p2 : start + strlen((char *)start);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realise there's a faster alternative here too.

Drop the if (*p == 0) code below (so move to the earlier if-else cases) and use p2 == NULL instead. Eg:

uint8_t *p2 = (uint8_t *)strchr((char *)start, aux->sep);
if (p2) {
    aux->p = (const char *)p2;
} else {
    aux->p = (const char *)start + strlen((char *)start);
    aux->finished = 1;
}

This avoids a second conditional on *p. Isolated testing of kstrtok though shows it's only around 3-4% quicker so I can't be bothered to change it now, and that corresponds to a tiny amount in most VCF parsing.

Note the old kstrtok was about 18% slower, so the use of strchr has been significant. We probably ought to apply this to klib, but I see there hasn't been movement on the last PR I made so I suspect support is dead.

}
aux->p = (const char *) p; // end of token
if (*p == 0) aux->finished = 1; // no more tokens
Expand Down
Loading