Skip to contentSkip to conversation

Add custom memory allocator to MinGW and MacOS builds

PATCH In review Marius Storm-Olsen 6 participants 10 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add custom memory allocator to MinGW and MacOS builds

The standard allocator on Windows is pretty bad prior to Windows Vista, and nedmalloc is better than the modified dlmalloc provided with newer versions of the MinGW libc.

Note that both Windows and MacOSX need to have an implementation of strdup, which I added at the end of nedmalloc.c, to ensure that strdup use the same heap. So, setting USE_NED_ALLOCATION = YesPlease will also add this function reimplementation, unless you define NEDMALLOC_DONT_OVERRIDE_STRDUP.

NedMalloc stats in Git
----------------------
All results are the best result out of 3 runs. The benchmarks have been done on different hardware, so the repack times are not comparable.

These benchmarks are all based on 'git repack -adf' on the Linux kernel.

XP
-----------------------------------------------
MinGW Threads Total Time Speed
-----------------------------------------------

  3.4.2                  (1T)  00:12:28.422
  3.4.2     + nedmalloc  (1T)  00:07:25.437 1.68x
  3.4.5                  (1T)  00:12:20.718
  3.4.5     + nedmalloc  (1T)  00:07:24.809 1.67x
  4.3.3-tdm              (1T)  00:12:01.843
  4.3.3-tdm + nedmalloc  (1T)  00:07:16.468 1.65x
  4.3.3-tdm              (2T)  00:07:35.062
  4.3.3-tdm + nedmalloc  (2T)  00:04:57.874 1.54x

Vista
-----------------------------------------------
MinGW Threads Total Time Speed
-----------------------------------------------

  4.3.3-tdm              (1T)  00:07:40.844
  4.3.3-tdm + nedmalloc  (1T)  00:07:17.548 1.05x
  4.3.3-tdm              (2T)  00:05:33.746
  4.3.3-tdm + nedmalloc  (2T)  00:05:27.334 1.02x

Mac Mini
-----------------------------------------------
GCC Threads Total Time Speed
-----------------------------------------------
i686-darwin9-4.0.1 (2T) 00:09:57.346
i686-darwin9-4.0.1+ned (2T) 00:08:51.072 1.12x

Linux (Kubuntu 8.10)
-----------------------------------------------
Same HW as Vista above (2T) 00:03:34.233
Adding nedmalloc made git segfault, but given
the standard performance I didn't even care to
investigate.

---
 Makefile                     |   10 +
 compat/nedmalloc/License.txt |   23 +
 compat/nedmalloc/Readme.txt  |  136 +
 compat/nedmalloc/malloc.c.h  | 5750 ++++++++++++++++++++++++++++++++++++++++++
 compat/nedmalloc/nedmalloc.c |  966 +++++++
 compat/nedmalloc/nedmalloc.h |  180 ++
 6 files changed, 7065 insertions(+), 0 deletions(-)
 create mode 100644 compat/nedmalloc/License.txt
 create mode 100644 compat/nedmalloc/Readme.txt
 create mode 100644 compat/nedmalloc/malloc.c.h
 create mode 100644 compat/nedmalloc/nedmalloc.c
 create mode 100644 compat/nedmalloc/nedmalloc.h
Makefile +10 -0
index 7867eac..6e88f2b 100644
@@ -165,6 +165,9 @@ all::
 # Define NO_EXTERNAL_GREP if you don't want "git grep" to ever call
 # your external grep (e.g., if your system lacks grep, if its grep is
 # broken, or spawning external process is slower than built-in grep git has).
+#
+# Define USE_NED_ALLOCATOR if you want to replace the platforms default
+# memory allocators with the nedmalloc allocator written by Niall Douglas.
 
 GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE
 	@$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -676,6 +679,7 @@ ifeq ($(uname_S),Darwin)
 	NO_MEMMEM = YesPlease
 	THREADED_DELTA_SEARCH = YesPlease
 	USE_ST_TIMESPEC = YesPlease
