From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Paul Pogonyshev Newsgroups: gmane.emacs.devel Subject: Make computational threads leave user interface usable Date: Wed, 1 Nov 2017 16:06:23 +0100 Message-ID: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" X-Trace: blaine.gmane.org 1509548793 27558 195.159.176.226 (1 Nov 2017 15:06:33 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 1 Nov 2017 15:06:33 +0000 (UTC) To: Emacs developers Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Nov 01 16:06:29 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 1e9ub2-0006WX-SC for ged-emacs-devel@m.gmane.org; Wed, 01 Nov 2017 16:06:24 +0100 Original-Received: from localhost ([::1]:56239 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e9ubA-0006Xy-A7 for ged-emacs-devel@m.gmane.org; Wed, 01 Nov 2017 11:06:32 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:49505) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e9ub4-0006XO-6I for emacs-devel@gnu.org; Wed, 01 Nov 2017 11:06:27 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e9ub3-0000hL-2P for emacs-devel@gnu.org; Wed, 01 Nov 2017 11:06:26 -0400 Original-Received: from mail-pf0-x235.google.com ([2607:f8b0:400e:c00::235]:45884) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1e9ub2-0000h6-S6 for emacs-devel@gnu.org; Wed, 01 Nov 2017 11:06:24 -0400 Original-Received: by mail-pf0-x235.google.com with SMTP id d28so2180008pfe.2 for ; Wed, 01 Nov 2017 08:06:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=ZZu6FJuEOisqKhHU32ymumj3DhWbPWZS80Jfh0/5kk0=; b=AZLCEdGYsBeP3IO3IAs5l4Lwcfn7JDyqA79Rw33//WWgK1eeEYL/QAfPdf0oc0LdF/ 5WaHdmmQh6E0KGz7MqiWabtfvc1bem0L+EnI10IhtL75uSQHQ6+JhxtkPiaud+LFzfQx MMr8znEovlzjpfK4DPy25exK6Sd0glbdAbqq+WOXsLCGe6Bm6EUFLUR8/vO2QfNcFnQF puzoNX0pRZpFrov7XLWgIFri5/4sqNwwnGpnv2VHfURybpsb1IL+tOS89J9N6ZuNjnxD oZ3gld/si60B2qcTfm4/tbQs2nYl9WSkKydKS8/9jhneGCfLryHwkqgtvcIV/G+r8A9Y YxTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=ZZu6FJuEOisqKhHU32ymumj3DhWbPWZS80Jfh0/5kk0=; b=kK1k5mvviipRmGJZ2WqbeYueRn5W3YKWibO1mjTQEPSw4KxAID08481bO5pSdxAaVp D1wTmKfWSQUQN2E7GvmWGHD/RShDwkGnm0UbFjomlme/jej8UcyNzR/30FMJB3+e3+cL YEgILm442t0XB4cuiHAshiMKF2rcMZS/lwah+V89Shp1VyAv1tZCr7InvS/1RNVG5Kij nAKTFywEZFRxMVcCjrVb373ygK40zyjkPYRp3MCPg1tnFWA5bHMrtbqdnsGoLK+1+Yh7 xg8oJXAUkre9OQ9kBaWLUBEEEeD4hKy/OM8IkLKIanpSdlR89GFtjvMSV1uKro8ijMm5 fdsw== X-Gm-Message-State: AMCzsaURAcIrs7zxqiKVTE0gj6hL27aYC1a5CBYDBrt6SaYsIFk90DJW lSBYGj65DylKzeQnbw0fVNN3t2RH2Q5oWwep1lyP X-Google-Smtp-Source: ABhQp+TL9zX3O4IkA7vdSlDMA9hNIICg9M6NWYojHAyWS2Lz9pwnYjci0cI2MNLO0zj3d38eOghs/EoSOHCd/CMPFOU= X-Received: by 10.99.3.213 with SMTP id 204mr147767pgd.407.1509548783449; Wed, 01 Nov 2017 08:06:23 -0700 (PDT) Original-Received: by 10.100.162.238 with HTTP; Wed, 1 Nov 2017 08:06:23 -0700 (PDT) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::235 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:219845 Archived-At: Since several versions Emacs has Lisp threads, but they are not used much, because 1) only one thread executes at any given time; 2) threads yield control to each other only with explicit (thread-yield) call or IO blocks. Which means that it is pointless to start a new thread for heavy computation: it will lock UI until finished anyway. Attached patch tries to solve point 2 only by making threads automatically yield control to each other from time to time. The patch is mainly for discussion. To see its effect, evaluate this expression: (make-thread (lambda () (dotimes (n 10000000) (when (= (% n 1000000) 0) (message "%s" n))) (message "done"))) In normal Emacs, UI is frozen until the thread completes. You see messages in the echo area, but that's rather a special case: you cannot e.g. navigate or type in the current buffer. With the patch, however, computation thread periodically (and automatically: no alteration of the expression is needed) yields to UI thread, leaving Emacs responsive while computation is going on. There are some problems, though. * Computation is 3-4 times slower than without auto-yielding. You can compare to unpatched Emacs or bind `thread-inhibit-auto-yield' to t in the thread function. This is probably due to the fact it auto-yields ~50 times per second. But on the other hand, does it really have to be that slow? I don't know much about Emacs internals, maybe someone with more knowledge can say if it is unavoidable, or yielding is just not optimized because it is just not done that frequently currently. * Message buffer contents seems screwed. But this is probably "normal", as non-main threads shouldn't touch UI as I understand. This expression is just an example. * Variable `thread-auto-yield-after' is accessible from Lisp, but rebinding doesn't take effect immediately. Which is especially bad if you rebind from nil to a non-nil value. In general, what are the thoughts about the patch? Does it look interesting, or is auto-yielding simply out of question? Paul P.S. Please CC me on replies, I'm not subscribed to the list.