microsoft / mimalloc

mimalloc is a compact general purpose allocator with excellent performance.
MIT License
10.4k stars 840 forks source link

Mimalloc with vcpkg on Windows #575

Open F-I-D-O opened 2 years ago

F-I-D-O commented 2 years ago

Hi, I was able to integrate mimalloc into my project, with great results: before the parallel execution was negated by the allocation, now with mimalloc, the speedup is almost linear.

However, I spent quite some time with the integration. Compared with other libraries, which I integrated by just installing them to vcpkg, the mimalloc did not work that way. There is a package mimalloc on vcpkg, but it obviously does not work as expected, even with the override component. In the end, I just gave up and build mimalloc manually using the solution in the mimalloc project, linked it from CMake with an absolute path, and manually copied the dll to my project.

I just wonder, is there some principal problem with the vcpkg/CMake integration on Windows or can I fix it with just a little effort?

RT2Code commented 2 years ago

Hello,

The vcpkg mimalloc port uses CMake to build on every platform. This leads to some issues on Windows like dynamic overriding being unavailable because it requires to build mimalloc from the VS solution to get the necessary dlls files. I also have crashes in debug mode on Windows when I include "mimalloc-new-delete.h" which disappears if I use the libraries built from the mimalloc VS solutions instead. I suppose that's why the documentation recommend to use the VS solution to build mimalloc for Windows, and CMake to build on every other platform.

[Edit] : After more tests and having studied mimalloc more extensively, I now understand that I had got this part wrong and that there's no problem about using CMake to build mimalloc on Windows. But the question still stands : Is there any advantage on Windows to build mimalloc from the VS solution? It's important because using only CMake to build mimalloc allows to keep things as simple as possible in the vcpkg port.

@daanx I'm working on an improvement of the current vcpkg port to fix some issues and add features, but first I need to be sure I understand perfectly how to build and use mimalloc. I carefully read the documentation and many issues, and I made a list to summarize the different ways to build and use mimalloc. Can you review it to check there's no mistakes and to answer some questions I still have?

Direct API use

Static override with mimalloc-override.h (all platforms)

Static linking override (Unix systems only)

Dynamic override (recommended)

Extra Features

Visual Studio Solution

bencherian commented 1 year ago

@RT222 One issue I've found with the vcpkg mimalloc install is that because it builds everything as C++, it ends up using RtlGenRandom instead of BCryptGenRandom and causes occasional crashes in some versions of Windows as described in #623.

RT2Code commented 1 year ago

Which compiler are you using? Because even if the vcpkg port is indeed enforcing C++ everywhere (which is not ideal and should be fixed), mimalloc is by default configured to build with C++ on MSVC or the Intel compiler, so this should not be an issue on these.

bencherian commented 1 year ago

I'm using VS2019 with vcpkg. I guess this is a generic issue with CMake builds on Windows then. I don't think the VS solutions force C++ use. This is the offending line of code that enables use of RtlGenRandom instead of BCrypt when C++ is enabled.

RT2Code commented 1 year ago

I checked it and I found that the VS solutions are also configured to build with C++ : mimalloc_build_options

According to the comments in the file you mentioned, the use of RtlGenRandom is supposed to fix some issues with BCryptGenRandom, except on Windows Server 2019 where BCryptGenRandom is needed apparently. Are you using mimalloc on Windows Server 2019?

bencherian commented 1 year ago

Sorry, I should have been clearer. I'm using the mimalloc-override solution from VS which is set to "compile as default". I have a crash in certain versions of Win10 which is associated with this function. Based on that issue, it seems like using BCrypt is the "right" thing to do.

daanx commented 1 year ago

Hi -- improving vcpkg support would be great!

Some quick thoughts:

I'll follow up later with your other design document

skh1000 commented 1 year ago

@daanx or someone -- can someone do what's necessary to get vcpkg to have a (newest) version of mimalloc that works well on Windows Server 2019? I.e. that uses BcryptRandom instead of the Rtl version? I think it would be good if it works out of the box, with an appropriate vcpkg (manifest) version. Maybe have two flavors for each code version, or something.

skh1000 commented 1 year ago

Some suggestions: either a “feature” in the vcpkg manifest, or maybe even an environment variable that would switch what is used at runtime. You would then set this appropriately for windows server 2019, or any other windows os where it’s s problem.