Skip to content

Commit 4fe9279

Browse files
committed
2x speedup for twobitBases, though I expect another 10x speedup is possible
1 parent a0b96d2 commit 4fe9279

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

2bit.c

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -308,40 +308,40 @@ void increment(char base, uint32_t *A, uint32_t *C, uint32_t *T, uint32_t *G) {
308308

309309
void *twobitBasesWorker(TwoBit *tb, uint32_t tid, uint32_t start, uint32_t end, int fraction) {
310310
void *out;
311-
uint32_t sz = end - start, pos = 0;
312-
uint32_t A = 0, C = 0, T = 0, G = 0, len = end - start;
313-
uint32_t maskIdx = -1, maskStart = -1, maskEnd = -1;
314-
uint32_t blockStart, offset;
315-
uint8_t byte, base;
316-
int rv = 0;
311+
uint32_t A = 0, C = 0, T = 0, G = 0, len = end - start, i = 0;
312+
uint32_t blockStart, blockEnd, offset;
313+
char *s = NULL;
317314

318315
if(fraction) {
319316
out = malloc(4 * sizeof(double));
320317
} else {
321318
out = malloc(4 * sizeof(uint32_t));
322319
}
323320
if(!out) return NULL;
324-
325-
getMask(tb, tid, start, end, &maskIdx, &maskStart, &maskEnd);
326-
327-
//There are 4 bases/byte
328-
blockStart = start/4;
329-
offset = start % 4;
330-
331-
if(twobitSeek(tb, tb->idx->offset[tid] + blockStart) != 0) goto error;
332-
while(pos < sz) {
333-
//Iterate over the 4 (or less) bases in each byte
334-
if(twobitRead(&byte, 1, 1, tb) != 1) goto error;
335-
for(; offset<4; offset++) {
336-
rv = cycle(tb, tid, start, end, &pos, &maskIdx, &maskStart, &maskEnd, &blockStart, &offset);
337-
if(rv == -1) goto error;
338-
if(rv == 1) break;
339-
base = byte2base(byte, offset);
340-
increment(base, &A, &C, &T, &G);
341-
if(++pos >= sz) break;
321+
s = constructSequence(tb, tid, start, end);
322+
if(!s) goto error;
323+
324+
for(i=0; i<len; i++) {
325+
switch(s[i]) {
326+
case 'A':
327+
case 'a':
328+
A++;
329+
break;
330+
case 'C':
331+
case 'c':
332+
C++;
333+
break;
334+
case 'G':
335+
case 'g':
336+
G++;
337+
break;
338+
case 'T':
339+
case 't':
340+
T++;
341+
break;
342342
}
343-
if(rv != 1) offset = 0;
344343
}
344+
free(s);
345345

346346
if(fraction) {
347347
((double*) out)[0] = ((double) A)/((double) len);
@@ -359,6 +359,7 @@ void *twobitBasesWorker(TwoBit *tb, uint32_t tid, uint32_t start, uint32_t end,
359359

360360
error:
361361
free(out);
362+
if(s) free(s);
362363
return NULL;
363364
}
364365

0 commit comments

Comments
 (0)