SE-454: adopt mimalloc for Windows toolchain

Swift uses swift_alloc as the allocation function, however, the user is still permitted to replace malloc from the C/C++ side of the world. However, that is irrelevant here, this is about the compiler, not the runtime.

1 Like

Joe is correct; the runtime no longer uses operator new because of people trying to globally override it without appreciating just how early in process start-up the first allocation requests are likely to be. More particularly, we had a handful of problems where people wrote a custom allocator and initialised it in a static constructor, but that is too late and so their allocator was causing runtime crashes.

It's not entirely true to say that it's irrelevant, because the compiler these days contains Swift code, and so depends on the runtime. You are permitted to override malloc and indeed operator new, but the runtime itself will not necessarily use either of them (it doesn't use globally overridden operator new any more, for the reason given above).

1 Like

The custom malloc implementation that looks up UserDefaults inside malloc still haunts me

8 Likes

Yes, this whole tangent got started because it was pointed out that someone might want to point swift-inspect at a running instance of a compiler.

The compiler itself uses a number of different allocation strategies, including an arena allocator for some types. The Swift runtime eventually hands off to malloc through swift_slowAlloc as that is the system allocator. As long as we can iterate on the heap backing malloc, it is possible to use swift-inspect for the array introspection. However, that itself is not fully guaranteed - as an example, glibc does not provide an interface to iterate the heap. The non-memory inspection operations should work irrespective of the changes here.

It's well beyond the scope of this thread, but I think it would be interesting and within the purview of SwiftPM or the stdlib to introduce something conceptually similar to sysctl()[1] just for Swift that would let us query the state of the runtime the way that API lets us query the state of the OS.


  1. But, you know, not awful. ā†©ļøŽ

As another weekend aside, far too much of my career at the Big Operating System Company, LLC has been spent dealing with this "feature" of C++! Let's make sure never to accidentally introduce it into Swift by way of a "you can replace the guts of swift_alloc() (or whatever)" kind of feature.

1 Like

This proposal has been accepted.

2 Likes