From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Arthur Miller Newsgroups: gmane.emacs.devel Subject: Re: empty-directory predicate, native implementation Date: Tue, 13 Oct 2020 20:43:41 +0200 Message-ID: References: <83y2ka18t7.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="8323"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Oct 13 20:45:23 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 1kSPIY-00023d-Sh for ged-emacs-devel@m.gmane-mx.org; Tue, 13 Oct 2020 20:45:22 +0200 Original-Received: from localhost ([::1]:47292 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kSPIX-0003M9-Qx for ged-emacs-devel@m.gmane-mx.org; Tue, 13 Oct 2020 14:45:21 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:55544) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kSPH4-00028Y-R0 for emacs-devel@gnu.org; Tue, 13 Oct 2020 14:43:51 -0400 Original-Received: from mail-oln040092068043.outbound.protection.outlook.com ([40.92.68.43]:60898 helo=EUR02-HE1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kSPH1-0002ix-OE; Tue, 13 Oct 2020 14:43:50 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MO03LO5TU8O1h2HARN53mijV4dp7V2p29HGH8kYCnj71doqp3kDngqlYmWpk5KkXQ5+qzY6q6JLKfBM2gT4JFhi7zsXDfmXC8yOkbTBg4/rWOpBFeODE1UuXaMKw2Cqx9QATWXe7I7TeTUpE2IvyyjJCuOeJA4eIFXHQYvmCHBM6auOpCTNPBtkoJyZfl4wnW13NYFXvubkwzg+amQ9S4kpE7/jLQ8MOYoZu8qEkoWZrzy9OjzcmKeRzjiFg0H3S2ktY6CDLRidPVZCKbh3MFE1vFjWYeTKxjGdpGPtDF+ZIPFmEhN2KuhAP0oCw/GFAkCHK1GmGy4kbqS/AS0hyZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wViyghv9cG8kwtaOP8YtpKvwW05fIjVdDZTIc5On2dY=; b=XkNZ8Bbf6gb06QMaizfvK/YcbxUQY7NBegOYDqKyQynUa1hSUyWBrtlmYzW78tRwtAw/jgZzbXiRHKdfda/3toGBseM3iBcxAGDXxxN0V1uYOLHFpqF9rvVWtu+WcqGGotgyvllmQmYt7H+8/C5Vks9667HYB0c/vcWPFv3RfNfNaaNYm9wd99KS6iU2HyeR3cRiJx5PHfVpU1cyr1rDREYVY1HiOVTasSMtcCk13Zxp7E9LUciSNY/S6Az+0iWIkSPmWWM82KOpxDFKcyUNYrcS9juJyq2FiI/1Cd2VzStChsthdR0oq9I2s0dUf8aqSm00hhez9v5Xjln0qXqiig== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=live.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wViyghv9cG8kwtaOP8YtpKvwW05fIjVdDZTIc5On2dY=; b=dNeFpJ7zKRrLDg2SSsXc59MGboGPUDKUac9KUd/vfXCcMd3G0dS2YjICxlKYcYaZrAb6I//GcZZcoZVQl0tHCRRLgcQwR1OShu6LsLgTRv3tOQcZuMTxdhRpMURHEBNz83qjLenow3OgeAU9kgU33n9dPZrr0d4mDbT1hM2xdMswPaQjBAowdDPSz3FLlMlQC4jKZ6+VfSlN/nQlT8FK0THR7tR5bVJ9pN9dJMPajXmaevOn1ETaWRRpH/NMKlZ/BEUxTpCrBugTPph15hAmeownk1CwDEnXiFIFBP/sUAYiw6ia7l9K6b3jSHZ13NmlKCnDYShW5zCRB4MMF3hsVQ== Original-Received: from AM5EUR02FT008.eop-EUR02.prod.protection.outlook.com (2a01:111:e400:7e1c::40) by AM5EUR02HT219.eop-EUR02.prod.protection.outlook.com (2a01:111:e400:7e1c::431) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3477.21; Tue, 13 Oct 2020 18:43:43 +0000 Original-Received: from VI1PR06MB4526.eurprd06.prod.outlook.com (2a01:111:e400:7e1c::42) by AM5EUR02FT008.mail.protection.outlook.com (2a01:111:e400:7e1c::69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3455.23 via Frontend Transport; Tue, 13 Oct 2020 18:43:43 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:86A12560B6B8039C5337ED7B1829CB1BE95E913173DFA3BF46BDA75299954348; UpperCasedChecksum:93507D7743FAB73E5E14C42D3119981E22450785CE3C50B85BD38C29EF432588; SizeAsReceived:7456; Count:46 Original-Received: from VI1PR06MB4526.eurprd06.prod.outlook.com ([fe80::b547:51cd:16c5:4487]) by VI1PR06MB4526.eurprd06.prod.outlook.com ([fe80::b547:51cd:16c5:4487%7]) with mapi id 15.20.3455.031; Tue, 13 Oct 2020 18:43:43 +0000 In-Reply-To: <83y2ka18t7.fsf@gnu.org> (Eli Zaretskii's message of "Tue, 13 Oct 2020 17:48:04 +0300") X-TMN: [A8yEo6HVoGHbsLVdqu1VzVBj8+FAYLKn] X-ClientProxiedBy: AM5PR0701CA0052.eurprd07.prod.outlook.com (2603:10a6:203:2::14) To VI1PR06MB4526.eurprd06.prod.outlook.com (2603:10a6:803:ac::17) X-Microsoft-Original-Message-ID: <878scac6g2.fsf@live.com> X-MS-Exchange-MessageSentRepresentingType: 1 Original-Received: from pascal.homepc (90.230.29.56) by AM5PR0701CA0052.eurprd07.prod.outlook.com (2603:10a6:203:2::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3477.11 via Frontend Transport; Tue, 13 Oct 2020 18:43:42 +0000 X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 46 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: 491c0007-f8fa-414b-f9d1-08d86fa7e89b X-MS-TrafficTypeDiagnostic: AM5EUR02HT219: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CapdBXLacwHGohY2i/M53GJE4ZibmAwyqb+NJTobtVOS4WBTiaLk/2++ZZzWQUL2bK+EmO5ZkXMaSZ8ziJgfCN2vPups+N9rTblz9/OrqecZROdThu5aKu4xS1gMV9yZtO8vPsrQFT/GuqzAvVEA8oojJJXlE9ILFO1wLkeddbrxP8WnS5guBFE2duckZihfQDh+byxHcLL1z0RWEQJyBA== X-MS-Exchange-AntiSpam-MessageData: 7zeAdPOViv8ysRWaIr4YkRPYj4HuKuuuC2qN1OS/YHVN3DtSAsEPuF6ElitwUpZoo/1xZp9jYcjjyiLipzfCVQOs2s9PAh16/K7m0L3u2mQ4NtH7SAgp/q0RHoMIlaDNrPxs6uottXZ9okhAP5q+4A== X-OriginatorOrg: live.com X-MS-Exchange-CrossTenant-Network-Message-Id: 491c0007-f8fa-414b-f9d1-08d86fa7e89b X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Oct 2020 18:43:42.9610 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-AuthSource: AM5EUR02FT008.eop-EUR02.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: Internet X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM5EUR02HT219 Received-SPF: pass client-ip=40.92.68.43; envelope-from=arthur.miller@live.com; helo=EUR02-HE1-obe.outbound.protection.outlook.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/10/13 14:43:43 X-ACL-Warn: Detected OS = Windows NT kernel [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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:257555 Archived-At: Eli Zaretskii writes: >> 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). Oh; and you say that now when I have just implemented a Windows version :-). > WDYT? I think that sounds like a very good idea; or rather a splended one! Much better then a predicate. If directory-files is asked just for 1 file, with regex to ignore, it would read max 3 files at most, and on some filysystems maybe only one file. > 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). Actually I didn't; I first tested with ENCODE_FILE but I didn't know what it does, and it appeared like it is working without, so I ditched it. I totally forgott about tramp & co, as Michael points out :-). I didn't count on it to be correct; I am not so very familiar with Emascs source, there are so many details to take care of, I was rather just presenting an idea.