diff --git a/bench.py b/bench.py index df804ea..6e85a46 100644 --- a/bench.py +++ b/bench.py @@ -27,6 +27,7 @@ # name -> (function name, keyword arguments, input preparation) OPERATIONS = { "b64encode": ("b64encode", {}, lambda raw: raw), + "b64encode(altchars)": ("b64encode", {"altchars": b"*!"}, lambda raw: raw), "b64decode": ("b64decode", {}, base64.b64encode), "b64decode(validate=True)": ("b64decode", {"validate": True}, base64.b64encode), "urlsafe_b64encode": ("urlsafe_b64encode", {}, lambda raw: raw), diff --git a/src/fastpybase64.cpp b/src/fastpybase64.cpp index 0c85540..c2d9ceb 100644 --- a/src/fastpybase64.cpp +++ b/src/fastpybase64.cpp @@ -215,12 +215,12 @@ PyObject *encode_with_altchars(const InputView &input, const char altchars[2]) { } char *out = PyBytes_AS_STRING(result); Py_ssize_t length = PyBytes_GET_SIZE(result); + const char plus = altchars[0]; + const char slash = altchars[1]; + // Store every byte so the compiler can vectorize the substitution. for (Py_ssize_t i = 0; i < length; i++) { - if (out[i] == '+') { - out[i] = altchars[0]; - } else if (out[i] == '/') { - out[i] = altchars[1]; - } + const char c = out[i]; + out[i] = c == '+' ? plus : (c == '/' ? slash : c); } return result; }