All Projects → nxrighthere → Smmalloc Csharp

nxrighthere / Smmalloc Csharp

Licence: mit
Blazing fast memory allocator designed for video games meets .NET

Projects that are alternatives of or similar to Smmalloc Csharp

Netstack
Lightweight toolset for creating concurrent networking systems for multiplayer games
Stars: ✭ 157 (+93.83%)
Mutual labels:  multi-threading, gamedev, high-performance
Mofuw
mofuw is *MO*re *F*aster, *U*ltra minimal *W*ebserver.
Stars: ✭ 107 (+32.1%)
Mutual labels:  multi-threading, high-performance
Valvesockets Csharp
Managed C# abstraction of GameNetworkingSockets library by Valve Software
Stars: ✭ 273 (+237.04%)
Mutual labels:  gamedev, interop
Atomic queue
C++ lockless queue.
Stars: ✭ 373 (+360.49%)
Mutual labels:  multi-threading, high-performance
bitECS
Functional, minimal, data-oriented, ultra-high performance ECS library written in JavaScript
Stars: ✭ 372 (+359.26%)
Mutual labels:  gamedev, high-performance
Enet Csharp
Reliable UDP networking library
Stars: ✭ 464 (+472.84%)
Mutual labels:  gamedev, interop
Crail
[Archived] A Fast Multi-tiered Distributed Storage System based on User-Level I/O
Stars: ✭ 69 (-14.81%)
Mutual labels:  high-performance
Hyperhdr
Open source ambilight implementation for the audio and video stream on Windows 10 and Linux (x86 and Raspberry Pi). Primarily for use with the movie content on your TV and the LED strip. Includes support for HDR10 tone mapping correction for video processing and multi-threading for better performance.
Stars: ✭ 71 (-12.35%)
Mutual labels:  multi-threading
Fast Dat Parser
Superfast blockchain parser for stats
Stars: ✭ 68 (-16.05%)
Mutual labels:  high-performance
Tdp
The Darkest Pipeline - Multithreaded pipelines for modern C++
Stars: ✭ 67 (-17.28%)
Mutual labels:  multi-threading
Hugo Papermod
A fast, clean, responsive Hugo theme
Stars: ✭ 1,202 (+1383.95%)
Mutual labels:  high-performance
Rsf
已作为 Hasor 的子项目,迁移到:http://git.oschina.net/zycgit/hasor
Stars: ✭ 77 (-4.94%)
Mutual labels:  high-performance
Ezpygame
An easier way to use pygame
Stars: ✭ 72 (-11.11%)
Mutual labels:  gamedev
Lance
Multiplayer game server based on Node.JS
Stars: ✭ 1,161 (+1333.33%)
Mutual labels:  gamedev
Super Mega Engine
Mega Man GameMaker Studio 1.4.1760 Engine
Stars: ✭ 73 (-9.88%)
Mutual labels:  gamedev
Servicestack.text
.NET's fastest JSON, JSV and CSV Text Serializers
Stars: ✭ 1,157 (+1328.4%)
Mutual labels:  high-performance
Raylib Games
Collection of games made with raylib
Stars: ✭ 78 (-3.7%)
Mutual labels:  gamedev
A j simple hud
High Performance Display Overlay in iOS
Stars: ✭ 67 (-17.28%)
Mutual labels:  high-performance
Stdpack.c
Collection of small public domain de/compressors in plain C.
Stars: ✭ 73 (-9.88%)
Mutual labels:  gamedev
Delta
Programming language focused on performance and productivity
Stars: ✭ 77 (-4.94%)
Mutual labels:  high-performance

alt logo

PayPal Coinbase

This is an improved version of smmalloc a fast and efficient memory allocator designed to handle many small allocations/deallocations in heavy multi-threaded scenarios. The allocator created for usage in applications where the performance is critical such as video games.

Using smmalloc allocator in the .NET environment helps to minimize GC pressure for allocating buffers and avoid using lock-based pools in multi-threaded systems. Modern .NET features such as Span<T> greatly works in tandem with smmalloc and allows conveniently manage data in native memory blocks.

Building

To build the native library appropriate software is required:

For desktop platforms CMake with GNU Make or Visual Studio.

A managed assembly can be built using any available compiling platform that supports C# 3.0 or higher.

Usage

Create a new smmalloc instance:
// 8 buckets, 16 MB each, 128 bytes maximum allocation size
SmmallocInstance smmalloc = new SmmallocInstance(8, 16 * 1024 * 1024);
Destroy the smmalloc instance and free allocated memory:
smmalloc.Dispose();
Create thread cache for a current thread:
// 4 KB of thread cache for each bucket, hot warmup
smmalloc.CreateThreadCache(4 * 1024, CacheWarmupOptions.Hot);
Destroy thread cache for a current thread:
smmalloc.DestroyThreadCache();
Allocate memory block:
// 64 bytes of a memory block
IntPtr memory = smmalloc.Malloc(64);
Release memory block:
smmalloc.Free(memory);
Work with batches of memory blocks:
IntPtr[] batch = new IntPtr[32];