+	USE_NED_ALLOCATOR = YesPlease
 endif
 ifeq ($(uname_S),SunOS)
 	NEEDS_SOCKET = YesPlease
@@ -821,6 +825,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
 	NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
 	NO_NSEC = YesPlease
 	USE_WIN32_MMAP = YesPlease
+	USE_NED_ALLOCATOR = YesPlease
 	COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -DNOGDI -Icompat -Icompat/regex -Icompat/fnmatch
 	COMPAT_CFLAGS += -DSNPRINTF_SIZE_CORR=1
 	COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\"
@@ -1100,6 +1105,11 @@ ifdef NO_EXTERNAL_GREP
 	BASIC_CFLAGS += -DNO_EXTERNAL_GREP
 endif
 
+ifdef USE_NED_ALLOCATOR
+       COMPAT_CFLAGS += -DUSE_NED_ALLOCATOR -DNDEBUG -DREPLACE_SYSTEM_ALLOCATOR -Icompat/nedmalloc
+       COMPAT_OBJS += compat/nedmalloc/nedmalloc.o
+endif
+
 ifeq ($(TCLTK_PATH),)
 NO_TCLTK=NoThanks
 endif
compat/nedmalloc/License.txt new file +23 -0
new file mode 100644
index 0000000..36b7cd9
@@ -0,0 +1,23 @@
+Boost Software License - Version 1.0 - August 17th, 2003
+
+Permission is hereby granted, free of charge, to any person or organization
+obtaining a copy of the software and accompanying documentation covered by
+this license (the "Software") to use, reproduce, display, distribute,
+execute, and transmit the Software, and to prepare derivative works of the
+Software, and to permit third-parties to whom the Software is furnished to
+do so, all subject to the following:
+
+The copyright notices in the Software and this entire statement, including
+the above license grant, this restriction and the following disclaimer,
+must be included in all copies of the Software, in whole or in part, and
+all derivative works of the Software, unless such copies or derivative
+works are solely in the form of machine-executable object code generated by
+a source language processor.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
+SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
+FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
compat/nedmalloc/Readme.txt new file +136 -0

136 lines not on this page · raw message

compat/nedmalloc/malloc.c.h new file +5750 -0

2,807 lines not on this page · raw message

compat/nedmalloc/nedmalloc.c new file +966 -0

966 lines not on this page · raw message

compat/nedmalloc/nedmalloc.h new file +180 -0

180 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.6.2.1.470.g56b66.dirty

Part of the diff is not shown here. View the raw message.

#1 lore raw

Marius Storm-Olsen said the following on 03.04.2009 15:52:

Quoted 4 lines

The standard allocator on Windows is pretty bad prior to Windows Vista, and nedmalloc is better than the modified dlmalloc provided with newer versions of the MinGW libc.

Actually, it just struck me that it's probably the synchronization primitives which are better on Vista than XP, and not the memory manager? (Since mingw 4.3.3-tdm on XP and Vista most likely use the same dlmalloc fork?) ^shrug^

Anyways, not that I haven't tried to 'tune' nedmalloc in any way, just ensured that it compiles with the different MinGWs which I benchmarked. So, if anyone feels like it, maybe we can squeeze more performance out of it by tweaking it.

--
.marius

#2 lore raw

2009/4/3 Marius Storm-Olsen <[email protected]>:

Quoted 12 lines

Marius Storm-Olsen said the following on 03.04.2009 15:52:

Quoted 4 lines

The standard allocator on Windows is pretty bad prior to Windows Vista, and nedmalloc is better than the modified dlmalloc provided with newer versions of the MinGW libc.

Actually, it just struck me that it's probably the synchronization primitives which are better on Vista than XP, and not the memory manager? (Since mingw 4.3.3-tdm on XP and Vista most likely use the same dlmalloc fork?) ^shrug^

Quoted 4 lines

Anyways, not that I haven't tried to 'tune' nedmalloc in any way, just ensured that it compiles with the different MinGWs which I benchmarked. So, if anyone feels like it, maybe we can squeeze more performance out of it by tweaking it.

