From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.2 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by inbox.vuxu.org (OpenSMTPD) with SMTP id 9beadece for ; Tue, 3 Mar 2020 21:34:06 +0000 (UTC) Received: (qmail 31893 invoked by uid 550); 3 Mar 2020 21:34:04 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 31863 invoked from network); 3 Mar 2020 21:34:04 -0000 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=XqifTQGeM+ZgfZj7d4f+kbW7oDmYnTaJ4zkmEj5oeqU=; b=MDvbaPQ2YdD6AvRSur3cfeN4wRzktQUbke9UKQz5DD1W4OmBqcvB9aecdAUWvROGoP wdtdn/RCJuO2CFV1D64GT770FYSeM1s5WKbNEhdZzIZ1ZOwVScuoBIMBp0vNYty/PPBv tT0Un+WqX0HWUqu4JbxuXbKFn8BVu97xwCoSs5k9vLzWJs038DRLXPqe75VUa90SkZiB Oc+7k5T/TqaLP5lGsVxjHxVBr3YL8pLGTOkz/viNLddKJ8E3EqihatnucnS2XbgqlVHY cjwut0TEOZozqZWSU9v5sHqW22FIUVIkDp78V5c7Zok2OHm9tGwkKcKc5w88nm4xHu3x 4lEQ== X-Gm-Message-State: ANhLgQ1uf5ewJRRG1jsUid/mc6FqVP0mcbSM7ma3LW2NokgaMjBhK+sr c8bQVnLWcb0RvOHOhi8vn2jumjLL6s0c3Q== X-Google-Smtp-Source: ADFU+vucxQ2x6sGBwRNQZN9tcHQROwr8b8CecDGzrTzsKFwtKkk9JBGROvt3HqLbuRNrcUpIc+XVQA== X-Received: by 2002:a2e:8490:: with SMTP id b16mr3631138ljh.282.1583271232385; Tue, 03 Mar 2020 13:33:52 -0800 (PST) From: =?UTF-8?q?Timo=20Ter=C3=A4s?= To: musl@lists.openwall.com Cc: =?UTF-8?q?Timo=20Ter=C3=A4s?= Date: Tue, 3 Mar 2020 23:33:48 +0200 Message-Id: <20200303213348.10326-1-timo.teras@iki.fi> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [musl] [PATCH] improve strerror speed change the current O(n) lookup to O(1) based on the machinery described in "How To Write Shared Libraries" (Appendix B). --- src/errno/__strerror.h | 13 ++++++------- src/errno/strerror.c | 41 ++++++++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/errno/__strerror.h b/src/errno/__strerror.h index 2f04d400..ec6044e6 100644 --- a/src/errno/__strerror.h +++ b/src/errno/__strerror.h @@ -1,8 +1,9 @@ -/* This file is sorted such that 'errors' which represent exceptional - * conditions under which a correct program may fail come first, followed - * by messages that indicate an incorrect program or system failure. The - * macro E() along with double-inclusion is used to ensure that ordering - * of the strings remains synchronized. */ +/* The first '0' mapping will be used for error codes that + * are not explicitly mentioned here. + * This file is included multiple times to generate struct + * populate it's content and create a fast lookup index to it. */ + +E(0, "No error information") E(EILSEQ, "Illegal byte sequence") E(EDOM, "Domain error") @@ -101,5 +102,3 @@ E(EDQUOT, "Quota exceeded") E(ENOMEDIUM, "No medium found") E(EMEDIUMTYPE, "Wrong medium type") E(EMULTIHOP, "Multihop attempted") - -E(0, "No error information") diff --git a/src/errno/strerror.c b/src/errno/strerror.c index e3ed771a..54a9191e 100644 --- a/src/errno/strerror.c +++ b/src/errno/strerror.c @@ -1,30 +1,41 @@ #include +#include #include #include "locale_impl.h" -#define E(a,b) ((unsigned char)a), -static const unsigned char errid[] = { +/* mips has one error code outside of the 8-bit range due to a + * historical typo, so we just remap it. */ +#if EDQUOT==1133 +#define EDQUOT_ORIG 1133 +#undef EDQUOT +#define EDQUOT 109 +#endif + +static const struct errmsgstr_t { +#define E(n, s) char str##n[sizeof(s)]; +#include "__strerror.h" +#undef E +} errmsgstr = { +#define E(n, s) s, #include "__strerror.h" +#undef E }; -#undef E -#define E(a,b) b "\0" -static const char errmsg[] = +static const unsigned short errmsgidx[] = { +#define E(n, s) [n] = offsetof(struct errmsgstr_t, str##n), #include "__strerror.h" -; +#undef E +}; char *__strerror_l(int e, locale_t loc) { const char *s; - int i; - /* mips has one error code outside of the 8-bit range due to a - * historical typo, so we just remap it. */ - if (EDQUOT==1133) { - if (e==109) e=-1; - else if (e==EDQUOT) e=109; - } - for (i=0; errid[i] && errid[i] != e; i++); - for (s=errmsg; i; s++, i--) for (; *s; s++); +#ifdef EDQUOT_ORIG + if (e==EDQUOT) e=0; + else if (e==EDQUOT_ORIG) e=EDQUOT; +#endif + if (e < 0 || e >= sizeof(errmsgidx)/sizeof(errmsgidx[0])) e = 0; + s = (char *)&errmsgstr + errmsgidx[e]; return (char *)LCTRANS(s, LC_MESSAGES, loc); } -- 2.25.1