// Allocate a batch of memory
for (int i = 0; i < batch.Length; i++) {
	batch[i] = smmalloc.Malloc(64);
}

// Release the whole batch
smmalloc.Free(batch);
Write data to memory block:
// Using Marshal
byte data = 0;

for (int i = 0; i < smmalloc.Size(memory); i++) {
	Marshal.WriteByte(memory, i, data++);
}

// Using Span
Span<byte> buffer;

unsafe {
	buffer = new Span<byte>((byte*)memory, smmalloc.Size(memory));
}

byte data = 0;

for (int i = 0; i < buffer.Length; i++) {
	buffer[i] = data++;
}
Read data from memory block:
// Using Marshal
int sum = 0;

for (int i = 0; i < smmalloc.Size(memory); i++) {
	sum += Marshal.ReadByte(memory, i);
}

// Using Span
int sum = 0;

foreach (var value in buffer) {
	sum += value;
}
Hardware accelerated operations:
// Xor using Vector and Span
if (Vector.IsHardwareAccelerated) {
	Span<Vector<byte>> bufferVector = MemoryMarshal.Cast<byte, Vector<byte>>(buffer);
	Span<Vector<byte>> xorVector = MemoryMarshal.Cast<byte, Vector<byte>>(xor);

	for (int i = 0; i < bufferVector.Length; i++) {
		bufferVector[i] ^= xorVector[i];
	}
}
Copy data using memory block:
// Using Marshal
byte[] data = new byte[64];

// Copy from native memory
Marshal.Copy(memory, data, 0, 64);

// Copy to native memory
Marshal.Copy(data, 0, memory, 64);

// Using Buffer
unsafe {
	// Copy from native memory
	fixed (byte* destination = &data[0]) {
		Buffer.MemoryCopy((byte*)memory, destination, 64, 64);
	}

	// Copy to native memory
	fixed (byte* source = &data[0]) {
		Buffer.MemoryCopy(source, (byte*)memory, 64, 64);
	}
}
Custom data structures:
// Define a custom structure
struct Entity {
	public uint id;
	public byte health;
	public byte state;
}

int entitySize = Marshal.SizeOf(typeof(Entity));
int entityCount = 10;

// Allocate memory block
IntPtr memory = smmalloc.Malloc(entitySize * entityCount);

// Create Span using native memory block
Span<Entity> entities;

unsafe {
	entities = new Span<Entity>((void*)memory, entityCount);
}

// Do some stuff
uint id = 1;

for (int i = 0; i < entities.Length; i++) {
	entities[i].id = id++;
	entities[i].health = (byte)(new Random().Next(1, 100));
	entities[i].state = (byte)(new Random().Next(1, 255));
}

// Release memory block
smmalloc.Free(memory);

API reference

Enumerations

CacheWarmupOptions

Definitions of warmup options for CreateThreadCache() function:

CacheWarmupOptions.Cold warmup not performed for cache elements.

CacheWarmupOptions.Warm warmup performed for half of the cache elements.

CacheWarmupOptions.Hot warmup performed for all cache elements.

Classes

A single low-level disposable class is used to work with smmalloc.

SmmallocInstance

Contains a managed pointer to the smmalloc instance.

Constructors

SmmallocInstance(uint bucketsCount, int bucketSize) creates allocator instance with a memory pool. Size of memory blocks in each bucket increases with a count of buckets. The bucket size parameter sets an initial size of a pooled memory in bytes.

Methods

SmmallocInstance.Dispose() destroys the smmalloc instance and frees allocated memory.

SmmallocInstance.CreateThreadCache(int cacheSize, CacheWarmupOptions warmupOption) creates thread cache for fast memory allocations within a thread. The warmup option sets pre-allocation degree of cache elements.

SmmallocInstance.DestroyThreadCache() destroys the thread cache. Should be called before the end of the thread's life cycle.

SmmallocInstance.Malloc(int bytesCount, int alignment) allocates aligned memory block. Allocation size depends on buckets count multiplied by 16, so the minimum allocation size is 16 bytes. Maximum allocation size using two buckets in a smmalloc instance will be 32 bytes, for three buckets 48 bytes, for four 64 bytes, and so on. The alignment parameter is optional. Returns pointer to a memory block. Returns a pointer to an allocated memory block.

SmmallocInstance.Free(IntPtr memory) frees memory block. A managed array or pointer to pointers with length can be used instead of a pointer to memory block to free a batch of memory.

SmmallocInstance.Realloc(IntPtr memory, int bytesCount, int alignment) reallocates memory block. The alignment parameter is optional. Returns a pointer to a reallocated memory block.

SmmallocInstance.Size(IntPtr memory) gets usable memory size. Returns size in bytes.

SmmallocInstance.Bucket(IntPtr memory) gets bucket index of a memory block. Returns placement index.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].