From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Tassilo Horn Newsgroups: gmane.emacs.devel Subject: Re: [RFC]: replace-region-contents Date: Fri, 08 Feb 2019 17:23:29 +0100 Message-ID: <871s4idzb2.fsf@gnu.org> References: <871s4rqk7u.fsf@gnu.org> <87o97syvno.fsf@gnu.org> <878syubwv3.fsf@gnu.org> <87y36u9xqt.fsf@gnu.org> <83k1ietcox.fsf@gnu.org> <87k1iew3u6.fsf@gnu.org> <83va1wsy95.fsf@gnu.org> <87h8dgetp1.fsf@gnu.org> <83mun8styg.fsf@gnu.org> <87lg2saiz9.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="15085"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: Paul Eggert , monnier@IRO.UMontreal.CA, emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Feb 08 17:24:57 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gs8xR-0003jq-VP for ged-emacs-devel@m.gmane.org; Fri, 08 Feb 2019 17:24:54 +0100 Original-Received: from localhost ([127.0.0.1]:59020 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gs8xQ-0004eY-Uj for ged-emacs-devel@m.gmane.org; Fri, 08 Feb 2019 11:24:52 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:35136) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gs8wG-0004KY-85 for emacs-devel@gnu.org; Fri, 08 Feb 2019 11:23:41 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:59619) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gs8wE-0000z5-4r; Fri, 08 Feb 2019 11:23:38 -0500 Original-Received: from auth2-smtp.messagingengine.com ([66.111.4.228]:40115) by fencepost.gnu.org with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.82) (envelope-from ) id 1gs8w9-0002fm-Pw; Fri, 08 Feb 2019 11:23:37 -0500 Original-Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailauth.nyi.internal (Postfix) with ESMTP id 1ECEE216C5; Fri, 8 Feb 2019 11:23:33 -0500 (EST) Original-Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Fri, 08 Feb 2019 11:23:33 -0500 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrledvgdekkecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfhuthenuceurghilhhouhhtmecufedt tdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephffvufhfffgjkf gfgggtsehttdertddtredtnecuhfhrohhmpefvrghsshhilhhoucfjohhrnhcuoehtshgu hhesghhnuhdrohhrgheqnecukfhppeelfedrvdefiedruddvledrhedvnecurfgrrhgrmh epmhgrihhlfhhrohhmpehthhhorhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhi thihqdekieejfeekjeekgedqieefhedvleekqdhtshguhheppehgnhhurdhorhhgsehfrg hsthhmrghilhdrfhhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Original-Received: from thinkpad-t440p (p5dec8134.dip0.t-ipconnect.de [93.236.129.52]) by mail.messagingengine.com (Postfix) with ESMTPA id BD3D81031B; Fri, 8 Feb 2019 11:23:30 -0500 (EST) In-Reply-To: <87lg2saiz9.fsf@gnu.org> (Tassilo Horn's message of "Wed, 06 Feb 2019 19:07:38 +0100") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:233134 Archived-At: Hi all, if nobody complains, I'd like to install the following patch. I've set too_expensive to 64 because then my benchmark was almost as fast as the original version without `replace-buffer-contents', and I couldn't find any difference in observable behavior in that value anyway except lower values give much better performance but a too low value like 10 will segfault. My main concern was that point was at the same position before and after pretty-printing my JSON. I also enabled the heuristic flag, although I've seen no difference without it. The comments just suggest there are cases where performance benefits from that. --8<---------------cut here---------------start------------->8--- diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el index 7d9f0bba4c..5618f1b478 100644 --- a/lisp/emacs-lisp/subr-x.el +++ b/lisp/emacs-lisp/subr-x.el @@ -250,6 +250,33 @@ string-remove-suffix (substring string 0 (- (length string) (length suffix))) string)) +(defun replace-region-contents (beg end replace-fn) + "Replace the region between BEG and END using REPLACE-FN. + +REPLACE-FN runs on the current buffer narrowed to the region. It +should return either a string or a buffer replacing the region. + +The replacement is performed using `replace-buffer-contents'. + +Note: If the replacement is a string, it'll be placed in a +temporary buffer so that `replace-buffer-contents' can operate on +it. Therefore, if you already have the replacement in a buffer, +it makes no sense to convert it to a string using +`buffer-substring' or similar." + (save-excursion + (save-restriction + (narrow-to-region beg end) + (goto-char (point-min)) + (let ((repl (funcall replace-fn))) + (if (bufferp repl) + (replace-buffer-contents repl) + (let ((source-buffer (current-buffer))) + (with-temp-buffer + (insert repl) + (let ((tmp-buffer (current-buffer))) + (set-buffer source-buffer) + (replace-buffer-contents tmp-buffer))))))))) + (provide 'subr-x) ;;; subr-x.el ends here diff --git a/lisp/json.el b/lisp/json.el index 26cd48f41d..8f86104c19 100644 --- a/lisp/json.el +++ b/lisp/json.el @@ -52,6 +52,7 @@ ;;; Code: +(require 'subr-x) (require 'map) ;; Parameters @@ -740,14 +741,14 @@ json-pretty-print-buffer (defun json-pretty-print (begin end) "Pretty-print selected region." (interactive "r") - (atomic-change-group - (let ((json-encoding-pretty-print t) - ;; Distinguish an empty objects from 'null' - (json-null :json-null) - ;; Ensure that ordering is maintained - (json-object-type 'alist) - (txt (delete-and-extract-region begin end))) - (insert (json-encode (json-read-from-string txt)))))) + (let ((json-encoding-pretty-print t) + ;; Distinguish an empty objects from 'null' + (json-null :json-null) + ;; Ensure that ordering is maintained + (json-object-type 'alist)) + (replace-region-contents + begin end + (lambda () (json-encode (json-read)))))) (defun json-pretty-print-buffer-ordered () "Pretty-print current buffer with object keys ordered." diff --git a/src/editfns.c b/src/editfns.c index a9ac263daf..7a600bacf1 100644 --- a/src/editfns.c +++ b/src/editfns.c @@ -1910,6 +1910,11 @@ determines whether case is significant or ignored. */) #undef ELEMENT #undef EQUAL +#define USE_HEURISTIC + +#ifdef USE_HEURISTIC +#define DIFFSEQ_HEURISTIC +#endif /* Counter used to rarely_quit in replace-buffer-contents. */ static unsigned short rbc_quitcounter; @@ -2017,8 +2022,11 @@ differences between the two buffers. */) .insertions = SAFE_ALLOCA (ins_bytes), .fdiag = buffer + size_b + 1, .bdiag = buffer + diags + size_b + 1, +#ifdef DIFFSEQ_HEURISTIC + .heuristic = true, +#endif /* FIXME: Find a good number for .too_expensive. */ - .too_expensive = 1000000, + .too_expensive = 64, }; memclear (ctx.deletions, del_bytes); memclear (ctx.insertions, ins_bytes); --8<---------------cut here---------------end--------------->8--- Bye, Tassilo