The difference on Windows Vista is that the low fragmentation heap is the default memory allocator. On Windows XP you need to enable it specifically for an application. So a possible alternative to this is just to enable the low fragmentation heap. (done via GetProcessHeaps and HeapSetInformation Win32 API calls).

Pat Thoyts

#3 lore raw

Pat Thoyts said the following on 03.04.2009 23:12:

Quoted 14 lines

2009/4/3 Marius Storm-Olsen <[email protected]>:

Quoted 4 lines

Marius Storm-Olsen said the following on 03.04.2009 15:52:

Quoted 3 lines

The standard allocator on Windows is pretty bad prior to Windows Vista, and nedmalloc is better than the modified dlmalloc provided with newer versions of the MinGW libc.

Quoted 8 lines

Actually, it just struck me that it's probably the synchronization primitives which are better on Vista than XP, and not the memory manager? (Since mingw 4.3.3-tdm on XP and Vista most likely use the same dlmalloc fork?) ^shrug^

Anyways, not that I haven't tried to 'tune' nedmalloc in any way,
just ensured that it compiles with the different MinGWs which I benchmarked. So, if anyone feels like it, maybe we can squeeze more performance out of it by tweaking it.

Quoted 5 lines

The difference on Windows Vista is that the low fragmentation heap is the default memory allocator. On Windows XP you need to enable it specifically for an application. So a possible alternative to this is just to enable the low fragmentation heap. (done via GetProcessHeaps and HeapSetInformation Win32 API calls).

I know about the low-fragmentation heap, but given that it was only supported on XP and up (and given that I also had MacOSX in mind when considering a custom allocator; see MacOSX got 12% itself ;-), I didn't even consider it. Thanks for clearing up the differences on the Vista and XP benchmarks though! Makes sense.

--
.marius

#4 lore raw
Quoted 7 lines · Marius Storm-Olsen

Pat Thoyts said the following on 03.04.2009 23:12:

Quoted 5 lines

The difference on Windows Vista is that the low fragmentation heap is the default memory allocator. On Windows XP you need to enable it specifically for an application. So a possible alternative to this is just to enable the low fragmentation heap. (done via GetProcessHeaps and HeapSetInformation Win32 API calls).

Quoted 6 lines

I know about the low-fragmentation heap, but given that it was only supported on XP and up (and given that I also had MacOSX in mind when considering a custom allocator; see MacOSX got 12% itself ;-), I didn't even consider it.
Thanks for clearing up the differences on the Vista and XP benchmarks though! Makes sense.

Wouldn't a GetProcessHeaps/HeapSetInformation solution add much less code, even with a runtime check whether the feature is supported?

The improvement that you observed is in a rather special area (repack). How is the improvment in day-to-day tools:

- procelains used on command line: git-status, git-add, git-commit, git-diff, git-log, perhaps even local git-fetch.

- plumbing used by guis: git-diff-files, git-diff-tree, git-log, git-rev-parse

- I'm not even mentioning git-am, git-rebase, because here the time sink is the fork emulation.

I doubt that the improvement is equally great, and it will perhaps vanish in the noise. 7000+ LOC is a bit much in this case, don't you think so?

BTW, I assume that the Boost license is compatible with GPL. But did you check that?

-- Hannes

#5 lore raw

Johannes Sixt said the following on 04.04.2009 08:35:

Quoted 14 lines · Marius Storm-Olsen
Quoted 7 lines

Pat Thoyts said the following on 03.04.2009 23:12:

Quoted 6 lines

The difference on Windows Vista is that the low fragmentation heap is the default memory allocator. On Windows XP you need to enable it specifically for an application. So a possible alternative to this is just to enable the low fragmentation heap. (done via GetProcessHeaps and HeapSetInformation Win32 API calls).

Quoted 5 lines

I know about the low-fragmentation heap, but given that it was only supported on XP and up (and given that I also had MacOSX in mind when considering a custom allocator; see MacOSX got 12% itself ;-), I didn't even consider it. Thanks for clearing up the differences on the Vista and XP benchmarks though! Makes sense.

