From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: =?utf-8?Q?Ludovic_Court=C3=A8s?= Newsgroups: gmane.lisp.guile.user Subject: Re: string-for-each vs. for-each+string->list performance Date: Fri, 12 Jun 2020 22:13:58 +0200 Message-ID: <87k10c2gah.fsf@gnu.org> References: <7377875a-507d-471e-866b-0f505517ff82@www.fastmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="106416"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) To: guile-user@gnu.org Cancel-Lock: sha1:rxjb4bLNLhNBGTSv5d20nS1DiRw= Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Fri Jun 12 22:14:19 2020 Return-path: Envelope-to: guile-user@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 1jjq4A-000RYt-Sd for guile-user@m.gmane-mx.org; Fri, 12 Jun 2020 22:14:18 +0200 Original-Received: from localhost ([::1]:51100 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jjq49-00011V-Pe for guile-user@m.gmane-mx.org; Fri, 12 Jun 2020 16:14:17 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:54960) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jjq3z-00011C-Vn for guile-user@gnu.org; Fri, 12 Jun 2020 16:14:07 -0400 Original-Received: from ciao.gmane.io ([159.69.161.202]:52814) by eggs.gnu.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jjq3y-0001vp-A7 for guile-user@gnu.org; Fri, 12 Jun 2020 16:14:07 -0400 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1jjq3v-000RI7-Sr for guile-user@gnu.org; Fri, 12 Jun 2020 22:14:03 +0200 X-Injected-Via-Gmane: http://gmane.org/ X-URL: http://www.fdn.fr/~lcourtes/ X-Revolutionary-Date: 25 Prairial an 228 de la =?utf-8?Q?R=C3=A9volution?= X-PGP-Key-ID: 0x090B11993D9AEBB5 X-PGP-Key: http://www.fdn.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 3CE4 6455 8A84 FDC6 9DB4 0CFB 090B 1199 3D9A EBB5 X-OS: x86_64-pc-linux-gnu Received-SPF: pass client-ip=159.69.161.202; envelope-from=guile-user@m.gmane-mx.org; helo=ciao.gmane.io X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/12 12:10:03 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -8 X-Spam_score: -0.9 X-Spam_bar: / X-Spam_report: (-0.9 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:16583 Archived-At: Hi, Linus Björnstam skribis: > You can cut another 15-ish % from that loop by making an inline loop, btw > > (let loop ((pos 0)) > (when (< pos (string-length str)) > ... > (loop (1+ pos))) > > I have been looking at the disassembly, even for simpler cases, but I haven't been able to understand enough of it. > > BTW: string-for-each is in the default environment, and is probably the same as the srfi-13 C implementation. ‘string-for-each’ in C (the default) is slower than its Scheme counterpart: --8<---------------cut here---------------start------------->8--- scheme@(guile-user)> (define (sfe proc str) (define len (string-length str)) (let loop ((i 0)) (unless (= i len) (proc (string-ref str i)) (loop (+ 1 i))))) scheme@(guile-user)> (define str (make-string 15000000)) scheme@(guile-user)> ,t (sfe identity str) ;; 0.263725s real time, 0.263722s run time. 0.000000s spent in GC. scheme@(guile-user)> ,t (sfe identity str) ;; 0.259538s real time, 0.259529s run time. 0.000000s spent in GC. scheme@(guile-user)> ,t (string-for-each identity str) ;; 0.841632s real time, 0.841624s run time. 0.000000s spent in GC. scheme@(guile-user)> (version) $2 = "3.0.2" --8<---------------cut here---------------end--------------->8--- In general we seem to pay a high price for leaving (calling a subr) and re-entering (via ‘scm_call_n’) the VM. This is especially acute here because there’s almost nothing happening in C, so we keep bouncing between Scheme and C. That’s another reason to start rewriting such primitives in Scheme and have the C functions just call out to Scheme. If we do: perf record guile -c '(string-for-each identity (make-string 15000000))' we get this profile: --8<---------------cut here---------------start------------->8--- Overhead Command Shared Object Symbol 31.10% guile libguile-3.0.so.1.1.1 [.] vm_regular_engine 27.48% guile libguile-3.0.so.1.1.1 [.] scm_call_n 14.34% guile libguile-3.0.so.1.1.1 [.] scm_jit_enter_mcode 3.55% guile libguile-3.0.so.1.1.1 [.] scm_i_string_ref 3.37% guile libguile-3.0.so.1.1.1 [.] get_callee_vcode 2.34% guile libguile-3.0.so.1.1.1 [.] scm_call_1 2.31% guile libguile-3.0.so.1.1.1 [.] scm_string_for_each --8<---------------cut here---------------end--------------->8--- Indeed, we get better performance when turning off JIT: --8<---------------cut here---------------start------------->8--- $ GUILE_JIT_THRESHOLD=-1 time guile -c '(string-for-each identity (make-string 15000000))' 0.47user 0.00system 0:00.47elapsed 100%CPU (0avgtext+0avgdata 26396maxresident)k 0inputs+0outputs (0major+1583minor)pagefaults 0swaps $ GUILE_JIT_THRESHOLD=100 time guile -c '(string-for-each identity (make-string 15000000))' 0.83user 0.00system 0:00.83elapsed 100%CPU (0avgtext+0avgdata 26948maxresident)k 0inputs+0outputs (0major+1748minor)pagefaults 0swaps $ GUILE_JIT_THRESHOLD=0 time guile -c '(string-for-each identity (make-string 15000000))' 0.84user 0.00system 0:00.85elapsed 100%CPU (0avgtext+0avgdata 27324maxresident)k 0inputs+0outputs (0major+2548minor)pagefaults 0swaps --8<---------------cut here---------------end--------------->8--- So it seems that we just keep firing the JIT machinery on every ‘scm_call_n’ for no benefit. That’s probably also the reason why ‘%after-gc-hunk’, ‘reap-pipes’, & co. always show high in statprof: https://lists.gnu.org/archive/html/guile-devel/2020-05/msg00019.html Thanks, Ludo’.