From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.devel Subject: Re: empty-directory predicate, native implementation Date: Tue, 13 Oct 2020 17:48:04 +0300 Message-ID: <83y2ka18t7.fsf@gnu.org> References: Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="35703"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Arthur Miller Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Oct 13 16:51:29 2020 Return-path: Envelope-to: ged-emacs-devel@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 1kSLeD-0009Af-DT for ged-emacs-devel@m.gmane-mx.org; Tue, 13 Oct 2020 16:51:29 +0200 Original-Received: from localhost ([::1]:45808 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kSLeC-0001qB-FM for ged-emacs-devel@m.gmane-mx.org; Tue, 13 Oct 2020 10:51:28 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:43112) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kSLao-0006rx-9g for emacs-devel@gnu.org; Tue, 13 Oct 2020 10:47:59 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:36624) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kSLan-0004MT-48; Tue, 13 Oct 2020 10:47:57 -0400 Original-Received: from [176.228.60.248] (port=2730 helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1kSLam-0004aW-J6; Tue, 13 Oct 2020 10:47:56 -0400 In-Reply-To: (message from Arthur Miller on Tue, 13 Oct 2020 04:22:36 +0200) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 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-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:257539 Archived-At: > From: Arthur Miller > Date: Tue, 13 Oct 2020 04:22:36 +0200 > > It is easy to check for an empty dir in elisp; we can just list files > and check if there is a list or not: > > (null (directory-files directory-name nil nodots t))) > > where nodots is just regex to omit dot files (from dired+). > > But then this is quite inneficient. We are listing all files in each > dir since directory-files will return entire content of directory. Also > we are matching every filename to a regex just to eliminate first two. > Alternative would be to take length and see if it is > 2; but then we > would iterate whole list twice. So I can't see anything avialable in > dired/elisp and I think a predicate implemented in low-level is better solution. > We are really interested just to see if there is some file; so we can > just open dir, and read first few entries, if there is more then 2 files > (. and .. on *nix) we can just abort and return true. > > I have tested an idea with getdents (Linux syscall) and I can see > difference. Attached is a patch for dired.c and a test file to play with > some benchmark. If all we want is to stop reading a directory after N entries, why not simply extend directory-files to accept one more argument: the maximum number of file entries to read? That should be easy to implement, and will not require us to repeat all the code that is already there in directory-files (and which you missed). For example, file names need to be encoded before they are passed to libc functions (or any external APIs that expect file names). As a bonus, we will be able to return the file names we read, not just ignore them. And the code will be much more portable; if someone wants a more efficient Linux-only version, that could be added as an additional feature (assuming the speed difference justifies that). WDYT?