All Projects → faustodavid → Listpool

faustodavid / Listpool

Licence: mit
Optimized allocation free implementation of IList using ArrayPool.

Projects that are alternatives of or similar to Listpool

Movies For Hackers
🎬 A curated list of movies every hacker & cyberpunk must watch.
Stars: ✭ 8,884 (+35436%)
Mutual labels:  collection, list
Medical Datasets
tracking medical datasets, with a focus on medical imaging
Stars: ✭ 296 (+1084%)
Mutual labels:  collection, list
Awesome Zsh Plugins
A collection of ZSH frameworks, plugins, themes and tutorials.
Stars: ✭ 10,129 (+40416%)
Mutual labels:  collection, list
Web Launch Checklist
📋 A simple website launch checklist to keep track of the most important enrichment possibilities for a website.
Stars: ✭ 214 (+756%)
Mutual labels:  list, performance
30 Seconds Of Swift Code
A Swift implementation of 30-seconds-of-code: A curated collection of useful Swift 4 snippets that you can understand in 30 seconds or less.
Stars: ✭ 476 (+1804%)
Mutual labels:  collection, list
Containers
This library provides various containers. Each container has utility functions to manipulate the data it holds. This is an abstraction as to not have to manually manage and reallocate memory.
Stars: ✭ 125 (+400%)
Mutual labels:  collection, list
NonEmptyCollections
A type-safe implementation for collections that cannot be empty. Life is too short for emptiness-checks!
Stars: ✭ 45 (+80%)
Mutual labels:  list, collection
Cryptolist
Curated collection of blockchain & cryptocurrency resources.
Stars: ✭ 3,501 (+13904%)
Mutual labels:  collection, list
React Virtualized
React components for efficiently rendering large lists and tabular data
Stars: ✭ 22,963 (+91752%)
Mutual labels:  list, performance
Awesome Css Frameworks
List of awesome CSS frameworks
Stars: ✭ 4,410 (+17540%)
Mutual labels:  collection, list
Vue Virtual Collection
Vue component for efficiently rendering large collection data
Stars: ✭ 506 (+1924%)
Mutual labels:  collection, performance
Bytebuf
Example of how CL133375 can be utilized to mitigate Go escape analysis limitations.
Stars: ✭ 494 (+1876%)
Mutual labels:  buffer, performance
React Virtual List
Super simple virtualized list React component
Stars: ✭ 597 (+2288%)
Mutual labels:  list, performance
Ppel
The Python Practical Examples List. REAL projects for beginners.
Stars: ✭ 17 (-32%)
Mutual labels:  list
Lazycache
An easy to use thread safe in-memory caching service with a simple developer friendly API for c#
Stars: ✭ 901 (+3504%)
Mutual labels:  performance
Werelogs
A logging library providing efficient raw logging in the form of JSON data.
Stars: ✭ 16 (-36%)
Mutual labels:  performance
Qq Music List To Kwl
导出qq音乐歌单的小工具,导出格式为.kwl,可以把.kwl导入到网易云、酷我里面去。
Stars: ✭ 16 (-36%)
Mutual labels:  list
Liblist
Generic Linked list Management Library in C
Stars: ✭ 22 (-12%)
Mutual labels:  list
Node Servertiming
📊 Generate Server-Timing headers interactively in NodeJS
Stars: ✭ 19 (-24%)
Mutual labels:  performance
Laravel Bootstrap Table List
Bootstrap table list generator for Laravel.
Stars: ✭ 16 (-36%)
Mutual labels:  list

ListPool

Allocation-free implementation of IList<T> using ArrayPool with two variants, ListPool<T> and ValueListPool<T>

GitHub Workflow Status Coveralls github Nuget Nuget GitHub

Installation

Available on nuget

PM> Install-Package ListPool

Requirements:

  • System.Memory (>= 4.5.3)

Introduction

When performance matter, ListPool provides all the goodness of ArrayPool with the usability of IList<T>, support for Span<T> and serialization.

It has two high-performance variants ListPool<T> and ValueListPool<T>.

We recommend to use ListPool<T> over ValueListPool<T> for most of use-cases. You should use ValueListPool<T> when working with small collections of primitive types with stackalloc, or when reusing arrays.

Differences:

  • ListPool<T>:

    • ReferenceType
    • Serializable
    • Because it is a class it has a constant heap allocation of ~56 bytes regardless the size
  • ValueListPool<T>:

    • Stack-only (It is a ref struct)
    • Allocation-free
    • Can be created using stackalloc or an array as initial buffer
    • Cannot be serialized/deserialized
    • Because it is ValueType when it is passed to other methods, it is passed by copy, not by reference. In case it is required to be updated, it is required to use the "ref" modifier in the parameter.

Benchmarks

To see all the benchmarks and details, please click the following link https://github.com/faustodavid/ListPool/tree/main/perf/docs/results

Inserting an item in the middle of the list

You can observe that ListPool<T> Mean is faster and it does not allocate in the heap when resizing. Zero heap allocation is vital to improve throughput by reducing "GC Wait" time.

Create list indicating the capacity, adding N items and performing a foreach

By indicating the capacity, we avoid regrowing, which is one of the slowest operations for List<T>, so we can pay more attention to already well-optimized scenarios by improving the Add and Enumeration time. As you can observe, ListPool<T> Mean is faster and has 40 bytes of heap allocations, which are used to create the class.

Doing a foreach in a list of N size.

ListPool enumeration is way faster than List for small and large sizes.

How to use

ListPool<T> and ValueListPool<T> implement IDisposable. After finishing their use, you must dispose the list.

Examples

Deserialization:

static async Task Main()
{
   var httpClient = HttpClientFactory.Create();
   var stream = await httpClient.GetStreamAsync("examplePath");
   using var examples = await JsonSerializer.DeserializeAsync<ListPool<string>>(stream); 
   ...
}

Mapping domain object to dto:

Note: ListPool<T> is not been dispose at MapToResult. It is dispose at the caller.

static void Main()
{
  using ListPool<Example> examples = new GetAllExamplesUseCase().Query();
  using ListPool<ExampleResult> exampleResults = MapToResult(examples); 
  ...
}

public static ListPool<ExampleResult> MapToResult(IReadOnlyCollection<Example> examples)
{
  ListPool<ExampleResult> examplesResult = new ListPool<ExampleResult>(examples.Count);
  foreach (var example in examples)
  {
      examplesResult.Add(new ExampleResult(example));
  }

  return examplesResult;
}

Mapping a domain object to dto using LINQ (It perform slower than with foreach):

static void Main()
{
  using ListPool<Example> examples = new GetAllExamplesUseCase().Query();
  using ListPool<ExampleResult> examplesResult = examples.Select(example => new ExampleResult(example)).ToListPool();
  ...
}

Updating ValueListPool in other methods:

Note: The use of ref is required for ValueListPool<T> when it is updated in other methods because it is a ValueType. ListPool<T> does not require it.

static void Main()
{
  Span<int> initialBuffer = stackalloc int[500];
  ValueListPool<int> numbers = new ValueListPool<int>(initialBuffer, ValueListPool<int>.SourceType.UseAsInitialBuffer)
  for(int i; i < 500; i++)
  {
      numbers.Add(i);
  }

  AddNumbers(ref numbers);
  ...
  numbers.Dispose();
}

static void AddNumbers(ref ValueListPool<int> numbers)
{
  numbers.Add(1);
  numbers.Add(2);
}

Contributors

A big thanks to the project contributors!

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].