From owner-freebsd-hackers@FreeBSD.ORG Wed Jun 13 17:25:30 2007 Return-Path: X-Original-To: hackers@freebsd.org Delivered-To: freebsd-hackers@FreeBSD.ORG Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 925A916A46E for ; Wed, 13 Jun 2007 17:25:30 +0000 (UTC) (envelope-from youshi10@u.washington.edu) Received: from mxout7.cac.washington.edu (mxout7.cac.washington.edu [140.142.32.178]) by mx1.freebsd.org (Postfix) with ESMTP id 73BCF13C489 for ; Wed, 13 Jun 2007 17:25:30 +0000 (UTC) (envelope-from youshi10@u.washington.edu) Received: from hymn01.u.washington.edu (hymn01.u.washington.edu [140.142.8.55]) by mxout7.cac.washington.edu (8.13.7+UW06.06/8.13.7+UW07.05) with ESMTP id l5DHPTFl019385 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 13 Jun 2007 10:25:30 -0700 Received: from localhost (localhost [127.0.0.1]) by hymn01.u.washington.edu (8.13.7+UW06.06/8.13.7+UW07.03) with ESMTP id l5DHPTYL007000 for ; Wed, 13 Jun 2007 10:25:29 -0700 X-Auth-Received: from [192.55.52.2] by hymn01.u.washington.edu via HTTP; Wed, 13 Jun 2007 10:25:29 PDT Date: Wed, 13 Jun 2007 10:25:29 -0700 (PDT) From: youshi10@u.washington.edu To: hackers@freebsd.org In-Reply-To: <20070613080334.GR89502@hoeg.nl> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-PMX-Version: 5.3.1.294258, Antispam-Engine: 2.5.1.298604, Antispam-Data: 2007.6.13.100033 X-Uwash-Spam: Gauge=IIIIIII, Probability=7%, Report='NO_REAL_NAME 0, __CP_URI_IN_BODY 0, __CT 0, __CT_TEXT_PLAIN 0, __HAS_MSGID 0, __MIME_TEXT_ONLY 0, __MIME_VERSION 0, __SANE_MSGID 0' Cc: Subject: Re: Reason for doing malloc / bzero over calloc (performance)? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Jun 2007 17:25:30 -0000 On Wed, 13 Jun 2007, Ed Schouten wrote: > * Garrett Cooper wrote: >> Garrett Cooper wrote: >>> Title says it all -- is there a particular reason why malloc/bzero >>> should be used instead of calloc? >>> -Garrett >> As someone just brought to my attention, I should do some Googling. >> >> Initial results brought up this: >> . > > To be more precise; I took a look at the source code of calloc on my > FreeBSD 6 box: > > | void * > | calloc(num, size) > | size_t num; > | size_t size; > | { > | void *p; > | > | if (size != 0 && SIZE_T_MAX / size < num) { > | errno = ENOMEM; > | return (NULL); > | } > | > | size *= num; > | if ( (p = malloc(size)) ) > | bzero(p, size); > | return(p); > | } > > This means that the results on that website would be quite different > than the the ones that the FreeBSD 6 malloc/calloc should give. There is > even a difference between calloc'ing 10 block of 10 MB and 1 block of > 100 MB, which shouldn't make a difference here. calloc doesn't have any > performance-advantage here, because it just calls malloc/bzero. > > When looking at FreeBSD -CURRENT's calloc (won't paste it; too long), it > just does a arena_malloc/memset (which is malloc/bzero) for small > allocations but a huge_malloc for big allocations (say, multiple pages > big). The latter one already returns pages that are zero'd by the > kernel, so I suspect the calloc performance for big allocations on > -CURRENT is a lot better than on FreeBSD 6. As with FreeBSD 6, it > wouldn't matter if you calloc 10 pieces of 10 MB or one piece of 100 MB. > > Yours, > -- > Ed Schouten > WWW: http://g-rave.nl/ Hmmm... I wonder what the Mach kernel in OSX does to allocate memory then. I'll have to take a look at OpenDarwin's source sometime and see what it does. -Garrett