From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: =?UTF-8?Q?Cl=c3=a9ment_Pit-Claudel?= Newsgroups: gmane.emacs.devel Subject: Re: Why does adding a useless copy-sequence and discarding the result make my ELisp 40 times faster? Date: Sat, 25 Mar 2017 09:45:26 -0400 Message-ID: <929e2866-26fa-c480-a51e-742e0bd3023f@gmail.com> References: <77900613-3b45-52f4-5770-38fe6f27e69c@gmail.com> <83vaqxbzub.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: blaine.gmane.org 1490449543 29549 195.159.176.226 (25 Mar 2017 13:45:43 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 25 Mar 2017 13:45:43 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 Cc: emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Mar 25 14:45:39 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1crm0g-0007CQ-MG for ged-emacs-devel@m.gmane.org; Sat, 25 Mar 2017 14:45:38 +0100 Original-Received: from localhost ([::1]:37417 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1crm0m-0004at-DK for ged-emacs-devel@m.gmane.org; Sat, 25 Mar 2017 09:45:44 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54459) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1crm0a-0004Zm-C1 for emacs-devel@gnu.org; Sat, 25 Mar 2017 09:45:34 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1crm0X-0005Eq-4u for emacs-devel@gnu.org; Sat, 25 Mar 2017 09:45:32 -0400 Original-Received: from mail-qt0-x22b.google.com ([2607:f8b0:400d:c0d::22b]:36213) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1crm0X-0005Ei-0g; Sat, 25 Mar 2017 09:45:29 -0400 Original-Received: by mail-qt0-x22b.google.com with SMTP id r45so10166651qte.3; Sat, 25 Mar 2017 06:45:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=DygUcZifX5HtWXlDBMW5+7Utelmd2/KI7piH0e6JjsQ=; b=ZnaKVmKAlOiaQgJzidA86qeopUnHqvBjAVSNEZ2YHnGas3sPitmbXZnpS0gJNJmzxi +W1vbThLs+Tjoobxygrzowmb7xFchzzdprg7v173A9X84Jmusaw6NWmRlb8JSYShGn3q dokiqqqqvn3RRBahxSLY0vFOE5m34UlyIHjZIl+tiytLcMfIttkKn5qLUjORRyENCIMN Xw60rGOy8St0Ln2warVaZFvbgA6JXal1K79oqdxtG5P3WMSw7Dl2UWqe1X1CjpiYOTe/ MhVxvOrgpGRQ0gKtaoi1kWJ+0RfHPglQKKqTuzfc8JUewmxigAeCxrk1nY5shYxlrXzJ lQ2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=DygUcZifX5HtWXlDBMW5+7Utelmd2/KI7piH0e6JjsQ=; b=T4OY9LtX7jUQKzkYMBQKJO64D8bKAuG0rq7VBW6dwvF/ww29hvQrRLhMm+a5HH9Rd/ cDZ4HllRS4xCwoHVN3neLxhTFxthncA9kGhPmnDcXjE3VhRS+nKkSdSXy15o9gOcBLHL o6RsUfO8Pi+MnJ19Dpc3p/TY9+EqIMv86d99kbFIzQR2XArqRDeuPW6sa8JUNrBEdPiX hcE+yLwFYtQOOt9j7BZlgHGDw8I7nuhiOFi2N1o+R4iTLl2a1SjXW4EmL631rhH7njnC QhA3b8tn3mLX+BpWoOa5EIX7FkkxyvEtYFdt4vyDqcyf1RiIqKwxvh5VoM/XBBlMjjif MCgg== X-Gm-Message-State: AFeK/H3CZH0OjKn6sd5HksmbiOrHqcW+FuShjDi7ud8q/0NK2/gNoEJPAzqDV4iRW3T9ZQ== X-Received: by 10.200.44.156 with SMTP id 28mr13039649qtw.48.1490449528075; Sat, 25 Mar 2017 06:45:28 -0700 (PDT) Original-Received: from [18.189.26.125] (dhcp-18-189-26-125.dyn.mit.edu. [18.189.26.125]) by smtp.gmail.com with ESMTPSA id t124sm3730894qkf.27.2017.03.25.06.45.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 25 Mar 2017 06:45:27 -0700 (PDT) In-Reply-To: <83vaqxbzub.fsf@gnu.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400d:c0d::22b 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:213336 Archived-At: On 2017-03-25 03:05, Eli Zaretskii wrote: >> From: Clément Pit-Claudel Date: Sat, 25 Mar >> 2017 00:24:20 -0400 >> >> On 2017-03-25 00:06, Clément Pit-Claudel wrote: >>> * Why does running additional, supposedly useless code speed >>> things up so much? (1) >> >> Interestingly, it turns out that (copy-sequence) isn't needed: >> instead, it's enough to use (sleep-for 0.000001) (or even >> (sleep-for cl-least-positive-normalized-float)) to get the 40x >> speedup. > > Because, if you don't sleep, the process is not yet running by the > time you get to process-send-string, and then Emacs waits for a much > longer time for it to become running? Quick experiments suggest this isn't the case: hoisting the (sleep) calls out of the loop and sleeping n-inner-loops times instead yields much slower code than when the sleep calls are interleaved with the process-send-string calls. That is, I don't observe the aforementioned speedup if I sleep a single, "long" time before the loop, instead of putting a small sleep in each iteration. Similarly, taking the process creation out of the function being timed, then adding a long sleep and a call to accept-process-output before the benchmarked code doesn't change performance significantly: (benchmark-send-string t 200 65536 proc) → (0.013895306 0 0.0) (benchmark-send-string nil 200 65536 proc) → (2.3035461760000002 0 0.0) I used this code to test this: (require 'cl-lib) (cl-float-limits) (defun benchmark-send-string (cargo-cult n-inner-loops size proc) (let* ((message (make-string size ?a))) (dotimes (_ n-inner-loops) (when cargo-cult (sleep-for cl-least-positive-normalized-float)) (process-send-string proc message)) (kill-process proc))) (pcase-let ((`(,cargo-cult ,n-inner-loops ,size . ,prog) argv)) (setq cargo-cult (read cargo-cult)) (setq size (string-to-number size)) (setq n-inner-loops (string-to-number n-inner-loops)) (let* ((process-connection-type nil) (proc (apply #'start-process "reader" nil prog)) (form `(benchmark-send-string ,cargo-cult ,n-inner-loops ,size proc))) (sleep-for 1.0) (accept-process-output nil 1.0) (message "%S → %S" form (eval `(benchmark-run 1 ,form))))) (setq argv nil) Clément.