From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: faster unicode character name completion Date: Mon, 07 Dec 2009 09:57:46 -0500 Message-ID: References: <87einfbxdw.fsf@red-bean.com> <87fx7r68s4.fsf@stupidchicken.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1260201326 25093 80.91.229.12 (7 Dec 2009 15:55:26 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 7 Dec 2009 15:55:26 +0000 (UTC) Cc: cyd@stupidchicken.com, emacs-devel@gnu.org To: Kenichi Handa Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Dec 07 16:55:19 2009 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1NHfvh-0000HE-W2 for ged-emacs-devel@m.gmane.org; Mon, 07 Dec 2009 16:55:18 +0100 Original-Received: from localhost ([127.0.0.1]:50546 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NHfve-0001Pz-Mj for ged-emacs-devel@m.gmane.org; Mon, 07 Dec 2009 10:55:14 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NHf28-00012N-Hh for emacs-devel@gnu.org; Mon, 07 Dec 2009 09:57:52 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NHf24-0000xW-MT for emacs-devel@gnu.org; Mon, 07 Dec 2009 09:57:52 -0500 Original-Received: from [199.232.76.173] (port=54662 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NHf24-0000xA-EW for emacs-devel@gnu.org; Mon, 07 Dec 2009 09:57:48 -0500 Original-Received: from ironport2-out.teksavvy.com ([206.248.154.181]:3097 helo=ironport2-out.pppoe.ca) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NHf24-0006Ld-E0 for emacs-devel@gnu.org; Mon, 07 Dec 2009 09:57:48 -0500 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApsEAA2nHEvO+INN/2dsb2JhbACBS9dlhDMEih4 X-IronPort-AV: E=Sophos;i="4.47,356,1257138000"; d="scan'208";a="50903783" Original-Received: from 206-248-131-77.dsl.teksavvy.com (HELO pastel.home) ([206.248.131.77]) by ironport2-out.pppoe.ca with ESMTP; 07 Dec 2009 09:57:46 -0500 Original-Received: by pastel.home (Postfix, from userid 20848) id 86B9780B4; Mon, 7 Dec 2009 09:57:46 -0500 (EST) In-Reply-To: (Kenichi Handa's message of "Mon, 07 Dec 2009 11:00:41 +0900") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux) X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:118365 Archived-At: >> > (defun ucs-name-completion (str) >> > (when (string-match "^[A-Za-z]*" str) >> > (let ((head (match-string 0 str)) >> > slot names) >> > (if (and (= (length head) (length str)) >> > (not (assoc-string str ucs-name-head-table))) >> > (ucs-name-filter str ucs-name-head-table) >> > (ucs-name-filter str (ucs-name-expand-table head)))))) >> I don't understand what ucs-name-filter is trying to do. > ?? It simply filters out elements that doesn't match with > STR from NAMES (alist). But then why is it needed? Doesn't `completion-table-dynamic' take care of that already? BTW, I tried the "precompute the table and autoload it" approach, and it works fine in the sense that it's fast. The generated file is about 1.3MB. Basically, the main downside of this approach is that it doesn't use the very strings we already have for the names, so after loading it we have 2 copies of every char name in memory. But I have a better idea: most of the time is not spent building the completion table, but rather just weeding out all the "chars" that don't have names, or should I say, looking for the few rare chars that do have a name. So the patch below seems to eb a good compromise: it uses up just about 1000K cons cells (i.e. 16KB on 64bit systems) to keep the precomputed set of ~34K chars that do have a name, so that building the completion table takes only a couple seconds. Stefan --- mule-cmds.el.~1.383.~ 2009-11-11 21:01:36.000000000 -0500 +++ mule-cmds.el 2009-12-07 09:55:16.000000000 -0500 @@ -2883,6 +2883,31 @@ (defvar nonascii-insert-offset 0 "This variable is obsolete.") (defvar nonascii-translation-table nil "This variable is obsolete.") +(defvar ucs-named-char-ranges + (purecopy + (eval-when-compile + (let ((ranges ()) + (first 0) + (last 0)) + (dotimes-with-progress-reporter (c #xEFFFF) + "Loading Unicode character names..." + (unless (or + (and (>= c #x3400 ) (<= c #x4dbf )) ; CJK Ideograph Extension Arch + (and (>= c #x4e00 ) (<= c #x9fff )) ; CJK Ideograph + (and (>= c #xd800 ) (<= c #xfaff )) ; Private/Surrogate + (and (>= c #x20000) (<= c #x2ffff)) ; CJK Ideograph Extensions B, C + (null (get-char-code-property c 'name))) + ;; This char has a name. + (if (<= c (1+ last)) + ;; Extend the current range. + (setq last c) + ;; We have to split the range. + (push (cons first last) ranges) + (setq first (setq last c))))) + (cons (cons first last) ranges)))) + "List of ranges of chars that have names. +Every range is of the form (FIRST . LAST).") + (defvar ucs-names nil "Alist of cached (CHAR-NAME . CHAR-CODE) pairs.") @@ -2891,18 +2916,16 @@ (or ucs-names (setq ucs-names (let (name names) - (dotimes-with-progress-reporter (c #xEFFFF) - "Loading Unicode character names..." - (unless (or - (and (>= c #x3400 ) (<= c #x4dbf )) ; CJK Ideograph Extension A - (and (>= c #x4e00 ) (<= c #x9fff )) ; CJK Ideograph - (and (>= c #xd800 ) (<= c #xfaff )) ; Private/Surrogate - (and (>= c #x20000) (<= c #x2ffff)) ; CJK Ideograph Extensions B, C - ) + (dolist (range ucs-named-char-ranges) + (let ((c (car range)) + (end (cdr range))) + (while (<= c end) (if (setq name (get-char-code-property c 'name)) - (setq names (cons (cons name c) names))) + (setq names (cons (cons name c) names)) + (error "Wrong range")) (if (setq name (get-char-code-property c 'old-name)) - (setq names (cons (cons name c) names))))) + (setq names (cons (cons name c) names))) + (setq c (1+ c))))) names)))) (defvar ucs-completions (lazy-completion-table ucs-completions ucs-names)