Skip to content

Commit

Permalink
Add support for using mimalloc allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
MikePopoloski committed May 13, 2023
1 parent 7ed4718 commit 7a87635
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 22 deletions.
9 changes: 3 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ option(SLANG_FUZZ_TARGET "Enables changes to make binaries easier to fuzz test"
OFF)
option(BUILD_SHARED_LIBS "Generate shared libraries instead of static" OFF)
option(SLANG_USE_BOOST "Enable the use of Boost libraries" ON)
option(SLANG_USE_MIMALLOC "Enable use of the mimalloc library" ON)

set(SLANG_LIB_NAME
"svlang"
Expand Down Expand Up @@ -133,17 +134,13 @@ set(CMAKE_CXX_EXTENSIONS OFF)
# target_ specific commands.
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
add_compile_definitions(WIN32 _WINDOWS NTDDI_VERSION=0x06010000
_WIN32_WINNT=0x0601)
add_compile_definitions(UNICODE _UNICODE NOMINMAX WIN32_LEAN_AND_MEAN)
_WIN32_WINNT=0x0601 UNICODE _UNICODE)
add_compile_definitions(_SCL_SECURE_NO_WARNINGS _CRT_SECURE_NO_WARNINGS)
add_compile_definitions(_CRT_SECURE_NO_DEPRECATE _CRT_NONSTDC_NO_WARNINGS)

# Project won't build out of the box without this flag.
add_compile_options(/permissive-)
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
add_compile_options(/utf-8 /bigobj)
add_compile_options(/utf-8 /bigobj /permissive-)
endif()

# Get sane install RPATH handling by default if none has been provided.
Expand Down
1 change: 1 addition & 0 deletions docs/building.dox
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ SLANG_INCLUDE_PYLIB | Include Python bindings in the build | OFF
SLANG_INCLUDE_COVERAGE | Include code coverage targets in the build | OFF
BUILD_SHARED_LIBS | Build a shared library instead of static | OFF
SLANG_USE_BOOST | Enable the use of Boost libraries. If turned off, slower components from the C++ standard lib will be used instead. | ON
SLANG_USE_MIMALLOC | Enable use of the mimalloc library. Can be turned off, the resulting library will be slightly slower. | ON
SLANG_FUZZ_TARGET | Turn on to enable some changes to make binaries easier to fuzz test | OFF
SLANG_CI_BUILD | Enable additional longer-running tests for automated builds | OFF
SLANG_CLANG_TIDY | The path to a clang-tidy binary to run against the slang sources | ""
Expand Down
41 changes: 40 additions & 1 deletion external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@ if(SLANG_USE_BOOST)
endif()
endif()

if(SLANG_USE_MIMALLOC)
set(find_pkg_args "")
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24.0")
set(find_pkg_args "FIND_PACKAGE_ARGS" "2.1")
endif()

FetchContent_Declare(
mimalloc
GIT_REPOSITORY https://github.com/microsoft/mimalloc.git
GIT_TAG v2.1.2
GIT_SHALLOW ON
${find_pkg_args})
FetchContent_MakeAvailable(mimalloc)

if(mimalloc_FOUND)
message(STATUS "Found system mimalloc version: ${Catch2_VERSION}")
else()
message(STATUS "Using remote mimalloc library")
if(IS_DIRECTORY "${mimalloc_SOURCE_DIR}")
set_property(DIRECTORY ${mimalloc_SOURCE_DIR} PROPERTY EXCLUDE_FROM_ALL
YES)
endif()
endif()
endif()

if(SLANG_INCLUDE_TESTS)
set(find_pkg_args "")
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24.0")
Expand Down Expand Up @@ -100,7 +125,10 @@ if(SLANG_INCLUDE_INSTALL)
endif()

if(SLANG_USE_BOOST AND NOT Boost_FOUND)
install(TARGETS boost_headers EXPORT slangTargets)
install(
TARGETS boost_headers
EXPORT slangTargets
COMPONENT slang_Development)
get_target_property(boost_target_install_dirs boost_headers
boost_target_install_dir)
foreach(dir ${boost_target_install_dirs})
Expand All @@ -110,4 +138,15 @@ if(SLANG_INCLUDE_INSTALL)
COMPONENT slang_Development)
endforeach()
endif()

