From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpng3.m.smailru.net (smtpng3.m.smailru.net [94.100.177.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 165F04765E3 for ; Fri, 25 Dec 2020 13:41:14 +0300 (MSK) From: Sergey Nikiforov Date: Fri, 25 Dec 2020 13:41:05 +0300 Message-Id: In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH v5 2/2] base64: improve decoder performance List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tarantool-patches@dev.tarantool.org Cc: Vladislav Shpilevoy Unnecessary checks were removed from internal loops. Benchmark shows that performance is now ~1.19 times higher (release build, Intel Core I7-9700K, only one thread). --- third_party/base64.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/third_party/base64.c b/third_party/base64.c index 3350a98ff..2b2b61ba4 100644 --- a/third_party/base64.c +++ b/third_party/base64.c @@ -257,10 +257,10 @@ base64_decode_block(const char *in_base64, int in_len, { case step_a: do { - if (in_pos == in_end || out_pos >= out_end) + if (in_pos >= in_end) { state->step = step_a; - state->result = curr_byte; + /* curr_byte is useless now. */ return out_pos - out_bin; } fragment = base64_decode_value(*in_pos++); @@ -268,7 +268,7 @@ base64_decode_block(const char *in_base64, int in_len, curr_byte = (fragment & 0x03f) << 2; case step_b: do { - if (in_pos == in_end || out_pos >= out_end) + if (in_pos >= in_end) { state->step = step_b; state->result = curr_byte; @@ -276,14 +276,19 @@ base64_decode_block(const char *in_base64, int in_len, } fragment = base64_decode_value(*in_pos++); } while (fragment < 0); + if (out_pos >= out_end) + { + /* We are losing some data. */ + state->step = step_b; + state->result = curr_byte; + return out_pos - out_bin; + } curr_byte |= (fragment & 0x030) >> 4; *out_pos++ = curr_byte; curr_byte = (fragment & 0x00f) << 4; - if (out_pos < out_end) - *out_pos = curr_byte; case step_c: do { - if (in_pos == in_end || out_pos >= out_end) + if (in_pos >= in_end) { state->step = step_c; state->result = curr_byte; @@ -291,14 +296,19 @@ base64_decode_block(const char *in_base64, int in_len, } fragment = base64_decode_value(*in_pos++); } while (fragment < 0); + if (out_pos >= out_end) + { + /* We are losing some data. */ + state->step = step_c; + state->result = curr_byte; + return out_pos - out_bin; + } curr_byte |= (fragment & 0x03c) >> 2; *out_pos++ = curr_byte; curr_byte = (fragment & 0x003) << 6; - if (out_pos < out_end) - *out_pos = curr_byte; case step_d: do { - if (in_pos == in_end || out_pos >= out_end) + if (in_pos >= in_end) { state->step = step_d; state->result = curr_byte; @@ -306,6 +316,13 @@ base64_decode_block(const char *in_base64, int in_len, } fragment = base64_decode_value(*in_pos++); } while (fragment < 0); + if (out_pos >= out_end) + { + /* We are losing some data. */ + state->step = step_d; + state->result = curr_byte; + return out_pos - out_bin; + } curr_byte |= (fragment & 0x03f); *out_pos++ = curr_byte; } -- 2.25.1