From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 4F299D65451; Fri, 13 Sep 2024 17:52:31 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4F299D65451 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1726239151; bh=8vU9CzvS+9FtIiim+2X+aksrERIYVbKamTBX/T6QQSQ=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=k28GwbTngeLMC1zWoGRJd2ZJ4LM09RpKAlFWYnkb3I94eREO8ph6LJLTBWBOQH+zh XAD4J3BRQYns2x7z0ZRepIx09ooQOBe/Xrh2COlCBjqZodwJYiXDAxZVLnWiCeFVXj WuIO0sfv4nB1CiZ4XRSW+KKFV16tvePV0Kpsc9WU= Received: from smtp58.i.mail.ru (smtp58.i.mail.ru [95.163.41.96]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 77AE4D65441 for ; Fri, 13 Sep 2024 17:52:30 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 77AE4D65441 Received: by smtp58.i.mail.ru with esmtpa (envelope-from ) id 1sp7eu-00000005nI0-3IvC; Fri, 13 Sep 2024 17:52:29 +0300 Content-Type: multipart/alternative; boundary="------------LOV3uMsvS3JDYouJe6y33JyK" Message-ID: Date: Fri, 13 Sep 2024 17:52:28 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: mandesero@gmail.com, tarantool-patches@dev.tarantool.org, skaplun@tarantool.org, m.kokryashkin@tarantool.org References: <20240626122735.165672-1-mandesero@gmail.com> <20240626122735.165672-2-mandesero@gmail.com> In-Reply-To: <20240626122735.165672-2-mandesero@gmail.com> X-Mailru-Src: smtp X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD9149934E261B3C850461CD6438549F4C8BA21A41957959D4E182A05F53808504052F406B8DAEFB0D7D27678DDAA806314BAD156EF418E1B58F843B126B34D6908F65AAC9D8C52D1E3 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7F2919D563845004AEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006375E347E1311495C1B8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D896C7DC90BA2665BA91907D9AD9FA30C72E9211F8395F1EF1CC7F00164DA146DAFE8445B8C89999728AA50765F7900637F6B57BC7E64490618DEB871D839B7333395957E7521B51C2DFABB839C843B9C08941B15DA834481F8AA50765F7900637BA939FD1B3BAB99B389733CBF5DBD5E9B5C8C57E37DE458BD9DD9810294C998ED8FC6C240DEA76428AA50765F79006373D42D4F566CB30C1D81D268191BDAD3DBD4B6F7A4D31EC0BE2F48590F00D11D6D81D268191BDAD3D78DA827A17800CE7B41913284266F17EEC76A7562686271ED91E3A1F190DE8FD2E808ACE2090B5E14AD6D5ED66289B5259CC434672EE63711DD303D21008E298D5E8D9A59859A8B6B372FE9A2E580EFC725E5C173C3A84C363D77626233CE5AB35872C767BF85DA2F004C90652538430E4A6367B16DE6309 X-C1DE0DAB: 0D63561A33F958A5EC648BCE06CE498B5002B1117B3ED69697A2649D875FA56630C8F815570A3530823CB91A9FED034534781492E4B8EEAD9CFA8CFAC159CE19BDAD6C7F3747799A X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF77DD89D51EBB7742D3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF488495B36C955C3C60F4FD9374DD707D0EBB6EA290FAC427384E046D7BFAABA5F93E7F6CC6F26229BEF149BEF2827C687E45C9F75CF7920ADD79BBAE8981E54C6C0E98C709A39C2AC226CC413062362A913E6812662D5F2AB9AF64DB4688768036DF5FE9C0001AF333F2C28C22F508233FCF178C6DD14203 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojmn7hiCWKTuimaylNqQaqCw== X-Mailru-Sender: 520A125C2F17F0B1E52FEF5D219D61405639014832156DECD0A854E95C66BF1D5B2F9A01FB8C5BF90152A3D17938EB451EB5A0BCEC6A560B3DDE9B364B0DF289BE2DA36745F2EEB5CEBA01FB949A1F1EEAB4BC95F72C04283CDA0F3B3F5B9367 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit 1/2] c: disable strcmp optimization in Valgrind build X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" This is a multi-part message in MIME format. --------------LOV3uMsvS3JDYouJe6y33JyK Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hi, Maxim, thanks for the patch! The patch below is quite similar to the patch in OpenResty [1]. Have you used it? 1. https://github.com/openresty/luajit2/commit/6315a752274f3a4db6827b64788173f40733204e On 26.06.2024 15:27, mandesero--- via Tarantool-patches wrote: > From: mandesero > > --- > src/lj_str.c | 43 +++++++++++++++++++++++++++++++++++++++++-- > 1 file changed, 41 insertions(+), 2 deletions(-) > > diff --git a/src/lj_str.c b/src/lj_str.c > index 321e8c4f..00cced03 100644 > --- a/src/lj_str.c > +++ b/src/lj_str.c > @@ -26,10 +26,29 @@ static LJ_AINLINE int str_fastcmp(const char *a, const char *b, MSize len) > > /* -- String helpers ------------------------------------------------------ */ > > +#if LUAJIT_USE_VALGRIND > +int lj_str_cmp_no_opt(const char *a, const char *b, MSize len) > +{ > + for (MSize i = 0; i < len; i++) > + if (a[i] != b[i]) > + return 1; > + return 0; > +} > +#endif > + > /* Ordered compare of strings. Assumes string data is 4-byte aligned. */ > int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b) > { > MSize i, n = a->len > b->len ? b->len : a->len; > +#if LUAJIT_USE_VALGRIND > + const uint8_t *sa = (const uint8_t *)strdata(a); > + const uint8_t *sb = (const uint8_t *)strdata(b); > + > + for (i = 0; i < n; i++) { > + if (sa[i] != sb[i]) > + return (uint8_t)sa[i] < (uint8_t)sb[i] ? -1 : 1; > + } > +#else > for (i = 0; i < n; i += 4) { > /* Note: innocuous access up to end of string + 3. */ > uint32_t va = *(const uint32_t *)(strdata(a)+i); > @@ -46,6 +65,7 @@ int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b) > return va < vb ? -1 : 1; > } > } > +#endif > return (int32_t)(a->len - b->len); > } > > @@ -53,6 +73,12 @@ int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b) > static LJ_AINLINE int str_fastcmp(const char *a, const char *b, MSize len) > { > MSize i = 0; > +#if LUAJIT_USE_VALGRIND > + for (i = 0; i < len; i++) { > + if (a[i] != b[i]) > + return (uint8_t)a[i] < (uint8_t)b[i] ? -1 : 1; > + } > +#else > lj_assertX(len > 0, "fast string compare with zero length"); > lj_assertX((((uintptr_t)a+len-1) & (LJ_PAGESIZE-1)) <= LJ_PAGESIZE-4, > "fast string compare crossing page boundary"); > @@ -68,6 +94,7 @@ static LJ_AINLINE int str_fastcmp(const char *a, const char *b, MSize len) > } > i += 4; > } while (i < len); > +#endif > return 0; > } > > @@ -83,7 +110,11 @@ const char *lj_str_find(const char *s, const char *p, MSize slen, MSize plen) > while (slen) { > const char *q = (const char *)memchr(s, c, slen); > if (!q) break; > +#if LUAJIT_USE_VALGRIND > + if (lj_str_cmp_no_opt(q+1, p, plen) == 0) return q; > +#else > if (memcmp(q+1, p, plen) == 0) return q; > +#endif > q++; slen -= (MSize)(q-s); s = q; > } > } > @@ -232,8 +263,13 @@ GCstr *lj_str_new(lua_State *L, const char *str, size_t lenx) > } else { /* Slow path: end of string is too close to a page boundary. */ > while (o != NULL) { > GCstr *sx = gco2str(o); > +#if LUAJIT_USE_VALGRIND > + if (sx->hash == h && sx->len == len && inc_collision_hard() && > + lj_str_cmp_no_opt(str, strdata(sx), len) == 0) { > +#else > if (sx->hash == h && sx->len == len && inc_collision_hard() && > memcmp(str, strdata(sx), len) == 0) { > +#endif > /* Resurrect if dead. Can only happen with fixstring() (keywords). */ > if (isdead(g, o)) flipwhite(o); > g->strhash_hit++; > @@ -277,7 +313,11 @@ GCstr *lj_str_new(lua_State *L, const char *str, size_t lenx) > } else { /* Slow path: end of string is too close to a page boundary. */ > while (o != NULL) { > GCstr *sx = gco2str(o); > +#if LUAJIT_USE_VALGRIND > + if (sx->hash == fh && sx->len == len && lj_str_cmp_no_opt(str, strdata(sx), len) == 0) { > +#else > if (sx->hash == fh && sx->len == len && memcmp(str, strdata(sx), len) == 0) { > +#endif > /* Resurrect if dead. Can only happen with fixstring() (keywords). */ > if (isdead(g, o)) flipwhite(o); > g->strhash_hit++; > @@ -323,5 +363,4 @@ void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s) > { > g->strnum--; > lj_mem_free(g, s, sizestring(s)); > -} > - > +} > \ No newline at end of file --------------LOV3uMsvS3JDYouJe6y33JyK Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit

