From owner-freebsd-ports@FreeBSD.ORG Sun Jul 15 00:39:28 2007 Return-Path: X-Original-To: ports@freebsd.org Delivered-To: freebsd-ports@FreeBSD.ORG Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id A5E0816A402; Sun, 15 Jul 2007 00:39:28 +0000 (UTC) (envelope-from youshi10@u.washington.edu) Received: from mxout2.cac.washington.edu (mxout2.cac.washington.edu [140.142.33.4]) by mx1.freebsd.org (Postfix) with ESMTP id 7F94613C4B5; Sun, 15 Jul 2007 00:39:28 +0000 (UTC) (envelope-from youshi10@u.washington.edu) Received: from smtp.washington.edu (smtp.washington.edu [140.142.32.141] (may be forged)) by mxout2.cac.washington.edu (8.13.7+UW06.06/8.13.7+UW07.06) with ESMTP id l6F0dRHq017053 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sat, 14 Jul 2007 17:39:28 -0700 X-Auth-Received: from [192.168.10.45] (c-24-10-12-194.hsd1.ca.comcast.net [24.10.12.194]) (authenticated authid=youshi10) by smtp.washington.edu (8.13.7+UW06.06/8.13.7+UW07.03) with ESMTP id l6F0dQ4U029544 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Sat, 14 Jul 2007 17:39:27 -0700 Message-ID: <46996CBE.6050401@u.washington.edu> Date: Sat, 14 Jul 2007 17:39:26 -0700 From: Garrett Cooper User-Agent: Thunderbird 2.0.0.4 (Windows/20070604) MIME-Version: 1.0 To: Tim Kientzle References: <468C96C0.1040603@u.washington.edu> <468C9718.1050108@u.washington.edu> <468E60E9.80507@freebsd.org> <468E6C81.4060908@u.washington.edu> <468E7192.8030105@freebsd.org> <4696C0D2.6010809@u.washington.edu> <4697A210.2020301@u.washington.edu> <4698ADB5.7080600@u.washington.edu> <4698F98A.6080908@freebsd.org> <4699587F.30703@u.washington.edu> <469967A8.3080901@freebsd.org> In-Reply-To: <469967A8.3080901@freebsd.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-PMX-Version: 5.3.2.304607, Antispam-Engine: 2.5.1.298604, Antispam-Data: 2007.7.14.172533 X-Uwash-Spam: Gauge=IIIIIII, Probability=7%, Report='__CP_URI_IN_BODY 0, __CT 0, __CTE 0, __CT_TEXT_PLAIN 0, __HAS_MSGID 0, __MIME_TEXT_ONLY 0, __MIME_VERSION 0, __SANE_MSGID 0, __USER_AGENT 0' Cc: ports@freebsd.org, hackers@freebsd.org, krion@freebsd.org Subject: Re: Finding slowdowns in pkg_install (continuations of previous threads) X-BeenThere: freebsd-ports@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Porting software to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 15 Jul 2007 00:39:28 -0000 Tim Kientzle wrote: >> The following blog post has all of my commentary on the results I >> have: >> . > > >> I tried to unroll strcmp a bit by checking for the first character of >> the > > command, then run strcmp ... > > There's a somewhat more straightforward optimization that > relies on this same idea: > > switch(cmd[0]) { > case 'c': > /* Commands that start with 'c' */ > if (strcmp(cmd, 'cwd') == 0) > return (CMD_CWD); > /* FALLTHROUGH */ > case 'd': > /* Commands that start with 'd' */ > > .... etc.... > /* FALLTHROUGH */ > default: > /* Unrecognized command. */ > } > > This is a little cleaner and easier to read > and may even be faster than the code you > presented in your blog. Note that the fall through > ensures that all unrecognized commands end up at > the same place. If unrecognized commands are > very rare (they should be), then the fallthrough > is not a performance issue. > >> /** malloc buffer large enough to hold +CONTENTS **/ >> >> while(!feof(file_p)) { >> >> /** add content via fgetc **/ >> } > > Yuck. Try this instead: > > struct stat st; > int fd; > char *buff; > > fd = open(file); > fstat(fd, &st); > buff = malloc(st.st_size + 1); > read(fd, buff, st.st_size); > buff[st.st_size] = '\0'; > close(fd); > > Plus some error checking, of course. You can > use stdio if you prefer: > > FILE *f; > > f = fopen(file, "r"); > fstat(fileno(f), &st); > buff = malloc(st.st_size + 1); > fread(buff, 1, st.st_size, f); > buff[st.st_size] = '\0'; > fclose(f); > > Either way, this is a lot more efficient than > tens of thousands of calls to fgetc(). > > Cheers, > > Tim Kientzle Tim, That was a very good call. I didn't even think of read(2) over fgetc(2). That decreased the overall time by 0.7 seconds in installing vim, which is just a little shy of a 10% speedup. -Garrett