From owner-svn-src-head@freebsd.org Fri Jul 14 18:36:16 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id A80CEDA9D1F; Fri, 14 Jul 2017 18:36:16 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 75B0271819; Fri, 14 Jul 2017 18:36:16 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v6EIaFw6015056; Fri, 14 Jul 2017 18:36:15 GMT (envelope-from ian@FreeBSD.org) Received: (from ian@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v6EIaF8R015055; Fri, 14 Jul 2017 18:36:15 GMT (envelope-from ian@FreeBSD.org) Message-Id: <201707141836.v6EIaF8R015055@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ian set sender to ian@FreeBSD.org using -f From: Ian Lepore Date: Fri, 14 Jul 2017 18:36:15 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r320997 - head/sys/kern X-SVN-Group: head X-SVN-Commit-Author: ian X-SVN-Commit-Paths: head/sys/kern X-SVN-Commit-Revision: 320997 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 14 Jul 2017 18:36:16 -0000 Author: ian Date: Fri Jul 14 18:36:15 2017 New Revision: 320997 URL: https://svnweb.freebsd.org/changeset/base/320997 Log: Minor optimization: instead of converting between days and years using loops that start in 1970, assume most conversions are going to be for recent dates and use a precomputed number of days through the end of 2016. Modified: head/sys/kern/subr_clock.c Modified: head/sys/kern/subr_clock.c ============================================================================== --- head/sys/kern/subr_clock.c Fri Jul 14 18:13:54 2017 (r320996) +++ head/sys/kern/subr_clock.c Fri Jul 14 18:36:15 2017 (r320997) @@ -97,6 +97,13 @@ static const int month_days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; +/* + * Optimization: using a precomputed count of days between POSIX_BASE_YEAR and a + * recent year avoids lots of needless loop iterations in conversion. + * recent_base_days is the number of days through the end of recent_base_year. + */ +static const int recent_base_year = 2016; +static const int recent_base_days = 17167; /* * This inline avoids some unnecessary modulo operations @@ -157,8 +164,14 @@ clock_ct_to_ts(struct clocktime *ct, struct timespec * * Compute days since start of time * First from years, then from months. */ - days = 0; - for (i = POSIX_BASE_YEAR; i < year; i++) + if (year > recent_base_year) { + i = recent_base_year; + days = recent_base_days; + } else { + i = POSIX_BASE_YEAR; + days = 0; + } + for (; i < year; i++) days += days_in_year(i); /* Months */ @@ -188,8 +201,14 @@ clock_ts_to_ct(struct timespec *ts, struct clocktime * ct->dow = day_of_week(days); - /* Subtract out whole years, counting them in i. */ - for (year = POSIX_BASE_YEAR; days >= days_in_year(year); year++) + /* Subtract out whole years. */ + if (days >= recent_base_days) { + year = recent_base_year + 1; + days -= recent_base_days; + } else { + year = POSIX_BASE_YEAR; + } + for (; days >= days_in_year(year); year++) days -= days_in_year(year); ct->year = year;