Quoted 3 lines

Wouldn't a GetProcessHeaps/HeapSetInformation solution add much less code, even with a runtime check whether the feature is supported?

It certainly would, if you'd also like to simply ignore the extra benefit to MacOSX, which was a not-so-bad additional 12%. However, I and several of my colleagues also use Git on Mac and see that any improvement in the performance there would also be welcome. So, I went with the custom allocator approach, instead of just looking into XP.

There might also be other platforms which could benefit from such a custom allocator, so I figured that there were many positive sides to this, rather than just going for the Low Fragmentation Heap on Windows.

Quoted 5 lines

The improvement that you observed is in a rather special area (repack). How is the improvment in day-to-day tools:

- procelains used on command line: git-status, git-add, git-commit, git-diff, git-log, perhaps even local git-fetch.

Quoted 7 lines

- plumbing used by guis: git-diff-files, git-diff-tree, git-log, git-rev-parse

- I'm not even mentioning git-am, git-rebase, because here the time sink is the fork emulation.

I doubt that the improvement is equally great, and it will perhaps vanish in the noise. 7000+ LOC is a bit much in this case, don't you think so?

I went with repack because it's a lot of data munging, and not so high IO. Clearly more I/O intensive git operations would not benefit as much as repack. But the goal in this patch was not to speed up I/O.

Obviously there are things that can be, and should be done for the I/O side too, but that's a separate subject.

I don't see the 7000+ LOC as such a big deal, given that they are all neatly tucked away in a compat subdirectory. They don't even add any additional sourcecode to the codepaths, since you just link with it.

Given the benefits, even 5% better than the Low-Frag case for single-threaded cases (which is the most dominant in git anyways), it think it's reason enough to include it. The 12% boost on Mac should also underline this.

Running 'git blame' on one of the files in my repos gives me this result:
XP
Without nedmalloc: 11.218sec
With nedmalloc : 9.514sec (18% speedup)
OSX
Without nedmalloc: 15.046sec
With nedmalloc : 13.957sec (8% speedup)

I'll take those speedups any day :-)

Quoted 1 line

BTW, I assume that the Boost license is compatible with GPL. But did you check that?

Of course I did, you'll find it under
http://www.fsf.org/licensing/licenses/index_html#GPLCompatibleLicenses

--
.marius

#6 lore raw
In reply to Pat Thoyts

Hi,

Quoted 17 lines · Pat Thoyts

2009/4/3 Marius Storm-Olsen <[email protected]>:

Quoted 11 lines

Marius Storm-Olsen said the following on 03.04.2009 15:52:

Quoted 3 lines

The standard allocator on Windows is pretty bad prior to Windows Vista, and nedmalloc is better than the modified dlmalloc provided with newer versions of the MinGW libc.

Actually, it just struck me that it's probably the synchronization primitives which are better on Vista than XP, and not the memory manager? (Since mingw 4.3.3-tdm on XP and Vista most likely use the same dlmalloc fork?) ^shrug^

Quoted 4 lines

Anyways, not that I haven't tried to 'tune' nedmalloc in any way, just ensured that it compiles with the different MinGWs which I benchmarked. So, if anyone feels like it, maybe we can squeeze more performance out of it by tweaking it.

Quoted 5 lines

The difference on Windows Vista is that the low fragmentation heap is the default memory allocator. On Windows XP you need to enable it specifically for an application. So a possible alternative to this is just to enable the low fragmentation heap. (done via GetProcessHeaps and HeapSetInformation Win32 API calls).

Does this also work on NT?

Ciao,
Dscho

#7 lore raw

2009/4/3 Johannes Schindelin <[email protected]>:

Quoted 1 line · Pat Thoyts

[snip]

Quoted 10 lines
Quoted 5 lines

The difference on Windows Vista is that the low fragmentation heap is the default memory allocator. On Windows XP you need to enable it specifically for an application. So a possible alternative to this is just to enable the low fragmentation heap. (done via GetProcessHeaps and HeapSetInformation Win32 API calls).

Does this also work on NT?

