Date: Wed, 13 Jun 2007 10:03:34 +0200 From: Ed Schouten <ed@fxq.nl> To: Garrett Cooper <youshi10@u.washington.edu> Cc: FreeBSD Hackers <freebsd-hackers@freebsd.org> Subject: Re: Reason for doing malloc / bzero over calloc (performance)? Message-ID: <20070613080334.GR89502@hoeg.nl> In-Reply-To: <466F9EFE.9020402@u.washington.edu> References: <466F7FD1.2020303@u.washington.edu> <466F9EFE.9020402@u.washington.edu>
next in thread | previous in thread | raw e-mail | index | archive | help
--brdEIFGMNIjz5YJG Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable * Garrett Cooper <youshi10@u.washington.edu> 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 > <http://boredzo.org/blog/archives/2006-11-26/calloc-vs-malloc>. 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 <ed@fxq.nl> 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--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20070613080334.GR89502>