From 74744a9d5b9f4c6b5432dc1d3e8b6b0383887b48 Mon Sep 17 00:00:00 2001 From: Rasmus Munk Larsen Date: Wed, 13 Dec 2023 12:42:50 -0800 Subject: [PATCH 1/4] Speed up initLUT in flute.cpp by adding a specialized parser for decimal integers to replace sscanf(..., "%d%n", ...). Signed-off-by: Rasmus Munk Larsen --- src/stt/src/flt/flute.cpp | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/stt/src/flt/flute.cpp b/src/stt/src/flt/flute.cpp index 304855a0a78..9d65f1e9174 100644 --- a/src/stt/src/flt/flute.cpp +++ b/src/stt/src/flt/flute.cpp @@ -281,6 +281,27 @@ static unsigned char charNum(unsigned char c) return 0; } +// Decodes a decimal integer from the string s. +// Returns the decoded value in *value and returns +// the number of characters read. +inline const char* readDecimalInt(const char* s, int &value) +{ + value = 0; + bool negative = (*s == '-'); + if (negative || *s == '+') { + ++s; + } + constexpr int zero_code = int('0'); + while (*s >= '0' && *s <= '9') { + value = 10 * value + (int(*s) - zero_code); + ++s; + } + if (negative) { + value = -value; + } + return s; +} + // Init LUTs from base64 encoded string variables. static void initLUT(int to_d, LUT_TYPE LUT, NUMSOLN_TYPE numsoln) { @@ -293,19 +314,21 @@ static void initLUT(int to_d, LUT_TYPE LUT, NUMSOLN_TYPE numsoln) #endif for (int d = 4; d <= to_d; d++) { - int char_cnt; - sscanf(pwv, "d=%d%n", &d, &char_cnt); - pwv += char_cnt + 1; + if (pwv[0] == 'd' && pwv[1] == '=') { + pwv = readDecimalInt(pwv + 2, d); + } + ++pwv; #if FLUTE_ROUTING == 1 - sscanf(prt, "d=%d%n", &d, &char_cnt); - prt += char_cnt + 1; + if (pwv[0] == 'd' && pwv[1] == '=') { + pwv = readDecimalInt(pwv + 2, d); + } + ++pwv; #endif for (int k = 0; k < numgrp[d]; k++) { int ns = charNum(*pwv++); if (ns == 0) { // same as some previous group int kk; - sscanf(pwv, "%d%n", &kk, &char_cnt); - pwv += char_cnt + 1; + pwv = readDecimalInt(pwv, kk) + 1; numsoln[d][k] = numsoln[d][kk]; LUT[d][k] = LUT[d][kk]; } else { From e03f124924a667b44f4237ae474bb7f7469095e8 Mon Sep 17 00:00:00 2001 From: Rasmus Munk Larsen Date: Wed, 13 Dec 2023 15:57:06 -0800 Subject: [PATCH 2/4] clang-format. Signed-off-by: Rasmus Munk Larsen --- src/stt/src/flt/flute.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stt/src/flt/flute.cpp b/src/stt/src/flt/flute.cpp index 9d65f1e9174..b13efb7b8de 100644 --- a/src/stt/src/flt/flute.cpp +++ b/src/stt/src/flt/flute.cpp @@ -284,7 +284,7 @@ static unsigned char charNum(unsigned char c) // Decodes a decimal integer from the string s. // Returns the decoded value in *value and returns // the number of characters read. -inline const char* readDecimalInt(const char* s, int &value) +inline const char* readDecimalInt(const char* s, int& value) { value = 0; bool negative = (*s == '-'); From 58c99eb87cddef70b579f6c5e9425e357e1fbbab Mon Sep 17 00:00:00 2001 From: Rasmus Munk Larsen Date: Thu, 14 Dec 2023 15:25:37 -0800 Subject: [PATCH 3/4] Routing and regular path use different pointers. Signed-off-by: Rasmus Munk Larsen --- src/stt/src/flt/flute.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/stt/src/flt/flute.cpp b/src/stt/src/flt/flute.cpp index b13efb7b8de..759980fa213 100644 --- a/src/stt/src/flt/flute.cpp +++ b/src/stt/src/flt/flute.cpp @@ -319,10 +319,10 @@ static void initLUT(int to_d, LUT_TYPE LUT, NUMSOLN_TYPE numsoln) } ++pwv; #if FLUTE_ROUTING == 1 - if (pwv[0] == 'd' && pwv[1] == '=') { - pwv = readDecimalInt(pwv + 2, d); + if (prt[0] == 'd' && prt[1] == '=') { + prt = readDecimalInt(prt + 2, d); } - ++pwv; + ++prt; #endif for (int k = 0; k < numgrp[d]; k++) { int ns = charNum(*pwv++); From 7f0134291710316b1c3978249a650c95b7058965 Mon Sep 17 00:00:00 2001 From: Rasmus Munk Larsen Date: Thu, 14 Dec 2023 16:21:49 -0800 Subject: [PATCH 4/4] Delete misleading comment. Signed-off-by: Rasmus Munk Larsen --- src/stt/src/flt/flute.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/stt/src/flt/flute.cpp b/src/stt/src/flt/flute.cpp index 759980fa213..6843a3eefec 100644 --- a/src/stt/src/flt/flute.cpp +++ b/src/stt/src/flt/flute.cpp @@ -281,9 +281,6 @@ static unsigned char charNum(unsigned char c) return 0; } -// Decodes a decimal integer from the string s. -// Returns the decoded value in *value and returns -// the number of characters read. inline const char* readDecimalInt(const char* s, int& value) { value = 0;