From owner-freebsd-hackers@FreeBSD.ORG Wed Jun 13 08:03:36 2007 Return-Path: X-Original-To: freebsd-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 29EE316A46B for ; Wed, 13 Jun 2007 08:03:36 +0000 (UTC) (envelope-from ed@hoeg.nl) Received: from palm.hoeg.nl (mx0.hoeg.nl [83.98.131.211]) by mx1.freebsd.org (Postfix) with ESMTP id DCA8413C4AE for ; Wed, 13 Jun 2007 08:03:35 +0000 (UTC) (envelope-from ed@hoeg.nl) Received: by palm.hoeg.nl (Postfix, from userid 1000) id 23E861CC2E; Wed, 13 Jun 2007 10:03:34 +0200 (CEST) Date: Wed, 13 Jun 2007 10:03:34 +0200 From: Ed Schouten To: Garrett Cooper Message-ID: <20070613080334.GR89502@hoeg.nl> References: <466F7FD1.2020303@u.washington.edu> <466F9EFE.9020402@u.washington.edu> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="brdEIFGMNIjz5YJG" Content-Disposition: inline In-Reply-To: <466F9EFE.9020402@u.washington.edu> User-Agent: Mutt/1.5.15 (2007-04-06) Cc: FreeBSD Hackers 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 08:03:36 -0000 --brdEIFGMNIjz5YJG Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable * Garrett Cooper wrote: > Garrett Cooper wrote: > > Title says it all -- is there a particular reason why malloc/bzero= =20 > > should be used instead of calloc? > > -Garrett > As someone just brought to my attention, I should do some Googling. >=20 > Initial results brought up this:=20 > . 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; |=20 | if (size !=3D 0 && SIZE_T_MAX / size < num) { | errno =3D ENOMEM; | return (NULL); | } |=20 | size *=3D num; | if ( (p =3D 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, --=20 Ed Schouten WWW: http://g-rave.nl/ --brdEIFGMNIjz5YJG Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (FreeBSD) iD8DBQFGb6TW52SDGA2eCwURAifxAJ4nfTZQ+i6ndb2Ngynv71kzMpNa8gCeO8mI sIHQmSo/zwL2OLtLMmXm2o8= =7pA7 -----END PGP SIGNATURE----- --brdEIFGMNIjz5YJG--