delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2018/01/12/09:05:44

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:subject:to:references:from:message-id:date
:mime-version:in-reply-to:content-type
:content-transfer-encoding; q=dns; s=default; b=vO6HXodkwWRb4dC4
ZJkDZGBWHRAWsSRjJWjfq7xWbTJdT1MTts321c/s3O300qyBCkcHROtAcZ0KFzAF
oMptiBkTfnb1LIEJ6CHnAMAZZboi+vWURExfEx5cL7zeo3GB8NlsPi10NZOuITJg
cTkg7ANq5Rs9Hf1ZJFbQHUH9Q7U=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:subject:to:references:from:message-id:date
:mime-version:in-reply-to:content-type
:content-transfer-encoding; s=default; bh=nwan4Bpw0XBUPA7zQxdwKX
mYKBM=; b=aQzSys4NTQ5a47yaw1SMR8HcQW0e2AzNcJ2FeD+hRWzawSDS6iQrEO
QVUdlYRlsYNus7U31Tfg7o2Zr/eUpbzLIWHhXkMvkmtn/6YLIjoVO9TIDL6VdUE2
xRLPCfDsppKJtJc3QwKAOVZMagyDoz7lwSrIBwAgbEhHj35c9i/Bc=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-0.9 required=5.0 tests=BAYES_00,KAM_LAZY_DOMAIN_SECURITY,RCVD_IN_DNSWL_NONE,T_RP_MATCHES_RCVD autolearn=no version=3.3.2 spammy=HX-Envelope-From:sk:Christi, reserves, H*UA:2.51
X-HELO: mailout05.t-online.de
Subject: Re: calloc speed difference
To: cygwin AT cygwin DOT com
References: <CAD8GWsvxzFe0dPnxO-odTY+EG5XOAApLjcOSN5d7vXhtLW0GmQ AT mail DOT gmail DOT com>
From: Christian Franke <Christian DOT Franke AT t-online DOT de>
Message-ID: <46515148-9f8e-6eae-69f9-9bf20921097a@t-online.de>
Date: Fri, 12 Jan 2018 15:06:17 +0100
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0 SeaMonkey/2.51
MIME-Version: 1.0
In-Reply-To: <CAD8GWsvxzFe0dPnxO-odTY+EG5XOAApLjcOSN5d7vXhtLW0GmQ@mail.gmail.com>
X-IsSubscribed: yes

Lee wrote:
> Why is the cygwin gcc calloc so much slower than the
> i686-w64-mingw32-gcc calloc?
>    1:12 vs 0:11
>
> $cat calloc-test.c
> #include <stdio.h>
> #include <stdlib.h>
> #define ALLOCATION_SIZE (100 * 1024 * 1024)
> int main (int argc, char *argv[]) {
>      for (int i = 0; i < 10000; i++) {
>          void *temp = calloc(ALLOCATION_SIZE, 1);
>          if ( temp == NULL ) {
>             printf("drat! calloc returned NULL\n");
>             return 1;
>          }
>          free(temp);
>      }
>      return 0;
> }
>

Could reproduce the difference on an older i7-2600K machine:

Cygwin: ~20s
MinGW: ~4s

Timing [cm]alloc() calls without actually using the allocated memory 
might produce misleading results due to lazy page allocation and/or 
zero-filling.

MinGW binaries use calloc() from msvcrt.dll. This calloc() does not call 
malloc() and then memset(). It directly calls:

   mem = HeapAlloc(_crtheap, HEAP_ZERO_MEMORY, size);

which possibly only reserves allocate-and-zero-fill-on-demand pages for 
later.

Cygwin's calloc() is different.

This variant of the above code adds one write access to each 4KiB page 
(guarded by "volatile" to prevent dead assignment optimization):

#include <stdio.h>
#include <stdlib.h>
#define ALLOCATION_SIZE (100 * 1024 * 1024)
int main (int argc, char *argv[]) {
     for (int i = 0; i < 1000; i++) {
         void *temp = calloc(ALLOCATION_SIZE, 1);
         if ( temp == NULL ) {
            printf("drat! calloc returned NULL\n");
            return 1;
         }
         for (int j = 0; j < ALLOCATION_SIZE; j += 4096)
           ((volatile char *)temp)[j] = (char)i;
         free(temp);
     }
     return 0;
}

Results:

Cygwin: ~310s
MinGW: ~210s

Christian


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019