[git commit] find: Unify time comparisons

Denys Vlasenko vda.linux at googlemail.com
Thu Oct 7 14:40:37 UTC 2021


commit: https://git.busybox.net/busybox/commit/?id=421c8767ba4ebf02fadc056026033e8feaf1a470
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

Split the common part into a function, to be reused.

The tail call is optimized, meaning now mmin/mtime just prepare arguments
and jump into the common code, thus near zero overhead.

This reduces code size slightly, e.g. on x86_64:
   text    data     bss     dec     hex filename
   4806       0       0    4806    12c6 findutils/find.o.orig
   4782       0       0    4782    12ae findutils/find.o

Of course, the savings are even greater when implementing atime/ctime
variants.

Signed-off-by: Ismael Luceno <ismael at iodev.co.uk>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 findutils/find.c | 36 ++++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/findutils/find.c b/findutils/find.c
index 6d55db4e9..f557bb762 100644
--- a/findutils/find.c
+++ b/findutils/find.c
@@ -618,30 +618,34 @@ ACTF(perm)
 	return (statbuf->st_mode & 07777) == ap->perm_mask;
 }
 #endif
+
+#if						\
+	ENABLE_FEATURE_FIND_MMIN  ||		\
+	ENABLE_FEATURE_FIND_MTIME
+static int time_cmp(time_t ftime, char time_char, time_t secs, time_t delta)
+{
+	time_t file_age = time(NULL) - ftime;
+	switch (time_char) {
+	case '+': return file_age >= secs + delta;
+	case '-': return file_age < secs;
+	/* just numeric time */
+	default:  return file_age >= secs && file_age < secs + delta;
+	}
+}
+#endif
+
 #if ENABLE_FEATURE_FIND_MTIME
 ACTF(mtime)
 {
-	time_t file_age = time(NULL) - statbuf->st_mtime;
-	time_t mtime_secs = ap->mtime_days * 24*60*60;
-	if (ap->mtime_char == '+')
-		return file_age >= mtime_secs + 24*60*60;
-	if (ap->mtime_char == '-')
-		return file_age < mtime_secs;
-	/* just numeric mtime */
-	return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
+	return time_cmp(statbuf->st_mtime, ap->mtime_char,
+			ap->mtime_days * 24*60*60, 24*60*60);
 }
 #endif
 #if ENABLE_FEATURE_FIND_MMIN
 ACTF(mmin)
 {
-	time_t file_age = time(NULL) - statbuf->st_mtime;
-	time_t mmin_secs = ap->mmin_mins * 60;
-	if (ap->mmin_char == '+')
-		return file_age >= mmin_secs + 60;
-	if (ap->mmin_char == '-')
-		return file_age < mmin_secs;
-	/* just numeric mmin */
-	return file_age >= mmin_secs && file_age < (mmin_secs + 60);
+	return time_cmp(statbuf->st_mtime, ap->mmin_char,
+			ap->mmin_mins * 60, 60);
 }
 #endif
 #if ENABLE_FEATURE_FIND_NEWER


More information about the busybox-cvs mailing list