if(SLANG_USE_MIMALLOC AND NOT mimalloc_FOUND)
install(
TARGETS mimalloc-static
EXPORT slangTargets
COMPONENT slang_Development)
install(
DIRECTORY ${mimalloc_SOURCE_DIR}/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
COMPONENT slang_Development)
endif()
endif()
9 changes: 4 additions & 5 deletions include/slang/util/SmallVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace detail {

[[noreturn]] SLANG_EXPORT void throwOutOfRange();
[[noreturn]] SLANG_EXPORT void throwLengthError();
SLANG_EXPORT void* allocArray(size_t capacity, size_t typeSize);

} // namespace detail

Expand Down Expand Up @@ -471,7 +470,7 @@ class SmallVectorBase {
void cleanup() {
std::destroy(begin(), end());
if (!isSmall())
free(data_);
::operator delete(data_);
}

template<typename... Args>
Expand Down Expand Up @@ -505,7 +504,7 @@ class SmallVectorBase {
}

void reallocateTo(size_type newCapacity) {
auto newData = (pointer)detail::allocArray(newCapacity, sizeof(T));
auto newData = (pointer)::operator new(newCapacity * sizeof(T));
std::uninitialized_move(begin(), end(), newData);

cleanup();
Expand Down Expand Up @@ -803,7 +802,7 @@ typename SmallVectorBase<T>::pointer SmallVectorBase<T>::emplaceRealloc(const po

auto newCap = calculateGrowth(len + 1);
auto offset = static_cast<size_type>(pos - begin());
auto newData = (pointer)detail::allocArray(newCap, sizeof(T));
auto newData = (pointer)::operator new(newCap * sizeof(T));

// First construct the new element in the new memory,
// so that we don't corrupt the new element if it relied on
Expand Down Expand Up @@ -835,7 +834,7 @@ void SmallVectorBase<T>::resizeRealloc(size_type newSize, const TVal& val) {
detail::throwLengthError();

auto newCap = calculateGrowth(newSize);
auto newData = (pointer)detail::allocArray(newCap, sizeof(T));
auto newData = (pointer)::operator new(newCap * sizeof(T));

std::uninitialized_move(begin(), end(), newData);

Expand Down
5 changes: 5 additions & 0 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ if(SLANG_USE_BOOST)
target_compile_definitions(slang_slang PUBLIC SLANG_USE_BOOST)
endif()

if(SLANG_USE_MIMALLOC)
target_link_libraries(slang_slang PUBLIC mimalloc-static)
target_compile_definitions(slang_slang PUBLIC SLANG_USE_MIMALLOC)
endif()

# If building the Python library we'll end up with a shared lib no matter what,
# so make sure we always build with PIC.
if(SLANG_INCLUDE_PYLIB)
Expand Down
5 changes: 3 additions & 2 deletions source/util/BumpAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "slang/util/BumpAllocator.h"

#include <cstdlib>
#include <new>

namespace slang {

Expand All @@ -20,7 +21,7 @@ BumpAllocator::~BumpAllocator() {
Segment* seg = head;
while (seg) {
Segment* prev = seg->prev;
free(seg);
::operator delete(seg);
seg = prev;
}
}
Expand Down Expand Up @@ -64,7 +65,7 @@ byte* BumpAllocator::allocateSlow(size_t size, size_t alignment) {
}

BumpAllocator::Segment* BumpAllocator::allocSegment(Segment* prev, size_t size) {
auto seg = (Segment*)malloc(size);
auto seg = (Segment*)::operator new(size);
seg->prev = prev;
seg->current = (byte*)seg + sizeof(Segment);
return seg;
Expand Down
6 changes: 6 additions & 0 deletions source/util/OS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
#include "slang/util/OS.h"

#if defined(_MSC_VER)
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <Windows.h>
# include <fcntl.h>
# include <io.h>
Expand Down
8 changes: 0 additions & 8 deletions source/util/SmallVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,4 @@ void throwLengthError() {
SLANG_THROW(std::length_error("vector is at maximum size"));
}

void* allocArray(size_t capacity, size_t typeSize) {
void* result = std::malloc(capacity * typeSize);
if (!result)
SLANG_THROW(std::bad_alloc());

return result;
}

} // namespace slang::detail
6 changes: 6 additions & 0 deletions source/util/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
#include "slang/util/SmallVector.h"

#if defined(_MSC_VER)
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <Windows.h>
#endif

Expand Down
4 changes: 4 additions & 0 deletions source/util/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

#include <fmt/core.h>

#if defined(SLANG_USE_MIMALLOC) && defined(_WIN32)
# include <mimalloc-new-delete.h>
#endif

namespace slang::assert {

[[noreturn]] void assertFailed(const char* expr, const char* file, int line, const char* func) {
Expand Down

0 comments on commit 7a87635

Please sign in to comment.