From 98406dd764137879ecfe73cc7618dfdbc7620baa Mon Sep 17 00:00:00 2001 From: Valentyn Bezshapkin <61702053+valentynbez@users.noreply.github.com> Date: Tue, 31 Oct 2023 07:18:49 +0100 Subject: [PATCH] Speed-up FASTA parsing in kseq.h with memchr (#756) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Valentyn Bezshapkin Co-authored-by: Fabian Klötzl --- lib/ksw2/kseq.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/ksw2/kseq.h b/lib/ksw2/kseq.h index 759d06add..749bb46f9 100644 --- a/lib/ksw2/kseq.h +++ b/lib/ksw2/kseq.h @@ -113,9 +113,10 @@ typedef struct __kstring_t { if (ks->end == -1) { ks->is_eof = 1; return -3; } \ } else break; \ } \ - if (delimiter == KS_SEP_LINE) { \ - for (i = ks->begin; i < ks->end; ++i) \ - if (ks->buf[i] == '\n') { ks->newline+=(append == 1); break; } \ + if (delimiter == KS_SEP_LINE) { \ + unsigned char *sep = (unsigned char*)memchr(ks->buf + ks->begin, '\n', ks->end - ks->begin); \ + i = sep != NULL ? sep - (unsigned char*)ks->buf : ks->end; \ + ks->newline += (sep != NULL && append == 1); \ } else if (delimiter > KS_SEP_MAX) { \ for (i = ks->begin; i < ks->end; ++i) \ if (ks->buf[i] == delimiter) break; \