From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#56347: Optimize/simplify STRING_SET_MULTIBYTE Date: Sat, 02 Jul 2022 12:12:06 -0400 Message-ID: References: <83pmioca3h.fsf@gnu.org> Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="30806"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) Cc: 56347@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Jul 02 18:13:20 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o7fkG-0007oK-Ab for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 02 Jul 2022 18:13:20 +0200 Original-Received: from localhost ([::1]:35380 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o7fkE-0006nn-Ga for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 02 Jul 2022 12:13:18 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:37178) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o7fjy-0006mV-D7 for bug-gnu-emacs@gnu.org; Sat, 02 Jul 2022 12:13:04 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:48783) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o7fjy-0000Xk-5N for bug-gnu-emacs@gnu.org; Sat, 02 Jul 2022 12:13:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1o7fjy-0001Am-10 for bug-gnu-emacs@gnu.org; Sat, 02 Jul 2022 12:13:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 02 Jul 2022 16:13:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 56347 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 56347-submit@debbugs.gnu.org id=B56347.16567783434459 (code B ref 56347); Sat, 02 Jul 2022 16:13:01 +0000 Original-Received: (at 56347) by debbugs.gnu.org; 2 Jul 2022 16:12:23 +0000 Original-Received: from localhost ([127.0.0.1]:42679 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1o7fjK-00019q-UZ for submit@debbugs.gnu.org; Sat, 02 Jul 2022 12:12:23 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:20695) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1o7fjI-00019c-O0 for 56347@debbugs.gnu.org; Sat, 02 Jul 2022 12:12:21 -0400 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 279F9440B56; Sat, 2 Jul 2022 12:12:15 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 682F2440950; Sat, 2 Jul 2022 12:12:13 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1656778333; bh=+ti7cNixsMPKP+qRdZFD5T4VIqPG9IdD8RxG9v9bkKU=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=nyHXSpkAulGopjdLPrUginBxhtHFxPx/NUl5dRmmNdsHoCQxJqvSlEX/0n/hb6LdQ mO2V4cOppQwSv234WvZ5RlRM3cz7rwp+yrcjj7TJqPxHwq/paVCO3unKHRL7NzekQP ZDq/x4TtcjXp/0b5dgIjL2skcJbfRY+YIxiVoE3LNRlqMUBuSZEdPv2LDghA9LfCpr /BUw74is8wfnRsSjIgOy31AzoIjs3NUpHxv41BjWGRFITJOCdntbz+LKp+0C1GMnw9 m5qfLNc7ubyp0aM964MP/ma/zDsQLlAEyGH7U3MFn3zGHQ8+hpeyJPFfLLDijLRtAv j6EOGvIRS8mEQ== Original-Received: from alfajor (unknown [45.72.196.165]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 125FB1204A0; Sat, 2 Jul 2022 12:12:13 -0400 (EDT) In-Reply-To: <83pmioca3h.fsf@gnu.org> (Eli Zaretskii's message of "Sat, 02 Jul 2022 09:17:06 +0300") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:235927 Archived-At: >> The patch below simplifies code around STRING_SET_MULTIBYTE. >> Any objection? > Rationale? STRING_SET_MULTIBYTE is fundamentally evil because it changes the nature of an object. Its current definition (like that of STRING_SET_UNIBYTE) is rather scary (it sometimes changes the nature of the arg passed to it, and sometimes replaces the arg with something else). >> --- a/src/composite.c >> +++ b/src/composite.c >> @@ -1879,11 +1879,7 @@ Otherwise (for terminal display), FONT-OBJECT must be a terminal ID, a >> for (i = SBYTES (string) - 1; i >= 0; i--) >> if (!ASCII_CHAR_P (SREF (string, i))) >> error ("Attempt to shape unibyte text"); >> - /* STRING is a pure-ASCII string, so we can convert it (or, >> - rather, its copy) to multibyte and use that thereafter. */ >> - Lisp_Object string_copy = Fconcat (1, &string); >> - STRING_SET_MULTIBYTE (string_copy); >> - string = string_copy; >> + /* STRING is a pure-ASCII string, so we can treat it as multibyte. */ > > Did you actually try your change in the situations where this problem > pops up? I don't even know how to go about doing that, no. > AFAIR, the code makes a copy of the string for good reasons: > the rest of handling of the string down the line barfs if we keep a > multibyte string here. [ I assume you meant "barfs if we keep a *uni*byte string here". ] Where? AFAICT `string` is only used in the subsequent code by passing it to `fill_gstring_header` and that function only passes that arg to `fetch_string_char_advance_no_check` and that function only looks at the string's SDATA, so as long as the sequence of bytes is consistent with a multibyte string (which we just checked with the ASCII_CHAR_P loop), I don't see any problem. >> --- a/src/lisp.h >> +++ b/src/lisp.h >> @@ -1637,12 +1637,10 @@ #define STRING_SET_UNIBYTE(STR) \ >> >> /* Mark STR as a multibyte string. Assure that STR contains only >> ASCII characters in advance. */ >> -#define STRING_SET_MULTIBYTE(STR) \ >> - do { \ >> - if (XSTRING (STR)->u.s.size == 0) \ >> - (STR) = empty_multibyte_string; \ >> - else \ >> - XSTRING (STR)->u.s.size_byte = XSTRING (STR)->u.s.size; \ >> +#define STRING_SET_MULTIBYTE(STR) \ >> + do { \ >> + eassert (XSTRING (STR)->u.s.size > 0); \ >> + XSTRING (STR)->u.s.size_byte = XSTRING (STR)->u.s.size; \ >> } while (false) >> >> /* Convenience functions for dealing with Lisp strings. */ > > You want to disallow uses of empty_multibyte_string? why? No, I want to reduce the scope of semantics of the macro, e.g. so it can be implemented as a function rather than a macro and so it doesn't magically substitute empty_multibyte_string into a variable that held something else. Stefan