Hi, Maxim,

thanks for the patch!

The patch below is quite similar to the patch in OpenResty [1].

Have you used it?

1. https://github.com/openresty/luajit2/commit/6315a752274f3a4db6827b64788173f40733204e

On 26.06.2024 15:27, mandesero--- via Tarantool-patches wrote:
From: mandesero <mandesero@gmail.com>

---
 src/lj_str.c | 43 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)

diff --git a/src/lj_str.c b/src/lj_str.c
index 321e8c4f..00cced03 100644
--- a/src/lj_str.c
+++ b/src/lj_str.c
@@ -26,10 +26,29 @@ static LJ_AINLINE int str_fastcmp(const char *a, const char *b, MSize len)
 
 /* -- String helpers ------------------------------------------------------ */
 
+#if LUAJIT_USE_VALGRIND
+int lj_str_cmp_no_opt(const char *a, const char *b, MSize len)
+{
+  for (MSize i = 0; i < len; i++)
+    if (a[i] != b[i])
+      return 1;
+  return 0;
+}
+#endif
+
 /* Ordered compare of strings. Assumes string data is 4-byte aligned. */
 int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b)
 {
   MSize i, n = a->len > b->len ? b->len : a->len;
+#if LUAJIT_USE_VALGRIND
+  const uint8_t *sa = (const uint8_t *)strdata(a);
+  const uint8_t *sb = (const uint8_t *)strdata(b);
+  
+  for (i = 0; i < n; i++) {
+    if (sa[i] != sb[i])
+      return (uint8_t)sa[i] < (uint8_t)sb[i] ? -1 : 1;
+  }
+#else
   for (i = 0; i < n; i += 4) {
     /* Note: innocuous access up to end of string + 3. */
     uint32_t va = *(const uint32_t *)(strdata(a)+i);
@@ -46,6 +65,7 @@ int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b)
       return va < vb ? -1 : 1;
     }
   }