Ciao,
Dscho

No - the low fragmentation heap was added as an option to Windows XP and became the default heap in Vista. XP or Server 2003 are documentated as the lowest versions supporting this on MSDN.

Pat Thoyts

#8 lore raw
In reply to Marius Storm-Olsen

Hi,

Quoted 26 lines · Marius Storm-Olsen

The standard allocator on Windows is pretty bad prior to Windows Vista, and nedmalloc is better than the modified dlmalloc provided with newer versions of the MinGW libc.

Note that both Windows and MacOSX need to have an implementation of strdup, which I added at the end of nedmalloc.c, to ensure that strdup use the same heap. So, setting USE_NED_ALLOCATION = YesPlease will also add this function reimplementation, unless you define NEDMALLOC_DONT_OVERRIDE_STRDUP.

NedMalloc stats in Git
----------------------
All results are the best result out of 3 runs. The benchmarks have been done on different hardware, so the repack times are not comparable.

These benchmarks are all based on 'git repack -adf' on the Linux kernel.

XP
-----------------------------------------------
MinGW Threads Total Time Speed
-----------------------------------------------

  3.4.2                  (1T)  00:12:28.422
  3.4.2     + nedmalloc  (1T)  00:07:25.437 1.68x
Quoted 8 lines
  3.4.5                  (1T)  00:12:20.718
  3.4.5     + nedmalloc  (1T)  00:07:24.809 1.67x
  4.3.3-tdm              (1T)  00:12:01.843
  4.3.3-tdm + nedmalloc  (1T)  00:07:16.468 1.65x
  4.3.3-tdm              (2T)  00:07:35.062
  4.3.3-tdm + nedmalloc  (2T)  00:04:57.874 1.54x

Wow! That is impressive.

Quoted 7 lines
 Makefile                     |   10 +
 compat/nedmalloc/License.txt |   23 +
 compat/nedmalloc/Readme.txt  |  136 +
 compat/nedmalloc/malloc.c.h  | 5750 ++++++++++++++++++++++++++++++++++++++++++
 compat/nedmalloc/nedmalloc.c |  966 +++++++
 compat/nedmalloc/nedmalloc.h |  180 ++
 6 files changed, 7065 insertions(+), 0 deletions(-)

However, this is impressive, too ;-)

Ciao,
Dscho

#9 lore raw
Quoted 35 lines

Hi,

Quoted 29 lines · Marius Storm-Olsen

The standard allocator on Windows is pretty bad prior to Windows Vista, and nedmalloc is better than the modified dlmalloc provided with newer versions of the MinGW libc.

Note that both Windows and MacOSX need to have an implementation of strdup, which I added at the end of nedmalloc.c, to ensure that strdup use the same heap. So, setting USE_NED_ALLOCATION = YesPlease will also add this function reimplementation, unless you define NEDMALLOC_DONT_OVERRIDE_STRDUP.

NedMalloc stats in Git
----------------------
  All results are the best result out of 3 runs. The benchmarks have been done on different hardware, so the repack times are not comparable.

  These benchmarks are all based on 'git repack -adf' on the Linux kernel.

  XP
  -----------------------------------------------
  MinGW               Threads  Total Time   Speed
  -----------------------------------------------
  3.4.2                  (1T)  00:12:28.422
  3.4.2     + nedmalloc  (1T)  00:07:25.437 1.68x

  3.4.5                  (1T)  00:12:20.718
  3.4.5     + nedmalloc  (1T)  00:07:24.809 1.67x

Quoted 7 lines
Quoted 5 lines

  4.3.3-tdm              (1T)  00:12:01.843
  4.3.3-tdm + nedmalloc  (1T)  00:07:16.468 1.65x

  4.3.3-tdm              (2T)  00:07:35.062
  4.3.3-tdm + nedmalloc  (2T)  00:04:57.874 1.54x

Wow!  That is impressive.

indeed, haven't time to test it myself currently tho :-( i hope someone can confirm this massive performance boost and that this patch makes it into msysgit very soon. thanks for the work :-)