From owner-svn-src-all@freebsd.org Wed Jun 6 13:01:58 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 632B7FF5487; Wed, 6 Jun 2018 13:01:58 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: from mail-qt0-x244.google.com (mail-qt0-x244.google.com [IPv6:2607:f8b0:400d:c0d::244]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id E6F2973219; Wed, 6 Jun 2018 13:01:57 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: by mail-qt0-x244.google.com with SMTP id s9-v6so6198576qtg.2; Wed, 06 Jun 2018 06:01:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=jh9Fxn0U00HSEc8X87PdggIsR63IuHSky6bKAcSyBew=; b=bFgDBWYr7GucGGtn10jHGCl9icu0YgilFUER34WIMC4Dn0emKVc20PmrZdaTP/+LhB pAnhP0MDpDG1cD5vnYKEBWw2r4Szh+KJl4/0P4vwAq7kHbFp4MI3ppKwHbJPdFG/NcmV vdHvLj+TQZ3c7YgtmO30aWQ+/bpd9EeprVtWwzCV/f20WTqnwQHWfUr7SHvlK7CxPI76 9Sj8oRm6JYdJ7h2kDChasuTKMhVB+02sYntmx+44ySCg4F005/iBY2TrSAIRD1LktUk2 NdtSSqCQuYs/i0NyXCDQP99GH7A0aCgEUkdWrnVPgoP4ohVnc/ebOzMUPf3NWEmp4zlf BI+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=jh9Fxn0U00HSEc8X87PdggIsR63IuHSky6bKAcSyBew=; b=P2BxPSzg+l/Do577lL+OjBFe2gFI+dp2TVLr0WAm/MWn9sS1DXww1acwHS+SAnLHtH M/GAdbyrB5afyX0qlNtr6j+wW3FW7aZvXDovw6qj6qPN+C4odBJvjVde3rX39ySasA19 YONa7lNpc9VKu2/Em9lnekBEuwjyujT0Ga97AVmVKo3HYcL1UbPBCndV00+diR3Ntv7p uLtI4kQOpd5aWKWREdsTtLLOhFjdCYOkYjxJz9trh42OaWYKZFIj+iTltAQjUYhddw62 7t/iewQEH8SIu7DtIFu7xziMNVOd1krex8PXYCIomwsEomWCD+5EiHbBw4EhUeSrqoZ2 Z+DA== X-Gm-Message-State: APt69E0V6qfuMTKar1DHIIAZsEZSgkyzVfs1EJ/i1SzL+wXIObse32g0 vppeHfP0h00dqy14QPykTyVXvd6D8lhOu8uUgNVpxg== X-Google-Smtp-Source: ADUXVKLYEHHcQNm5LstDfMUiMAW6iPHKKyHCQa92q4F+UZ7OaC632qaCSEGVFs3Sp59IVx+LsAcwTLOqolWrSWTNrEo= X-Received: by 2002:ac8:358a:: with SMTP id k10-v6mr2544606qtb.248.1528290117367; Wed, 06 Jun 2018 06:01:57 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ac8:1c4e:0:0:0:0:0 with HTTP; Wed, 6 Jun 2018 06:01:56 -0700 (PDT) In-Reply-To: <6E6E92B2-7536-4281-8EAF-72823E84902E@panasas.com> References: <201806060508.w56586c9053686@repo.freebsd.org> <6E6E92B2-7536-4281-8EAF-72823E84902E@panasas.com> From: Mateusz Guzik Date: Wed, 6 Jun 2018 15:01:56 +0200 Message-ID: Subject: Re: svn commit: r334702 - head/sys/sys To: Ravi Pokala Cc: Mateusz Guzik , src-committers , svn-src-all@freebsd.org, svn-src-head@freebsd.org Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.26 X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 06 Jun 2018 13:01:58 -0000 On Wed, Jun 6, 2018 at 1:35 PM, Ravi Pokala wrote: > > + * Passing the flag down requires malloc to blindly zero the entire > object. > > + * In practice a lot of the zeroing can be avoided if most of the object > > + * gets explicitly initialized after the allocation. Letting the > compiler > > + * zero in place gives it the opportunity to take advantage of this > state. > > This part, I still don't understand. :-( > > The call to bzero() is still for the full length passed in, so how does > this help? > > bzero is: #define bzero(buf, len) __builtin_memset((buf), 0, (len)) > ... > > + * _malloc_item = malloc(_size, type, (flags) &~ M_ZERO); > > + * if (((flags) & M_WAITOK) != 0 || _malloc_item != NULL) > > + * bzero(_malloc_item, _size); > > + * > > + * If the flag is set, the compiler knows the left side is always true, > > + * therefore the entire statement is true and the callsite is: > > I think you mean "... the *right* side is always true ...", since the left > side is the check for the flag being set. "If the flag is set, compiler > knows (the check for the flag being set) is always true" is tautological. > It explains how __builtin_constant_p(flags) being true allows the compiler to optimize out the flags-based check. I don't understand why this particular use runs into so much confusion. Just above it there is a M_ZERO check relying on the same property and receiving no attention. > ... > > + * If the flag is not set, the compiler knows the left size is always > false > > + * and the NULL check is needed, therefore the callsite is: > > Same issue here. > > > ... > > #ifdef _KERNEL > > #define malloc(size, type, flags) ({ > \ > > Now that I'm taking another look at this, I'm confused as to why the > entire macro expansion is inside parentheses? (The braces make sense, since > this is a block with local variables which need to be contained.) > > It is to return the value (the last expression). > > void *_malloc_item; \ > > @@ -193,7 +228,8 @@ void *malloc(size_t size, struct malloc_type > *type, in > > if (__builtin_constant_p(size) && __builtin_constant_p(flags) &&\ > > ((flags) & M_ZERO) != 0) { \ > > _malloc_item = malloc(_size, type, (flags) &~ M_ZERO); \ > > - if (((flags) & M_WAITOK) != 0 || _malloc_item != NULL) \ > > + if (((flags) & M_WAITOK) != 0 || \ > > + __predict_true(_malloc_item != NULL)) \ > > bzero(_malloc_item, _size); \ > > } else { \ > > _malloc_item = malloc(_size, type, flags); \ > > This confuses me too. If the constant-size/constant-flags/M_ZERO-is-set > test fails, then it falls down to calling malloc(). Which we are in the > middle of defining. So what does that expand to? > > Expansion is not recursive, so this is an actual call to malloc. -- Mateusz Guzik