From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ivan Shmakov Newsgroups: gmane.emacs.bugs Subject: bug#20154: 25.0.50; json-encode-string is too slow for large strings Date: Sat, 21 Mar 2015 22:20:51 +0000 Message-ID: <87a8z6vvp8.fsf@violet.siamics.net> References: <86twxf68zk.fsf@yandex.ru> <83384zwxdx.fsf@gnu.org> <550C3218.4000903@yandex.ru> <831tkjww0y.fsf@gnu.org> <550C3AB9.7020403@yandex.ru> <83wq2bveq6.fsf@gnu.org> <550C491A.6000909@yandex.ru> <83siczvcss.fsf@gnu.org> <550C504A.10708@yandex.ru> <83r3sjva0q.fsf@gnu.org> <550C6A06.6040203@yandex.ru> <83fv8zv0b1.fsf@gnu.org> <550C9E7B.4060907@yandex.ru> <837fuavkn9.fsf@gnu.org> <550DDE25.1090000@yandex.ru> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1426976544 5335 80.91.229.3 (21 Mar 2015 22:22:24 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 21 Mar 2015 22:22:24 +0000 (UTC) To: 20154@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sat Mar 21 23:22:12 2015 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1YZRmV-0002uc-3Y for geb-bug-gnu-emacs@m.gmane.org; Sat, 21 Mar 2015 23:22:11 +0100 Original-Received: from localhost ([::1]:49132 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YZRmU-0003y1-8z for geb-bug-gnu-emacs@m.gmane.org; Sat, 21 Mar 2015 18:22:10 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:44074) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YZRmQ-0003xt-Uw for bug-gnu-emacs@gnu.org; Sat, 21 Mar 2015 18:22:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YZRmN-0007n1-H0 for bug-gnu-emacs@gnu.org; Sat, 21 Mar 2015 18:22:06 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:42118) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YZRmM-0007mv-OV for bug-gnu-emacs@gnu.org; Sat, 21 Mar 2015 18:22:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1YZRmM-0002DQ-F3 for bug-gnu-emacs@gnu.org; Sat, 21 Mar 2015 18:22:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Ivan Shmakov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 21 Mar 2015 22:22:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 20154 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 20154-submit@debbugs.gnu.org id=B20154.14269764658441 (code B ref 20154); Sat, 21 Mar 2015 22:22:02 +0000 Original-Received: (at 20154) by debbugs.gnu.org; 21 Mar 2015 22:21:05 +0000 Original-Received: from localhost ([127.0.0.1]:60127 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1YZRlQ-0002C5-KT for submit@debbugs.gnu.org; Sat, 21 Mar 2015 18:21:04 -0400 Original-Received: from fely.am-1.org ([78.47.74.50]:60886) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1YZRlN-0002BQ-C1 for 20154@debbugs.gnu.org; Sat, 21 Mar 2015 18:21:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=siamics.net; s=a2013295; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Message-ID:In-Reply-To:Date:Sender:References:Subject:To:From; bh=SgBT2RjGPOKyV3dG1gJhZbelKoHJgCZA3H7c78vYzP8=; b=cptk0t0Rij2rsIFTLC0ok619j9VeimTxMiFodbpxSf88iqfp+0IyvTmBkwpMMGHtgmL8xPSenP9JCwoHtYmR6wG8XCRtsqm6JTMIu+ivBHFE7GF8fKQuf8R+XZJ4fNef/sKH/WisbKVOHIlhcYajasDiFy5ZTUwIArCY4/CGAOg=; Original-Received: from [2a02:2560:6d4:26ca::1:1d] (helo=violet.siamics.net) by fely.am-1.org with esmtps (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.80) (envelope-from ) id 1YZRlM-0006DN-89 for 20154@debbugs.gnu.org; Sat, 21 Mar 2015 22:21:00 +0000 Original-Received: from localhost ([::1] helo=violet.siamics.net) by violet.siamics.net with esmtps (TLS1.2:RSA_AES_128_CBC_SHA1:128) (Exim 4.80) (envelope-from ) id 1YZRlF-0002kv-93 for 20154@debbugs.gnu.org; Sun, 22 Mar 2015 05:20:53 +0700 Mail-Followup-To: 20154@debbugs.gnu.org In-Reply-To: <550DDE25.1090000@yandex.ru> (Dmitry Gutov's message of "Sat, 21 Mar 2015 23:09:57 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 140.186.70.43 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.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:100767 Archived-At: >>>>> Dmitry Gutov writes: [=E2=80=A6] > Here's the updated definition (by the way, `json-special-chars' is > still needed, to convert ?\n to ?n, and so on, and the performance > hit is negligible). Perhaps a plain vector may fit there? > (defun json-encode-string-1 (string) > "Return a JSON representation of STRING." > (with-temp-buffer > (insert string) > (goto-char (point-min)) > ;; Skip over ASCIIish printable characters. > (while (re-search-forward "\\([\"\\/\b\f\n\r\t]\\)\\|[^ -~]" nil t) > (let ((c (char-before))) > (delete-region (1- (point)) (point)) > (if (match-beginning 1) > ;; Special JSON character (\n, \r, etc.). > (insert "\\" (car (rassoc c json-special-chars))) > ;; Fallback: UCS code point in \uNNNN form. > (insert (format "\\u%04x" c))))) > (concat "\"" (buffer-string) "\""))) FWIW, using replace-match in the loop seem to speed up the routine by another few percents. (while (re-search-forward "\\([\"\\/\b\f\n\r\t]\\)\\|[^ -~]" nil t) (let ((c (char-before))) (replace-match (if (match-beginning 1) ;; Special JSON character (\n, \r, etc.). (string ?\\ (car (rassq c json-special-chars))) ;; Fallback: UCS code point in \uNNNN form. (format "\\u%04x" c)) t t))) [=E2=80=A6] --=20 FSF associate member #7257 http://boycottsystemd.org/ =E2=80=A6 3013 B6A0= 230E 334A