Skip to content

Commit d7e7f2c

Browse files
committed
Modifications following review
1 parent be5a21d commit d7e7f2c

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

htslib/vcf.h

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,7 @@ static inline int bcf_enc_size(kstring_t *s, int size, int type)
15321532
return 0;
15331533
}
15341534

1535-
if (ks_resize(s, s->l + 5) < 0)
1535+
if (ks_resize(s, s->l + 6) < 0)
15361536
return -1;
15371537
uint8_t *p = (uint8_t *)s->s + s->l;
15381538
*p++ = 15<<4|type;
@@ -1555,6 +1555,13 @@ static inline int bcf_enc_size(kstring_t *s, int size, int type)
15551555
return 0;
15561556
}
15571557

1558+
static inline int bcf_enc_inttype(long x)
1559+
{
1560+
if (x <= BCF_MAX_BT_INT8 && x >= BCF_MIN_BT_INT8) return BCF_BT_INT8;
1561+
if (x <= BCF_MAX_BT_INT16 && x >= BCF_MIN_BT_INT16) return BCF_BT_INT16;
1562+
return BCF_BT_INT32;
1563+
}
1564+
15581565
// INTERNAL - not to be assumed to be a part of public API
15591566
// As per bcf_enc_size but kstring is already guaranteed to be big enough
15601567
// and with size == 1.
@@ -1565,35 +1572,36 @@ static inline void bcf_enc_size1_(kstring_t *s, int type)
15651572
s->l++;
15661573
}
15671574

1568-
static inline int bcf_enc_inttype(long x)
1569-
{
1570-
if (x <= BCF_MAX_BT_INT8 && x >= BCF_MIN_BT_INT8) return BCF_BT_INT8;
1571-
if (x <= BCF_MAX_BT_INT16 && x >= BCF_MIN_BT_INT16) return BCF_BT_INT16;
1572-
return BCF_BT_INT32;
1573-
}
1574-
15751575
static inline int bcf_enc_int1(kstring_t *s, int32_t x)
15761576
{
15771577
if (ks_resize(s, s->l + 5) < 0)
15781578
return -1;
1579-
uint8_t *p = (uint8_t *)s->s + s->l + 1/*1 for size*/;
1579+
uint8_t *p = (uint8_t *)s->s + s->l;
15801580

15811581
if (x == bcf_int32_vector_end) {
1582-
bcf_enc_size1_(s, BCF_BT_INT8);
1583-
*p = bcf_int8_vector_end; s->l++;
1582+
// An inline implementation of bcf_enc_size with size==1 and
1583+
// memory allocation already accounted for.
1584+
*p = (1<<4) | BCF_BT_INT8;
1585+
p[1] = bcf_int8_vector_end;
1586+
s->l+=2;
15841587
} else if (x == bcf_int32_missing) {
1585-
bcf_enc_size1_(s, BCF_BT_INT8);
1586-
*p = bcf_int8_missing; s->l++;
1588+
*p = (1<<4) | BCF_BT_INT8;
1589+
p[1] = bcf_int8_missing;
1590+
s->l+=2;
15871591
} else if (x <= BCF_MAX_BT_INT8 && x >= BCF_MIN_BT_INT8) {
1588-
bcf_enc_size1_(s, BCF_BT_INT8);
1589-
*p = x; s->l++;
1592+
*p = (1<<4) | BCF_BT_INT8;
1593+
p[1] = x;
1594+
s->l+=2;
15901595
} else if (x <= BCF_MAX_BT_INT16 && x >= BCF_MIN_BT_INT16) {
1591-
bcf_enc_size1_(s, BCF_BT_INT16);
1592-
i16_to_le(x, p); s->l+=2;
1596+
*p = (1<<4) | BCF_BT_INT16;
1597+
i16_to_le(x, p+1);
1598+
s->l+=3;
15931599
} else {
1594-
bcf_enc_size1_(s, BCF_BT_INT32);
1595-
i32_to_le(x, p); s->l+=4;
1600+
*p = (1<<4) | BCF_BT_INT32;
1601+
i32_to_le(x, p+1);
1602+
s->l+=5;
15961603
}
1604+
15971605
return 0;
15981606
}
15991607

vcf.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3512,6 +3512,16 @@ int vcf_parse(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v)
35123512
if (ks_resize(s, s->l+4) < 0)
35133513
return -1;
35143514

3515+
// Force our memory to be initialised so we avoid the technicality of
3516+
// undefined behaviour in using a 4-byte memcmp. (The reality is this
3517+
// almost certainly is never detected by the compiler so has no impact,
3518+
// but equally so this code has minimal (often beneficial) impact on
3519+
// performance too.)
3520+
s->s[s->l+0] = 0;
3521+
s->s[s->l+1] = 0;
3522+
s->s[s->l+2] = 0;
3523+
s->s[s->l+3] = 0;
3524+
35153525
bcf_clear1(v);
35163526
str = &v->shared;
35173527
memset(&aux, 0, sizeof(ks_tokaux_t));

0 commit comments

Comments
 (0)