+#endif
   return (int32_t)(a->len - b->len);
 }
 
@@ -53,6 +73,12 @@ int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b)
 static LJ_AINLINE int str_fastcmp(const char *a, const char *b, MSize len)
 {
   MSize i = 0;
+#if LUAJIT_USE_VALGRIND
+  for (i = 0; i < len; i++) {
+    if (a[i] != b[i])
+      return (uint8_t)a[i] < (uint8_t)b[i] ? -1 : 1;
+  }
+#else
   lj_assertX(len > 0, "fast string compare with zero length");
   lj_assertX((((uintptr_t)a+len-1) & (LJ_PAGESIZE-1)) <= LJ_PAGESIZE-4,
 	     "fast string compare crossing page boundary");
@@ -68,6 +94,7 @@ static LJ_AINLINE int str_fastcmp(const char *a, const char *b, MSize len)
     }
     i += 4;
   } while (i < len);
+#endif
   return 0;
 }
 
@@ -83,7 +110,11 @@ const char *lj_str_find(const char *s, const char *p, MSize slen, MSize plen)
       while (slen) {
 	const char *q = (const char *)memchr(s, c, slen);
 	if (!q) break;
+#if LUAJIT_USE_VALGRIND
+  if (lj_str_cmp_no_opt(q+1, p, plen) == 0) return q;
+#else
 	if (memcmp(q+1, p, plen) == 0) return q;
+#endif
 	q++; slen -= (MSize)(q-s); s = q;
       }
     }
@@ -232,8 +263,13 @@ GCstr *lj_str_new(lua_State *L, const char *str, size_t lenx)
   } else {  /* Slow path: end of string is too close to a page boundary. */
     while (o != NULL) {
       GCstr *sx = gco2str(o);
+#if LUAJIT_USE_VALGRIND
+      if (sx->hash == h && sx->len == len && inc_collision_hard() &&
+                      lj_str_cmp_no_opt(str, strdata(sx), len) == 0) {
+#else
       if (sx->hash == h && sx->len == len && inc_collision_hard() &&
                       memcmp(str, strdata(sx), len) == 0) {
+#endif
 	/* Resurrect if dead. Can only happen with fixstring() (keywords). */
 	if (isdead(g, o)) flipwhite(o);
 	g->strhash_hit++;
@@ -277,7 +313,11 @@ GCstr *lj_str_new(lua_State *L, const char *str, size_t lenx)
 	} else {  /* Slow path: end of string is too close to a page boundary. */
 	  while (o != NULL) {
 	    GCstr *sx = gco2str(o);
+#if LUAJIT_USE_VALGRIND
+	    if (sx->hash == fh && sx->len == len && lj_str_cmp_no_opt(str, strdata(sx), len) == 0) {
+#else
 	    if (sx->hash == fh && sx->len == len && memcmp(str, strdata(sx), len) == 0) {
+#endif
 	      /* Resurrect if dead. Can only happen with fixstring() (keywords). */
 	      if (isdead(g, o)) flipwhite(o);
 	      g->strhash_hit++;
@@ -323,5 +363,4 @@ void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s)
 {
   g->strnum--;
   lj_mem_free(g, s, sizestring(s));
-}
-
+}
\ No newline at end of file
--------------LOV3uMsvS3JDYouJe6y33JyK--