From owner-freebsd-ports@FreeBSD.ORG Sun Jul 15 00:17:45 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 E117316A40A; Sun, 15 Jul 2007 00:17:45 +0000 (UTC) (envelope-from kientzle@freebsd.org) Received: from kientzle.com (h-66-166-149-50.snvacaid.covad.net [66.166.149.50]) by mx1.freebsd.org (Postfix) with ESMTP id 8CB8013C4A6; Sun, 15 Jul 2007 00:17:45 +0000 (UTC) (envelope-from kientzle@freebsd.org) Received: from [10.0.0.222] (p54.kientzle.com [66.166.149.54]) by kientzle.com (8.12.9/8.12.9) with ESMTP id l6F0HiH7003993; Sat, 14 Jul 2007 17:17:45 -0700 (PDT) (envelope-from kientzle@freebsd.org) Message-ID: <469967A8.3080901@freebsd.org> Date: Sat, 14 Jul 2007 17:17:44 -0700 From: Tim Kientzle User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.7.12) Gecko/20060422 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Garrett Cooper 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> In-Reply-To: <4699587F.30703@u.washington.edu> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit 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:17:46 -0000 > 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