From owner-freebsd-current Wed Mar 13 22:40:37 2002 Delivered-To: freebsd-current@freebsd.org Received: from critter.freebsd.dk (critter.freebsd.dk [212.242.86.163]) by hub.freebsd.org (Postfix) with ESMTP id B94E637B416 for ; Wed, 13 Mar 2002 22:40:34 -0800 (PST) Received: from critter.freebsd.dk (localhost [127.0.0.1]) by critter.freebsd.dk (8.12.2/8.12.2) with ESMTP id g2E6e4np041162; Thu, 14 Mar 2002 07:40:05 +0100 (CET) (envelope-from phk@critter.freebsd.dk) To: David Greenman Cc: John Indra , freebsd-current@FreeBSD.ORG Subject: Re: malloc() and the stock Perl in -CURRENT (and -STABLE) In-Reply-To: Your message of "Wed, 13 Mar 2002 22:25:18 PST." <20020313222518.J27616@nexus.root.com> Date: Thu, 14 Mar 2002 07:40:04 +0100 Message-ID: <41161.1016088004@critter.freebsd.dk> From: Poul-Henning Kamp Sender: owner-freebsd-current@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG In message <20020313222518.J27616@nexus.root.com>, David Greenman writes: >>The above perl program results in a loop more or less like: >> >> n = 2 >> for (i = 0; i < 1000000; i++) >> realloc(n++); >> >>Now, if you read _any_ malloc(3) man page, they will tell you that there >>is no way it can be guaranteed that this does not result in a lot of >>copying. > > Um, except that copying isn't what is causing the problem. The performance >problem is apparantly caused by tens of thousands of page faults per second as >the memory is freed and immediately reallocated again from the kernel. Doesn't >phkmalloc keep a small pool of allocations around to avoid problems like >this? Yes it does, but it doesn't help here. Basically what happens is that relloc() is called on to extend a string of one megabyte by another page, so it allocates 1M+1p and copies the contents over. Now, in this very particular cornercase, we might be able to optimize for just being able to allocate the next page, but in all real-world scenarioes I've seen, real usage is more like: long loop { realloc(n++); do some other stuff involving malloc/free/realloc } which negates that optimization. But if somebody wants to try to code this optimization, I'll be more than happy to review the result. I just don't expect it to do much in "real-life" as opposed to "silly benchmark" situations. -- Poul-Henning Kamp | UNIX since Zilog Zeus 3.20 phk@FreeBSD.ORG | TCP/IP since RFC 956 FreeBSD committer | BSD since 4.3-tahoe Never attribute to malice what can adequately be explained by incompetence. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-current" in the body of the message