Author SHA1 Message Date
Marek Lemanczyk b5a574355e Migrated to git.privacyguard.company 2026-09-08 22:49:08 +02:00
Marek Lemańczyk 4b17a4c827 Merge pull request #25 from mlemanczyk/codex/add-addrange-method-to-recyclablepriorityqueue
AddRange improvements for priority queue
2025-09-17 16:20:08 +02:00
Marek Lemańczyk 37f147a262 Fix priority queue tests compilation 2025-09-16 14:20:18 +02:00
Marek Lemańczyk b0e7f96496 Add memory-based AddRange overloads 2025-09-16 14:20:11 +02:00
Marek Lemańczyk f6640deaf1 Add IReadOnlyCollection AddRange overload 2025-09-16 13:52:42 +02:00
Marek Lemańczyk 050d7fd386 Add non-generic enumerable AddRange to priority queue 2025-09-16 13:52:36 +02:00
Marek Lemańczyk 33187c1053 Add dictionary overloads for priority queue 2025-09-16 13:52:29 +02:00
Marek Lemańczyk ff38aa302a Add more PriorityQueue AddRange overloads 2025-09-16 13:52:21 +02:00
Marek Lemańczyk 6d80b36d70 Implement standalone AddRange overloads 2025-09-16 13:52:15 +02:00
Marek Lemańczyk d8440fe8c9 Add AddRange overloads for priority queue 2025-09-16 13:52:10 +02:00
Marek Lemańczyk 67630e3797 Optimize AddRange copy 2025-07-29 17:07:17 +02:00
Marek Lemańczyk 8cc2e49ae4 Merge pull request #24 from mlemanczyk/codex/implementuj-metody-addrange-dla-recyclablelinkedlist
Expand LinkedList AddRange overloads
2025-07-28 22:19:32 +02:00
Marek Lemańczyk 4ea3df4cca Add SortedList and SortedDictionary support to AddRange 2025-07-28 22:05:14 +02:00
Marek Lemańczyk 9d1afe3e32 Implement specialized AddRange overloads 2025-07-28 21:43:50 +02:00
Marek Lemańczyk 1ee464d6b8 Merge pull request #23 from mlemanczyk/codex/add-addrange-method-to-recyclabledictionary
AddRange span overload for HashSet
2025-07-17 15:51:31 +02:00
Marek Lemańczyk 26f2920f33 Enhance HashSet AddRange with typed source detection 2025-07-17 15:37:04 +02:00
Marek Lemańczyk aa48407bba Add remaining AddRange overloads for HashSet 2025-07-17 15:36:59 +02:00
Marek Lemańczyk 3dea509958 Add additional AddRange overloads for hash set 2025-07-17 15:36:54 +02:00
Marek Lemańczyk 1dc6c479ae Add more AddRange overloads for RecyclableHashSet 2025-07-17 15:36:49 +02:00
Marek Lemańczyk 12b01c32ba Add more AddRange overloads for RecyclableHashSet 2025-07-17 15:36:44 +02:00
Marek Lemańczyk ea5c4eee28 Implement Span and array AddRange for HashSet 2025-07-17 15:36:39 +02:00
Marek Lemańczyk c24d8d54ea Add span AddRange for hash set 2025-07-17 08:34:21 +02:00
Marek Lemańczyk a178956761 Merge pull request #22 from mlemanczyk/codex/zaktualizuj-strukture-recyclablequeue-o-metody-addrange
Optimize list AddRange
2025-07-17 01:35:20 +02:00
Marek Lemańczyk 78e9373491 Optimize stack AddRange for sorted dictionary 2025-07-17 00:21:51 +02:00
Marek Lemańczyk 56a5c3375a Optimize sorted-list and read-only list AddRange 2025-07-17 00:21:45 +02:00
Marek Lemańczyk aacfed4e7e Optimize queue and dictionary AddRange overloads for stack 2025-07-17 00:21:40 +02:00
Marek Lemańczyk 414e482aaf Refine stack AddRange preallocation 2025-07-17 00:21:34 +02:00
Marek Lemańczyk 2546d6bf3f Optimize hash set and stack AddRange for RecyclableStack 2025-07-17 00:21:30 +02:00
Marek Lemańczyk 2de3736f94 Optimize span-based stack AddRange 2025-07-17 00:21:25 +02:00
Marek Lemańczyk 136c6408f3 Fix stack AddRange capacity growth 2025-07-17 00:21:20 +02:00
Marek Lemańczyk 1aedfb2cbc Optimize long-list AddRange for stack 2025-07-17 00:21:15 +02:00
Marek Lemańczyk 4274f4dcbc Fix stack enumerator for empty chunks 2025-07-16 00:20:25 +02:00
Marek Lemańczyk 7e71abdcf2 Merge pull request #21 from mlemanczyk/codex/update-recyclablestack-with-addrange-methods
AddRange performance tweaks for RecyclableStack
2025-07-15 00:46:49 +02:00
Marek Lemańczyk ffcbfb0493 Improve AddRange for ICollection 2025-07-15 00:40:06 +02:00
Marek Lemańczyk 037271acc8 Merge pull request #20 from mlemanczyk/d2kqjh-codex/dodaj-oznaczenia-wersjonowania-w-recyclablelonglist
Implement versioned enumerator retrieval
2025-07-14 23:13:48 +02:00
Marek Lemańczyk 25bf466f77 Optimize indexing helpers 2025-07-14 23:02:13 +02:00
Marek Lemańczyk c2affa4ad7 Implement versioned enumerator retrieval 2025-07-14 22:43:43 +02:00
Marek Lemańczyk a18caf42d6 Merge pull request #19 from mlemanczyk/codex/dodaj-oznaczenia-wersjonowania-w-recyclablelonglist
Add versioning to RecyclableLongList
2025-07-14 22:40:13 +02:00
Marek Lemańczyk 1e64c71af3 Add versioning support for RecyclableLongList 2025-07-14 22:01:03 +02:00
Marek Lemańczyk ed23fd7f69 Merge pull request #18 from mlemanczyk/codex/stworz-generator-kodu-zrodowego-w-c#
Mark version increments for source generation
2025-07-14 21:34:13 +02:00
Marek Lemańczyk 29a5db476e Mark version increments with directives and update generator 2025-07-14 21:28:52 +02:00
Marek Lemańczyk 1abc07e9a2 Merge pull request #17 from mlemanczyk/codex/add-optimized-addrange-methods
Add optimized AddRange overloads
2025-07-14 19:40:34 +02:00
Marek Lemańczyk dc0b3b92e5 Remove AddRange interface and optimize casting 2025-07-14 19:27:41 +02:00
Marek Lemańczyk b571a2df59 Use interface for AddRange optimizations 2025-07-14 19:27:33 +02:00
Marek Lemańczyk 1d4d5e9217 Avoid dynamic in AddRange detection 2025-07-14 19:27:25 +02:00
Marek Lemańczyk 976c5eb78b Add missing AddRange overloads for dictionary and sorted list 2025-07-14 18:17:37 +02:00
Marek Lemańczyk 40074ce368 Merge pull request #16 from mlemanczyk/codex/dodaj-przeciazenia-metod-addrange
Add optimized AddRange overloads
2025-07-14 17:46:21 +02:00
Marek Lemańczyk f4e2285734 Add optimized AddRange overloads for recyclable collections 2025-07-14 17:30:16 +02:00
Marek Lemańczyk 35f1124d6f Merge branch 'main' of https://github.com/mlemanczyk/Recyclable.Collections 2025-07-14 17:09:03 +02:00
Marek Lemańczyk 801ab88c01 Specification for agents updated. 2025-07-14 17:07:37 +02:00
Marek Lemańczyk e1298c817d Merge pull request #15 from mlemanczyk/codex/implement-recyclablesorteddictionary-class
Add RecyclableSortedDictionary
2025-07-14 16:31:41 +02:00
Marek Lemańczyk 66443480b7 Merge pull request #14 from mlemanczyk/codex/add-recyclablesortedlist-implementation-and-tests
Add recyclable sorted list
2025-07-14 16:31:14 +02:00
Marek Lemańczyk 941cf4f3fd Add RecyclableSortedDictionary with tests 2025-07-14 16:25:29 +02:00
Marek Lemańczyk 0d28f6515d Implement recyclable sorted list using pooled arrays 2025-07-14 16:24:24 +02:00
Marek Lemańczyk 0496b63e5a Specification for agents updated 2025-07-14 16:23:11 +02:00
Marek Lemańczyk 9aa96ccb19 Merge pull request #13 from mlemanczyk/codex/analyze-tests-for-new-generic-classes
Add dataset coverage and fix stack pop test
2025-07-14 13:55:13 +02:00
Marek Lemańczyk 53c34d0fa7 Fix stack test for empty pop 2025-07-14 13:49:26 +02:00
Marek Lemańczyk 80b4b0e890 Merge pull request #12 from mlemanczyk/codex/implement-recyclablesortedsett
Implement RecyclableSortedSet
2025-07-14 11:43:26 +02:00
Marek Lemańczyk 9133867d39 Add sort tests for RecyclableLongList 2025-07-14 11:34:12 +02:00
Marek Lemańczyk 80499d0936 Add RecyclableSortedSet and long list sort extensions 2025-07-14 11:22:58 +02:00
Marek Lemańczyk 06987cfb0f Merge pull request #11 from mlemanczyk/codex/implementuj-recyclablepriorityqueuet-z-testami
Implement RecyclablePriorityQueue
2025-07-14 10:39:35 +02:00
Marek Lemańczyk 8ff09f12a5 Add RecyclablePriorityQueue with tests 2025-07-14 10:31:51 +02:00
Marek Lemańczyk 7128a73e2e Merge pull request #10 from mlemanczyk/codex/zaimplementuj-recyclablehashsett
Fix initial bucket allocation for HashSet
2025-07-14 10:04:05 +02:00
Marek Lemańczyk a97a81b50c Remove unreachable bucket pooling check 2025-07-14 10:02:05 +02:00
Marek Lemańczyk f203f19312 Merge pull request #9 from mlemanczyk/codex/dodaj-klase-recyclabledictionary
Implement RecyclableDictionary with array-only storage
2025-07-14 06:26:32 +02:00
Marek Lemańczyk f9af1086e5 Use pooled arrays in RecyclableDictionary 2025-07-14 06:24:40 +02:00
Marek Lemańczyk 0f2d05f60d Simplify block allocation logic 2025-07-14 06:05:08 +02:00
Marek Lemańczyk 98c24430ba Merge pull request #8 from mlemanczyk/codex/zaimplementuj-recyclablequeue-z-linkedlistt
Fix missing blank line in RecyclableLinkedList
2025-07-14 02:31:22 +02:00
Marek Lemańczyk 6934dbd02e Fix blank line after if block in RecyclableLinkedList 2025-07-14 02:29:00 +02:00
Marek Lemańczyk 6f9ffd7916 Merge pull request #7 from mlemanczyk/codex/implementuj-klase-recyclablequeue
Implement queue using pooled chunks
2025-07-14 00:30:50 +02:00
Marek Lemańczyk 713a8ffdba Rename queue chunk to BiDirectional prefix 2025-07-14 00:28:03 +02:00
Marek Lemańczyk 3a712dbe57 Implement chunk-based queue 2025-07-13 23:59:39 +02:00
Marek Lemańczyk 93261808aa Merge pull request #6 from mlemanczyk/codex/optymalizuj-klase-recyclablestack-dla-wydajnosci
Document test workflow update
2025-07-13 22:30:28 +02:00
Marek Lemańczyk 770ae2d2f6 Document test command usage and update workflow 2025-07-13 22:28:21 +02:00
Marek Lemańczyk e9887957f9 Merge pull request #5 from mlemanczyk/codex/zmodyfikuj-klase-chunk-i-zaktualizuj-agents.md
Polish chunk API docs and style
2025-07-13 20:50:15 +02:00
Marek Lemańczyk fc0afdcca8 Finalize chunk APIs 2025-07-13 20:49:29 +02:00
Marek Lemańczyk aa52bddd66 Merge pull request #4 from mlemanczyk/codex/implementuj-klase-recyclablestack
Refactor stack enumeration
2025-07-12 03:22:12 +02:00
Marek Lemańczyk ac163014d8 Add chunk pooling and optimize stack growth 2025-07-12 03:17:00 +02:00
Marek Lemańczyk 1b4b9f534b Align stack enumerator with .NET Stack 2025-07-12 02:46:17 +02:00
Marek Lemańczyk 210f0df68c Merge pull request #3 from mlemanczyk/codex/add-nuget-package-links-to-agents.md-and-readme.md
Add NuGet package links
2025-07-10 13:24:27 +02:00
Marek Lemańczyk 64482ca13c Add NuGet package links 2025-07-10 13:23:53 +02:00
Marek Lemańczyk 21634eca68 Merge pull request #2 from mlemanczyk/codex/zmodyfikuj-repozytoria-na-.net-8.0
Remove .NET 9 target frameworks
2025-07-10 13:05:28 +02:00
Marek Lemańczyk a9ee409a18 Remove .NET 9 targets 2025-07-10 13:04:33 +02:00
Marek Lemańczyk 6944fe25b3 Merge pull request #1 from mlemanczyk/codex/sprawdz-metody-listt-w-kolekcjach
Add EnsureCapacity to RecyclableList
2025-07-10 12:17:35 +02:00
Marek Lemańczyk 29b809cead Add .NET 8 and 9 target frameworks 2025-07-10 12:16:48 +02:00
Marek Lemańczyk 877b363381 Add EnsureCapacity method 2025-07-10 12:03:34 +02:00
Marek Lemańczyk c3205debed Create dotnet.yml pipeline 2024-01-18 00:00:15 +01:00
151 changed files with 38551 additions and 24513 deletions

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net
name: .NET
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --framework net8.0 --verbosity normal
+350 -350
View File
@@ -1,350 +1,350 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ogs/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUnit
*.VisualState.xml
TestResult.xml
nunit-*.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
*.appxbundle
*.appxupload
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/
# BeatPulse healthcheck temp database
healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ogs/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUnit
*.VisualState.xml
TestResult.xml
nunit-*.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
*.appxbundle
*.appxupload
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/
# BeatPulse healthcheck temp database
healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/
+25 -25
View File
@@ -1,26 +1,26 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/Recyclable.Collections.Benchmarks/bin/Debug/net6.0/Recyclable.Collections.Benchmarks.dll",
"args": [],
"cwd": "${workspaceFolder}/Recyclable.Collections.Benchmarks",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/Recyclable.Collections.Benchmarks/bin/Debug/net8.0/Recyclable.Collections.Benchmarks.dll",
"args": [],
"cwd": "${workspaceFolder}/Recyclable.Collections.Benchmarks",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
+1
View File
@@ -0,0 +1 @@
{}
+72 -72
View File
@@ -1,73 +1,73 @@
{
"version": "2.0.0",
"tasks": [
{
"isBuildCommand": true,
"label": "build",
"dependsOn": ["build debug", "build release"],
"problemMatcher": "$msCompile",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"isBuildCommand": true,
"label": "build debug",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Recyclable.Collections.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary",
],
"problemMatcher": "$msCompile",
"group": {
"kind": "build",
"isDefault": false
}
},
{
"label": "build release",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Recyclable.Collections.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary",
"-c", "Release",
],
"problemMatcher": "$msCompile",
"group": {
"kind": "build",
"isDefault": false
}
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/Recyclable.Collections.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/Recyclable.Collections.sln"
],
"problemMatcher": "$msCompile"
}
]
{
"version": "2.0.0",
"tasks": [
{
"isBuildCommand": true,
"label": "build",
"dependsOn": ["build debug", "build release"],
"problemMatcher": "$msCompile",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"isBuildCommand": true,
"label": "build debug",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Recyclable.Collections.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary",
],
"problemMatcher": "$msCompile",
"group": {
"kind": "build",
"isDefault": false
}
},
{
"label": "build release",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Recyclable.Collections.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary",
"-c", "Release",
],
"problemMatcher": "$msCompile",
"group": {
"kind": "build",
"isDefault": false
}
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/Recyclable.Collections.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/Recyclable.Collections.sln"
],
"problemMatcher": "$msCompile"
}
]
}
+29
View File
@@ -0,0 +1,29 @@
# AGENTS
This repository hosts various libraries distributed as NuGet packages. Keep these links up to date so contributors and tools can easily locate them.
Only the .NET SDK **8.0** is available in the execution environment. Use this version when building or running tests.
## Implementation Guidelines
1. All `*.csproj` files should enable nullable reference types with `<Nullable>enable</Nullable>`.
1. Implementations should be optimized for maximum performance even if that introduces some code duplication.
1. Consider applying attributes such as `MethodImplOptions.AggressiveInlining` or faster alternatives to common operations when appropriate. Another would be modulo "%" or division "/" operations, which could be often replaced with faster alternatives, possibly under specific conditions.
## Running Tests
1. When running `dotnet test`, do **not** pass `--no-build` because that leads to missing assemblies, test failures without logs and other issues.
1. When running `dotnet test` specify the `--framework` argument matching the SDK available in the environment (currently `net8.0`). If you omit this parameter, the tests will fail running with no logs etc.
1. The tests run for a very long time, because there are 61_000+ unit tests for all the collections. Set timeout when running them to 15 mis.
1. All tests must use **xUnit** together with **FluentAssertions**.
## Source Code Formatting Guidelines
1. Indent code using four spaces or a single tab per level.
1. Place one blank line after each closing curly brace of a code block, except when several closing braces appear sequentially.
1. Always use braces for `if` and `else` bodies.
## NuGet packages
- [Recyclable.Collections](https://github.com/mlemanczyk/Recyclable.Collections)
- [Recyclable.Collections.Compatibility.List](https://github.com/mlemanczyk/Recyclable.Collections.Compatibility.List)
- [Recyclable.Collections.Concurrent](https://github.com/mlemanczyk/Recyclable.Collections.Concurrent)
- [Recyclable.Collections.Linq](https://github.com/mlemanczyk/Recyclable.Collections.Linq)
- [Recyclable.Collections.Searching](https://github.com/mlemanczyk/Recyclable.Collections.Searching)
- [Recyclable.Collections.TestData](https://github.com/mlemanczyk/Recyclable.Collections.TestData)
+9 -9
View File
@@ -1,10 +1,10 @@
# License
The project &amp; source code is free for educational &amp; personal use. **Any kind** of commercial use requires a donation, as prescribed below.
# Donation requirements for a life-time, commercial use
* Commercial license is paid one time.
* It can be obtained by donaiting $10 for each developer seat, using the link provided below. If you hire 2 developers, you need to donate $20 for a life-time, commercial use of this package.
* Your license stays with you, if your developer moves somewhere else.
# Link for donations
# License
The project &amp; source code is free for educational &amp; personal use. **Any kind** of commercial use requires a donation, as prescribed below.
# Donation requirements for a life-time, commercial use
* Commercial license is paid one time.
* It can be obtained by donaiting $10 for each developer seat, using the link provided below. If you hire 2 developers, you need to donate $20 for a life-time, commercial use of this package.
* Your license stays with you, if your developer moves somewhere else.
# Link for donations
[![Donate](buy-me-a-coffee.svg)](https://www.buymeacoffee.com/mlemanczyk)
+433 -425
View File
@@ -1,425 +1,433 @@
# Recyclable.Collections
`Recyclable.Collections` project is an open source framework for operating dynamic lists at performance close to raw arrays, but fairly unlimited in size. It aims at providing minimal memory footprint. It implements `IList<T>` interface and is targeted as direct replacements of `List<T>`, `SortableList<T>`, `PriorityQueue<T>` & similar.
## Included
* `RecyclableList<T>`
* `RecyclableLongList<T>`
# Milestones
1. ✅ Create basic classes
1. ✅ `RecyclableList<T>`
1. ✅ `RecyclableLongList<T>`
1. 🅿️ `RecyclableQueue<T>`
1. 🅿️ `RecyclableSortedList<T>`
1. 🅿️ `RecyclableStack<T>`
1. 🅿️ `RecyclableUnorderedList<T>`
1. ✅ Create basic unit tests
1. ✅ `RecyclableList<T>`
1. ✅ `RecyclableLongList<T>`
1. 🅿️ `RecyclableQueue<T>`
1. 🅿️ `RecyclableSortedList<T>`
1. 🅿️ `RecyclableStack<T>`
1. 🅿️ `RecyclableUnorderedList<T>`
1. ✅ Optimize `RecyclableList<T>`
1. ✅ `Add`
1. ✅ `AddRange`
1. ✅ when source is `array<T>`
1. ✅ when source is `List<T>`
1. ✅ when source is `IList<T>`
1. ✅ when source is `RecyclableList<T>`
1. ✅ when source is `RecyclableLongList<T>`
1. ✅ when source is `IEnumerable<T>`
1. ✅ when source has non-enumerated count
1. ✅ `Clear`
1. ✅ `Contains`
1. ✅ `CopyTo`
1. ✅ `Dispose`
1. ✅ `EnsureCapacity`
1. ✅ `GetEnumerator`
1. ✅ `IndexOf`
1. ✅ `Insert`
1. ✅ `Remove`
1. ✅ `RemoveAt`
1. ✅ `Resize`
1. ✅ `this[int index]`
1. ✅ Port `RecyclableList<T>` implementation to `RecyclableLongList<T>`
1. 👉 Optimize `RecyclableLongList<T>`
1. ✅ `Add`
1. ✅ `AddRange`
1. ✅ when source is `array<T>`
1. ✅ when source is `List<T>`
1. ✅ when source is `IList<T>`
1. ✅ when source is `RecyclableList<T>`
1. ✅ when source is `RecyclableLongList<T>`
1. ✅ when source is `IEnumerable<T>`
1. ✅ when source has non-enumerated count
1. ✅ `Clear`
1. ✅ `Contains`
1. 🅿️ `CopyTo`
1. ✅ `Dispose`
1. ✅ `EnsureCapacity`
1. ✅ `GetEnumerator`
1. ✅ `IndexOf`
1. ✅ `LongIndexOf`
1. ✅ `Insert`
1. ✅ `Remove`
1. ✅ `RemoveAt(int index)`
1. ✅ `RemoveAt(long index)`
1. ✅ `RemoveAt`
1. ✅ `Resize`
1. ✅ `this[int index]`
1. ✅ `this[long index]`
1. ✅ Rename `RecyclableList<T>` to `RecyclableLongList<T>`
1. ✅ Rename `RecyclableArrayList<T>` to `RecyclableList<T>`
1. ✅ Fix failing tests
1. ✅ Hide not ready classes
1. ✅ `RecyclableQueue<T>`
1. ✅ `RecyclableSortedList<T>`
1. ✅ `RecyclableStack<T>`
1. ✅ `RecyclableUnorderedList<T>`
1. ✅ Add support for `ReadOnlySpan<T>`
1. ✅ Release 0.0.3-alpha
1. ✅ Implement `List<T>` interfaces
1. ✅ `IList<T>`
1. ✅ `ICollection<T>`
1. ✅ `IEnumerable<T>`
1. ✅ `IEnumerable`
1. ✅ `IReadOnlyList<T>`
1. ✅ `IReadOnlyCollection<T>`
1. ✅ `IList`
1. ✅ `ICollection`
1. ✅ Implement list versioning to allow data change identification
1. ✅ Make sure that `NeedsClearing` is used & items are cleared in
1. ✅ `RecyclableList<T>`
1. ✅ `Clear`
1. ✅ `Dispose`
1. ✅ `Remove`
1. ✅ `RemoveAt`
1. ✅ Properties
1. ✅ Non-versioned & Versioned Enumerators
1. ✅ `RecyclableListHelpers`
1. ✅ `EnsureCapacity`
1. ✅ `Resize`
1. ✅ `IList<T>` implementation
1. ✅ `RecyclableLongList<T>`
1. ✅ `Clear`
1. ✅ `Dispose`
1. ✅ `Remove`
1. ✅ `RemoveAt`
1. ✅ `RemoveBlock`
1. ✅ Properties
1. ✅ Non-versioned & Versioned Enumerators
1. ✅ `RecyclableLongListHelpers`
1. ✅ `CopyFollowingItems`
1. ✅ `CopyTo`
1. ✅ `EnsureCapacity`
1. ✅ `Resize`
1. ✅ `RecyclableLongList<T>.IndexOfHelpers`
1. ✅ Complete the implementation of `RecyclableLongList<T>`
1. ✅ `InsertAt` to allow inserting items anywhere
1. ✅ `RemoveAt` to allow removing item at any index
1. ✅ Release 0.0.4
1. ✅ Extend unit tests
1. ✅ `.Add` / `.AddRange` must accept `null` values
1. ✅ `.AddRange` must accept empty collections
1. ✅ `.Remove` / `.RemoveAt` / `.Clear` / `.Dispose` must clear reference when reference type
1. ✅ `.Clear` / `.Dispose` must return arrays to pools
1. ✅ `int` / `long` variants of `.Count` / `.LongCount`, `.IndexOf` / `.LongIndexOf`, `.Insert`, `.RemoveAt`, `this[]`
1. ✅ Add `.ToRecyclableList` / `.ToRecyclableLongList` variants for all supported collection types
1. ✅ `Array`
1. ✅ `ICollection`
1. ✅ `ICollection<T>`
1. ✅ `IEnumerable`
1. ✅ `IEnumerable<T>`
1. ✅ `IReadOnlyList<T>`
1. ✅ `ReadOnlySpan<T>`
1. ✅ `RecyclableList`
1. ✅ `RecyclableLongList`
1. ✅ `Span<T>`
1. ✅ `T[]`
1. ✅ Release 0.0.5
1. ✅ Implement `List<T>` compatibility pack for `RecyclableList<T>`
1. ✅ Overflow review
1. ✅ Add type casting to `long` for `<<` & `>>` operations, where required
1. ✅ Make type castings `checked`, where required
1. ✅ Release 0.0.6
1. 👉 Implement `List<T>` compatibility pack for `RecyclableLongList<T>`
1. 🅿️ Port `RecyclableLongList<T>` optimizations to `RecyclableList<T>`
1. `IndexOf` parallelization
1. 🅿️ Release 0.0.7
1. 🅿️ Implement `ILongList<T>` interface
1. 🅿️ `RecyclableList<T>`
1. 🅿️ `RecyclableLongList<T>`
1. 🅿️ Implement `RecyclableQueue<T>`
1. 🅿️ Port `RecyclableLongList<T>` optimizations to `RecyclableQueue<T>`
1. 🅿️ Release 0.0.8
1. 🅿️ Implement `RecyclableStack<T>`
1. 🅿️ Port `RecyclableLongList<T>` optimizations to `RecyclableStack<T>`
1. 🅿️ Release 0.0.9
1. 🅿️ Implement `RecyclableSortedList<T>`
1. 🅿️ Port `RecyclableLongList<T>` optimizations to `RecyclableSortedList<T>`
1. 🅿️ Release 0.0.10
1. 🅿️ Implement `RecyclableUnorderedList<T>`
1. 🅿️ Port `RecyclableLongList<T>` optimizations to `RecyclableUnorderedList<T>`
1. 🅿️ Release 0.0.11
1. 🅿️ Implement `RecyclableVersionedList<T>`
1. 🅿️ Port `RecyclableList<T>` to `RecyclableVersionedList<T>`
1. 🅿️ Port `RecyclableLongList<T>` to `RecyclableVersionedLongList<T>`
1. 🅿️ Release 0.0.12
1. 🅿️ Optimize `OneSizeArrayPool`
1. 🅿️ Review locks
1. 🅿️ Measure multi-threading performance
1. 🅿️ Implement memory bucket disposal in high RAM pressure scenario
1. 🅿️ Optimize `RecyclableCollectionVersionObjectPool`
1. 🅿️ Implement custom `ObjectPool<T>`
1. 🅿️ Measure multi-threading performance
1. 🅿️ Implement memory bucket disposal in high RAM pressure scenario
1. 🅿️ Review `RecyclableArrayPool`
1. 🅿️ Review locks
1. 🅿️ Measure multi-threading performance
1. 🅿️ Implement array disposal in high RAM pressure scenario
1. 🅿️ Optimize `MemoryBucket<T>`
1. 🅿️ Convert to `struct`, if possible
1. 🅿️ Find out if there are better replacements
1. 🅿️ Release 0.0.13
1. 🅿️ Optimize
1. 🅿️ `IndexOfSynchronizationContext`
1. 🅿️ `IndexOfSynchronizationContextPool`
1. 🅿️ `ManualResetEventSlimmer`
1. 🅿️ Multi-threading benchmarks
1. 🅿️ `ManualResetEventSlimmerPool`
1. 🅿️ Multi-threading benchmarks
1. 🅿️ `SpinLockSlimmer`
1. 🅿️ Multi-threading benchmarks
1. 🅿️ Remove range operators, where possible
1. 🅿️ Release 0.0.14
1. 🅿️ Extend unit tests
1. 🅿️ `RecyclableLongListExtensions.CopyTo`
1. 🅿️ `RecyclableLongList` cases for large items count (&gt;`int.MaxValue`)
1. 🅿️ Pools
1. 🅿️ Multi-threading
1. 🅿️ Add comparison tests vs `T[]` / `List<T>` equivalents
1. 🅿️ Add test cases for indexes beyond items count
1. 🅿️ Cover border cases
1. 🅿️ Non-existing items in `.Contains...()`, `.Exists...()`, `.Find...()`, `.IndexOf...()`, `.Last...()`
1. 🅿️ Duplicated items in `.Contains...()`, `.Exists...()`, `.Find...()`, `.IndexOf...()`, `.Last...()`
1. 🅿️ Append test data in reversed order in `.AddRange()`, `.InsertRange()` and similar tests
1. 🅿️ Switch tests to use Source / Target data variants in `.AddRange()`, `.InsertRange()` and similar tests
1. 🅿️ Cleanup
1. 🅿️ Replace `LastBlockWithData` property with `_lastBlockWithData` field
1. 🅿️ Replace all internal properties refs with fields, where possible
1. ✅ `RecyclableLongListExtensions`
1. ✅ `ListExtensions`
1. 🅿️ `MathUtils`
1. 🅿️ Resolve TODOs
1. 🅿️ Remove obsolete methods
1. 🅿️ Add`RecyclableLongListHelpers.Enumerate` to benchmarks
1. 🅿️ Review all classes & add `volatile`, where needed
1. 🅿️ Remove `expectedItemsCount` parameter from `RecyclableLongList`'s constructor
1. 🅿️ Remove `array` slicing in favor for indexing, like in `RecyclableCompatibilityListExtensions`
1. 🅿️ Release 0.0.15
1. 🅿️ Optimize
1. 🅿️ `ListExtensions`
1. 🅿️ `MathUtils`
1. 🅿️ `SystemRandomNumberGenerator`
1. 🅿️ `RecyclableLongListExtensions`
1. 🅿️ Memory release in pools to utilize GC events in case of memory pressure
1. 🅿️ Release 0.0.16
1. ✅ Review and remove warnings & hints
1. ✅ Warnings
1. ✅ Hints
1. Documentation
1. 🅿️ Document the most efficient iteration over array collections
1. 🅿️ Document the most efficient iteration over blocked collections
1. 🅿️ Document differences in behavior
1. 🅿️ Document other specifics
1. 🅿️ Release 1.0.0
1. 🅿️ Implement source code generators
1. 🅿️ Add attributes
1. 🅿️ `GeneratorBaseClassAttribute` for marking base class used for generation
1. 🅿️ `VersionedAttribute` for marking classes as versioned
1. 🅿️ `IncVersionAttribute` for marking methods & setters as resulting in version increase
1. 🅿️ Implement source code generator
1. 🅿️ Generate partial class for classes marked with `GeneratorBaseClassAttribute`
1. 🅿️ Add support for fields
1. 🅿️ Add support for properties
1. 🅿️ Add support for methods
1. 🅿️ Add support for constructor
1. 🅿️ Skip base fields, methods, properties etc. when they're overridden in the child class
1. 🅿️ Add support for `ulong` indexing
1. 🅿️ Convert `_memoryBlocks` to `Array` to allow `ulong` lengths
1. 🅿️ Convert block indexes from `int` to `ulong` or `long`
1. 🅿️ Optimize
1. 🅿️ `RecyclableLongList<T>.Resize`
1. 🅿️ `RecyclableLongList<T>.CopyTo`
1. 🅿️ Check if we can benefit from Sse2 in `.IndexOf`/`.Contains` methods as given in [MS blog](https://devblogs.microsoft.com/dotnet/hardware-intrinsics-in-net-core/).
1. 🅿️ Memory pools
1. 🅿️ `OneSizeArrayPool<T>`
1. 🅿️ `RecyclableArrayPool<T>`
1. 🅿️ Add support for `ICollection<T>` interface in `.AddRange` & `constructor`
1. 🅿️ Final optimizations
1. 🅿️ Replace `Math` class usages with `if` statements
1. 🅿️ Replace `a - b > 0` & `a - b < 0` comparisons with `a > b` & `a < b`
1. 🅿️ Replace `a + b > 0` & `a + b < 0` comparisons with `a > b` & `a < b`
1. 🅿️ Replace `a / b` & `a * b` calculations with equivalents, where possible
1. 🅿️ Replace virtual calls with static calls
1. 🅿️ Separate `++number`, `number++`, `--number` & `number--` operations to their own lines (they add extra variable)
1. 🅿️ Replace `blockSize` sums by powers of 2, minus 1
1. 🅿️ Remove type castings, if possible
1. 🅿️ Convert generic methods to non-generic
1. 🅿️ Replace integer comparisons with comparisons to 0 / 1, if possible
1. 🅿️ Release 2.0.0
# Characteristics of the classes
## Common
* All classes implement `IDisposable` interface & SHOULD be disposed after use. That's to return block arrays taken from the shared pool. It may be foreseen as an issue for replacement in existing code, which obviously is missing `using` clause. But considering that `Dispose` will be called by `GC` anyway, it should cause issues in specific scenarios, only. In either case the fix is one-word addition of `using`.
* Memory blocks are created in 2 ways, depending on `blockSize` value
* when `blockSize < 128` ⇨ blocks are created as regular `arrays`.
* when `blockSize >= 128` ⇨ blocks are taken from and returned to `ArrayPool<T>.Shared`, when blocks are removed.
* maximum `blockSize` value is `2_147_483_591`, which corresponds more or less to the maximum array size.
* `blockSize` MUST be a power of 2. It will be rounded up to the closest power of 2, if needed. That is due to high performance gain on some operations, like the calculation of item index in a block.
* Array pools are shared between the same `T` type. I.e. `List<int>` will use a different pool from `List<short>` and so on. For high concurrency environments you may want to provide your own pools, when this feature becomes available in the upcoming releases.
* Trying to access `this[int index]`, `this[long index]`, `Count`, `LongCount` etc. when `Capacity == 0` will raise `NullReferenceException`. This is by design to remove all non-critical code from the constructor.
* ⚠️ The default enumerators don't check if the collection was modified. That's by design due to high performance hit of the check. That should be fine in most use-cases. If you need to check for modifications, you're welcomed to use `.GetVersionedEnumerator()` / `IRecyclableVersionedXxxList<T>` type-casting, e.g. in `foreach` loops.
```csharp
foreach (var item in (IRecyclableVersionedList<long>)recyclableList)
{
...
...
...
}
foreach (var item in (IRecyclableVersionedLongList<long>)recyclableLongList)
{
...
...
...
}
```
* ⚠️ The state of the default enumerators before the first & after the last call to `.MoveNext()` is undefined, but no exception is raised. That's by design due to high performance hit of the checks. This behavior is compatible with `foreach` loops, which will never access `.Current` property
* when the collection is empty, or
* when `.MoveNext()` returned `false`, after reaching the end of the collection.
* ⚠️ If you decide to use `.GetEnumerator()` / `.GetVersionedEnumerator()`, instead of relying on `foreach` loops
* you MUST ensure that you always respect the result of `.MoveNext()` call, and
* you MUST NOT access `.Current` property before calling `.MoveNext()` or after reaching the end of the collection.
\
**Failing to do so WILL result in unpredictable behavior.**
### [Benchmark results](https://github.com/mlemanczyk/Recyclable.Collections/blob/main/Recyclable.Collections.Benchmarks/Results/Benchmarks.md)
## `RecyclableList<T>`
* Range: `int`
* Interfaces: `IList<T>`, `IEnumerable<T>`, `IDisposable`\
\
This is the direct equivalent of `List<T>` class, except that the arrays are taken from the shared pool, instead of directly being allocated. It proves to out perform `List<T>` & the standard arrays in certain operations and is fairly close in others. But they may perform worse in certain scenarios. If you're interested in details, please refer to benchmarks.
## `RecyclableLongList<T>`, `RecyclableSortableList<T>`, `RecyclableQueue<T>`, `RecyclableStack<T>`
* Range: `long`
* Interfaces: `IList<T>`, `ILongList<T>`, `IEnumerable<T>`, `IDisposable`
* All classes provide large storage capabilities, in practice limited only by the memory available in your system.
* All data is stored in blocks, with the provided `int blockSize` or the default, currently being `10,240` items in each block. It's recommended to use smaller numbers, if you foresee storing low no. of items on the list.
* There is no memory copying to increase the capacity of the recyclable classes, unless the no. of items exceeds the no. of allocated blocks. In that case, a new array of blocks is created to accommodate . If more capacity is needed, a new memory block is allocate & added to the internal list of blocks. This is the only list that may re-allocate memory block & copy their content to grow. That shouldn't be an issue considering limited no. of blocks stored by each class in practical scenarios. Currently I'm not planning any works around that. Shall there be a need to eliminate it, you're welcomed to file a PR with the proposed changes.
* ⚠️ `IndexOf` will automatically switch to parallel search mode. Collections bigger than (currently) 850_000 items will be scanned using parallelization. In either case it'll return the index of the first matching item. This note also applies to `Contains`, because it internally uses `IndexOf`. The value of 850_000 may become configurable in the future, unless it proves to result in much worse performance.
The parallelization utilizes default `Task` scheduling, resulting in non-deterministic order in which search tasks are executed. In most cases it resulted in significant performance improvements. Below you can find the benchmarks for various item counts.
As `RecyclableLongList<T>` is foreseen for use-cases with big data, you should automatically benefit from it in many use-cases.
### [Benchmark Results](https://github.com/mlemanczyk/Recyclable.Collections/blob/main/Recyclable.Collections.Benchmarks/Results/Benchmarks.md)
# Architecture Decisions
The architecture of the classes included in this project is driven primarily by performance & memory footprints. The following decisions were made to support it.
## Code duplication
There is high level of code duplication in each of the classes. When you look at them, you will see they are alike & some of the code could be extracted to extension methods. It has been done & has proven to perform worse. For this reason, properties & methods expected to be on the hot-path, duplicate much of the code to get the most out of it.
## Common base - `ILongList<T>`
Having a common base class for all the classes looks very attractive. But it is also proved to perform worse, if you start making properties & methods virtual & there are subsequent calls to `base`. For this reason, each of the classes is the root class & there are hardly any virtual methods & properties. All classes are meant to be `final`, but I've not marked them as `sealed`. If you feel that you'd benefit from it, you're welcomed to inherit from them. The protected stuff is there for your use.
If you desire to have a common base for calls, each of the classes implements `IList<T>` & `ILongList<T>` interfaces. `ILongList<T>` is a new interface, made alike `IList<T>`, but unlocking full support for `long` indexing & use. Considering the characteristics of the classes I feel that the generic name is justified, instead of `IRecyclableList<T>` what you could expect.
## Protection against incorrect use & state
Having protection against invalid state of the object is an important part of software engineering. The classes included in this project provide the same level of protection as the regular `array` & `List` class. They will raise exceptions when invalid index is given. But there is no additional protection that the content & state of the objects internally used by these classes are valid. You can break them, if you want to.
This decision is driven by performance measures. Considering wide use of the lists on the hot-paths, each additional check & operation starts counting. I've decided to eliminate all the checks for the sake of performance. If you feel that you need additional protection, feel free to inherit or reuse any of the provided classes to provide that.
## `this[int index]`, `Count`, `IndexOf(T item)` etc. may overflow, but...
All the classes included in this package support `long` indexing and addressing. The `LongCount` property will always provide the total no. of items stored on the list. Since `Count` is just `LongCount` type-casted to `int`, its value may overflow.
But `List<T>` addressing is kind of currently limited to `int` ranges anyway in practical scenarios. As such `Count` overflowing shouldn't be an issue when using the classes with existing code. If you use `Count` in your loops, it's recommended to replace it with `LongCount`, tough. You don't need to make any changes in `foreach` or other iterator loops. They are expected to transparently use the new classes & yield all items, beyond `int.MaxValue` range.
This is by design, for the best performance. Initially there have been checks in place to prevent overflowing & return `int.MaxValue` instead, but they proved to slow down the operations. Considering low risk for existing code, I've decided to take it out.
If you foresee the overflowing as an issue, you're welcomed to propose an improvement & file a PR with proposed solution.
# Auto-properties, new language features & code style
When you look at the code you may note, that it looks like in early c# days. Where the new language features yield the same or better performance, they are & will be used.
However, many of the new language features have proved to provide worse performance at some point in my testing. It includes auto-properties, for which I could see getters & setters generated in IL, instead of fields. Because all the classes are expected to be on the hot-paths, I've eliminated most of them.
Similarly, you will find places in code with hints or warnings disabled. The decision was always driven by performance measures. The use case & business logic was carefully reviewed to ensure it's safe to take shortcuts, before making the change. If you run into failing scenario, please feel free to create a work item against it and/or file a PR to fix it.
Finally, you'll find places with code commented out. That's to easily track what has been tried & what proved to perform worse.
At any time, if you know & can show in your benchmarks that the code can be simplified without impacting the performance, feel free to file a PR with proposed changes. Please include benchmark results from your testing to speed up things.
# Thread Safety
The `public static` members of the classes are thread safe. Any instance members are not guaranteed to be thread safe. You need to implement locking mechanism to safely use the classes in a multi-threading environment.
# Use
All the classes included in this package are meant to be direct replacements of their corresponding system classes.
* `IList<T>` ⇨ `ILongList<T>`
* `List<T>` ⇨ `RecyclableLongList<T>`
* `Queue<T>` ⇨ `RecyclableQueue<T>`
* `PriorityQueue<T>` ⇨ `RecyclableSortedList<T>`
* `SortedList<T>` ⇨ `RecyclableSortedList<T>`
* `Stack<T>` ⇨ `RecyclableStack<T>`
Examples of each of the above classes are provided below. Please refer automated tests for more.
## `RecyclableList<T>`
It's the closest equivalent to `List<T>` in regards to functionality. It supports items' removal & delivers the best performance in most scenarios. But it's limited to `int` range, tough. If you find that acceptable, my recommendation is to use this class as the direct replacement for `List<T>`.
Do note, that it implements `ILongList<T>` interface, too. This is to allow having common code base regardless, if you use the basic `RecyclableList<T>` or it's `long`-range version - `RecyclableLongList<T>`. By doing that you can switch between them with highly limited changes in code.
## `RecyclableLongList<T>`
```CSharp
public void RecyclableListExample()
{
int[] testNumbers = new[] { 1, 2, 2, 3 };
// Create
using var list = new RecyclableLongList<int>();
// Add items
foreach (var index in testNumbers)
{
list.Add(index);
}
foreach (var item in list)
{
Console.WriteLine(item);
}
// This will iterate over this[long index]
for (var itemIdx = 0; itemIdx <= list.LongCount; itemIdx++)
{
Console.WriteLine(list[itemIdx]);
}
// This will iterate over this[int index] and may overflow if there are > int.MaxValue no. of items on the list. See: Architecture Decisions
for (var itemIdx = 0; itemIdx <= list.LongCount; itemIdx++)
{
Console.WriteLine(list[itemIdx]);
}
list.Clear();
}
```
### [Benchmark results](https://github.com/mlemanczyk/Recyclable.Collections/blob/main/Recyclable.Collections.Benchmarks/Results/Benchmarks.md)
# Recyclable.Collections
`Recyclable.Collections` project is an open source framework for operating dynamic lists at performance close to raw arrays, but fairly unlimited in size. It aims at providing minimal memory footprint. It implements `IList<T>` interface and is targeted as direct replacements of `List<T>`, `SortableList<T>`, `PriorityQueue<T>` & similar.
## Included
* `RecyclableList<T>`
* `RecyclableLongList<T>`
## NuGet packages
- [Recyclable.Collections](https://github.com/mlemanczyk/Recyclable.Collections)
- [Recyclable.Collections.Compatibility.List](https://github.com/mlemanczyk/Recyclable.Collections.Compatibility.List)
- [Recyclable.Collections.Concurrent](https://github.com/mlemanczyk/Recyclable.Collections.Concurrent)
- [Recyclable.Collections.Linq](https://github.com/mlemanczyk/Recyclable.Collections.Linq)
- [Recyclable.Collections.Searching](https://github.com/mlemanczyk/Recyclable.Collections.Searching)
- [Recyclable.Collections.TestData](https://github.com/mlemanczyk/Recyclable.Collections.TestData)
# Milestones
1. ✅ Create basic classes
1. ✅ `RecyclableList<T>`
1. ✅ `RecyclableLongList<T>`
1. 🅿️ `RecyclableQueue<T>`
1. 🅿️ `RecyclableSortedList<T>`
1. 🅿️ `RecyclableStack<T>`
1. 🅿️ `RecyclableUnorderedList<T>`
1. ✅ Create basic unit tests
1. ✅ `RecyclableList<T>`
1. ✅ `RecyclableLongList<T>`
1. 🅿️ `RecyclableQueue<T>`
1. 🅿️ `RecyclableSortedList<T>`
1. 🅿️ `RecyclableStack<T>`
1. 🅿️ `RecyclableUnorderedList<T>`
1. ✅ Optimize `RecyclableList<T>`
1. ✅ `Add`
1. ✅ `AddRange`
1. ✅ when source is `array<T>`
1. ✅ when source is `List<T>`
1. ✅ when source is `IList<T>`
1. ✅ when source is `RecyclableList<T>`
1. ✅ when source is `RecyclableLongList<T>`
1. ✅ when source is `IEnumerable<T>`
1. ✅ when source has non-enumerated count
1. ✅ `Clear`
1. ✅ `Contains`
1. ✅ `CopyTo`
1. ✅ `Dispose`
1. ✅ `EnsureCapacity`
1. ✅ `GetEnumerator`
1. ✅ `IndexOf`
1. ✅ `Insert`
1. ✅ `Remove`
1. ✅ `RemoveAt`
1. ✅ `Resize`
1. ✅ `this[int index]`
1. ✅ Port `RecyclableList<T>` implementation to `RecyclableLongList<T>`
1. 👉 Optimize `RecyclableLongList<T>`
1. ✅ `Add`
1. ✅ `AddRange`
1. ✅ when source is `array<T>`
1. ✅ when source is `List<T>`
1. ✅ when source is `IList<T>`
1. ✅ when source is `RecyclableList<T>`
1. ✅ when source is `RecyclableLongList<T>`
1. ✅ when source is `IEnumerable<T>`
1. ✅ when source has non-enumerated count
1. ✅ `Clear`
1. ✅ `Contains`
1. 🅿️ `CopyTo`
1. ✅ `Dispose`
1. ✅ `EnsureCapacity`
1. ✅ `GetEnumerator`
1. ✅ `IndexOf`
1. ✅ `LongIndexOf`
1. ✅ `Insert`
1. ✅ `Remove`
1. ✅ `RemoveAt(int index)`
1. ✅ `RemoveAt(long index)`
1. ✅ `RemoveAt`
1. ✅ `Resize`
1. ✅ `this[int index]`
1. ✅ `this[long index]`
1. ✅ Rename `RecyclableList<T>` to `RecyclableLongList<T>`
1. ✅ Rename `RecyclableArrayList<T>` to `RecyclableList<T>`
1. ✅ Fix failing tests
1. ✅ Hide not ready classes
1. ✅ `RecyclableQueue<T>`
1. ✅ `RecyclableSortedList<T>`
1. ✅ `RecyclableStack<T>`
1. ✅ `RecyclableUnorderedList<T>`
1. ✅ Add support for `ReadOnlySpan<T>`
1. ✅ Release 0.0.3-alpha
1. ✅ Implement `List<T>` interfaces
1. ✅ `IList<T>`
1. ✅ `ICollection<T>`
1. ✅ `IEnumerable<T>`
1. ✅ `IEnumerable`
1. ✅ `IReadOnlyList<T>`
1. ✅ `IReadOnlyCollection<T>`
1. ✅ `IList`
1. ✅ `ICollection`
1. ✅ Implement list versioning to allow data change identification
1. ✅ Make sure that `NeedsClearing` is used & items are cleared in
1. ✅ `RecyclableList<T>`
1. ✅ `Clear`
1. ✅ `Dispose`
1. ✅ `Remove`
1. ✅ `RemoveAt`
1. ✅ Properties
1. ✅ Non-versioned & Versioned Enumerators
1. ✅ `RecyclableListHelpers`
1. ✅ `EnsureCapacity`
1. ✅ `Resize`
1. ✅ `IList<T>` implementation
1. ✅ `RecyclableLongList<T>`
1. ✅ `Clear`
1. ✅ `Dispose`
1. ✅ `Remove`
1. ✅ `RemoveAt`
1. ✅ `RemoveBlock`
1. ✅ Properties
1. ✅ Non-versioned & Versioned Enumerators
1. ✅ `RecyclableLongListHelpers`
1. ✅ `CopyFollowingItems`
1. ✅ `CopyTo`
1. ✅ `EnsureCapacity`
1. ✅ `Resize`
1. ✅ `RecyclableLongList<T>.IndexOfHelpers`
1. ✅ Complete the implementation of `RecyclableLongList<T>`
1. ✅ `InsertAt` to allow inserting items anywhere
1. ✅ `RemoveAt` to allow removing item at any index
1. ✅ Release 0.0.4
1. ✅ Extend unit tests
1. ✅ `.Add` / `.AddRange` must accept `null` values
1. ✅ `.AddRange` must accept empty collections
1. ✅ `.Remove` / `.RemoveAt` / `.Clear` / `.Dispose` must clear reference when reference type
1. ✅ `.Clear` / `.Dispose` must return arrays to pools
1. ✅ `int` / `long` variants of `.Count` / `.LongCount`, `.IndexOf` / `.LongIndexOf`, `.Insert`, `.RemoveAt`, `this[]`
1. ✅ Add `.ToRecyclableList` / `.ToRecyclableLongList` variants for all supported collection types
1. ✅ `Array`
1. ✅ `ICollection`
1. ✅ `ICollection<T>`
1. ✅ `IEnumerable`
1. ✅ `IEnumerable<T>`
1. ✅ `IReadOnlyList<T>`
1. ✅ `ReadOnlySpan<T>`
1. ✅ `RecyclableList`
1. ✅ `RecyclableLongList`
1. ✅ `Span<T>`
1. ✅ `T[]`
1. ✅ Release 0.0.5
1. ✅ Implement `List<T>` compatibility pack for `RecyclableList<T>`
1. ✅ Overflow review
1. ✅ Add type casting to `long` for `<<` & `>>` operations, where required
1. ✅ Make type castings `checked`, where required
1. ✅ Release 0.0.6
1. 👉 Implement `List<T>` compatibility pack for `RecyclableLongList<T>`
1. 🅿️ Port `RecyclableLongList<T>` optimizations to `RecyclableList<T>`
1. `IndexOf` parallelization
1. 🅿️ Release 0.0.7
1. 🅿️ Implement `ILongList<T>` interface
1. 🅿️ `RecyclableList<T>`
1. 🅿️ `RecyclableLongList<T>`
1. 🅿️ Implement `RecyclableQueue<T>`
1. 🅿️ Port `RecyclableLongList<T>` optimizations to `RecyclableQueue<T>`
1. 🅿️ Release 0.0.8
1. 🅿️ Implement `RecyclableStack<T>`
1. 🅿️ Port `RecyclableLongList<T>` optimizations to `RecyclableStack<T>`
1. 🅿️ Release 0.0.9
1. 🅿️ Implement `RecyclableSortedList<T>`
1. 🅿️ Port `RecyclableLongList<T>` optimizations to `RecyclableSortedList<T>`
1. 🅿️ Release 0.0.10
1. 🅿️ Implement `RecyclableUnorderedList<T>`
1. 🅿️ Port `RecyclableLongList<T>` optimizations to `RecyclableUnorderedList<T>`
1. 🅿️ Release 0.0.11
1. 🅿️ Implement `RecyclableVersionedList<T>`
1. 🅿️ Port `RecyclableList<T>` to `RecyclableVersionedList<T>`
1. 🅿️ Port `RecyclableLongList<T>` to `RecyclableVersionedLongList<T>`
1. 🅿️ Release 0.0.12
1. 🅿️ Optimize `OneSizeArrayPool`
1. 🅿️ Review locks
1. 🅿️ Measure multi-threading performance
1. 🅿️ Implement memory bucket disposal in high RAM pressure scenario
1. 🅿️ Optimize `RecyclableCollectionVersionObjectPool`
1. 🅿️ Implement custom `ObjectPool<T>`
1. 🅿️ Measure multi-threading performance
1. 🅿️ Implement memory bucket disposal in high RAM pressure scenario
1. 🅿️ Review `RecyclableArrayPool`
1. 🅿️ Review locks
1. 🅿️ Measure multi-threading performance
1. 🅿️ Implement array disposal in high RAM pressure scenario
1. 🅿️ Optimize `MemoryBucket<T>`
1. 🅿️ Convert to `struct`, if possible
1. 🅿️ Find out if there are better replacements
1. 🅿️ Release 0.0.13
1. 🅿️ Optimize
1. 🅿️ `IndexOfSynchronizationContext`
1. 🅿️ `IndexOfSynchronizationContextPool`
1. 🅿️ `ManualResetEventSlimmer`
1. 🅿️ Multi-threading benchmarks
1. 🅿️ `ManualResetEventSlimmerPool`
1. 🅿️ Multi-threading benchmarks
1. 🅿️ `SpinLockSlimmer`
1. 🅿️ Multi-threading benchmarks
1. 🅿️ Remove range operators, where possible
1. 🅿️ Release 0.0.14
1. 🅿️ Extend unit tests
1. 🅿️ `RecyclableLongListExtensions.CopyTo`
1. 🅿️ `RecyclableLongList` cases for large items count (&gt;`int.MaxValue`)
1. 🅿️ Pools
1. 🅿️ Multi-threading
1. 🅿️ Add comparison tests vs `T[]` / `List<T>` equivalents
1. 🅿️ Add test cases for indexes beyond items count
1. 🅿️ Cover border cases
1. 🅿️ Non-existing items in `.Contains...()`, `.Exists...()`, `.Find...()`, `.IndexOf...()`, `.Last...()`
1. 🅿️ Duplicated items in `.Contains...()`, `.Exists...()`, `.Find...()`, `.IndexOf...()`, `.Last...()`
1. 🅿️ Append test data in reversed order in `.AddRange()`, `.InsertRange()` and similar tests
1. 🅿️ Switch tests to use Source / Target data variants in `.AddRange()`, `.InsertRange()` and similar tests
1. 🅿️ Cleanup
1. 🅿️ Replace `LastBlockWithData` property with `_lastBlockWithData` field
1. 🅿️ Replace all internal properties refs with fields, where possible
1. ✅ `RecyclableLongListExtensions`
1. ✅ `ListExtensions`
1. 🅿️ `MathUtils`
1. 🅿️ Resolve TODOs
1. 🅿️ Remove obsolete methods
1. 🅿️ Add`RecyclableLongListHelpers.Enumerate` to benchmarks
1. 🅿️ Review all classes & add `volatile`, where needed
1. 🅿️ Remove `expectedItemsCount` parameter from `RecyclableLongList`'s constructor
1. 🅿️ Remove `array` slicing in favor for indexing, like in `RecyclableCompatibilityListExtensions`
1. 🅿️ Release 0.0.15
1. 🅿️ Optimize
1. 🅿️ `ListExtensions`
1. 🅿️ `MathUtils`
1. 🅿️ `SystemRandomNumberGenerator`
1. 🅿️ `RecyclableLongListExtensions`
1. 🅿️ Memory release in pools to utilize GC events in case of memory pressure
1. 🅿️ Release 0.0.16
1. ✅ Review and remove warnings & hints
1. ✅ Warnings
1. ✅ Hints
1. Documentation
1. 🅿️ Document the most efficient iteration over array collections
1. 🅿️ Document the most efficient iteration over blocked collections
1. 🅿️ Document differences in behavior
1. 🅿️ Document other specifics
1. 🅿️ Release 1.0.0
1. 🅿️ Implement source code generators
1. 🅿️ Add attributes
1. 🅿️ `GeneratorBaseClassAttribute` for marking base class used for generation
1. 🅿️ `VersionedAttribute` for marking classes as versioned
1. 🅿️ `IncVersionAttribute` for marking methods & setters as resulting in version increase
1. 🅿️ Implement source code generator
1. 🅿️ Generate partial class for classes marked with `GeneratorBaseClassAttribute`
1. 🅿️ Add support for fields
1. 🅿️ Add support for properties
1. 🅿️ Add support for methods
1. 🅿️ Add support for constructor
1. 🅿️ Skip base fields, methods, properties etc. when they're overridden in the child class
1. 🅿️ Add support for `ulong` indexing
1. 🅿️ Convert `_memoryBlocks` to `Array` to allow `ulong` lengths
1. 🅿️ Convert block indexes from `int` to `ulong` or `long`
1. 🅿️ Optimize
1. 🅿️ `RecyclableLongList<T>.Resize`
1. 🅿️ `RecyclableLongList<T>.CopyTo`
1. 🅿️ Check if we can benefit from Sse2 in `.IndexOf`/`.Contains` methods as given in [MS blog](https://devblogs.microsoft.com/dotnet/hardware-intrinsics-in-net-core/).
1. 🅿️ Memory pools
1. 🅿️ `OneSizeArrayPool<T>`
1. 🅿️ `RecyclableArrayPool<T>`
1. 🅿️ Add support for `ICollection<T>` interface in `.AddRange` & `constructor`
1. 🅿️ Final optimizations
1. 🅿️ Replace `Math` class usages with `if` statements
1. 🅿️ Replace `a - b > 0` & `a - b < 0` comparisons with `a > b` & `a < b`
1. 🅿️ Replace `a + b > 0` & `a + b < 0` comparisons with `a > b` & `a < b`
1. 🅿️ Replace `a / b` & `a * b` calculations with equivalents, where possible
1. 🅿️ Replace virtual calls with static calls
1. 🅿️ Separate `++number`, `number++`, `--number` & `number--` operations to their own lines (they add extra variable)
1. 🅿️ Replace `blockSize` sums by powers of 2, minus 1
1. 🅿️ Remove type castings, if possible
1. 🅿️ Convert generic methods to non-generic
1. 🅿️ Replace integer comparisons with comparisons to 0 / 1, if possible
1. 🅿️ Release 2.0.0
# Characteristics of the classes
## Common
* All classes implement `IDisposable` interface & SHOULD be disposed after use. That's to return block arrays taken from the shared pool. It may be foreseen as an issue for replacement in existing code, which obviously is missing `using` clause. But considering that `Dispose` will be called by `GC` anyway, it should cause issues in specific scenarios, only. In either case the fix is one-word addition of `using`.
* Memory blocks are created in 2 ways, depending on `blockSize` value
* when `blockSize < 128` ⇨ blocks are created as regular `arrays`.
* when `blockSize >= 128` ⇨ blocks are taken from and returned to `ArrayPool<T>.Shared`, when blocks are removed.
* maximum `blockSize` value is `2_147_483_591`, which corresponds more or less to the maximum array size.
* `blockSize` MUST be a power of 2. It will be rounded up to the closest power of 2, if needed. That is due to high performance gain on some operations, like the calculation of item index in a block.
* Array pools are shared between the same `T` type. I.e. `List<int>` will use a different pool from `List<short>` and so on. For high concurrency environments you may want to provide your own pools, when this feature becomes available in the upcoming releases.
* Trying to access `this[int index]`, `this[long index]`, `Count`, `LongCount` etc. when `Capacity == 0` will raise `NullReferenceException`. This is by design to remove all non-critical code from the constructor.
* ⚠️ The default enumerators don't check if the collection was modified. That's by design due to high performance hit of the check. That should be fine in most use-cases. If you need to check for modifications, you're welcomed to use `.GetVersionedEnumerator()` / `IRecyclableVersionedXxxList<T>` type-casting, e.g. in `foreach` loops.
```csharp
foreach (var item in (IRecyclableVersionedList<long>)recyclableList)
{
...
...
...
}
foreach (var item in (IRecyclableVersionedLongList<long>)recyclableLongList)
{
...
...
...
}
```
* ⚠️ The state of the default enumerators before the first & after the last call to `.MoveNext()` is undefined, but no exception is raised. That's by design due to high performance hit of the checks. This behavior is compatible with `foreach` loops, which will never access `.Current` property
* when the collection is empty, or
* when `.MoveNext()` returned `false`, after reaching the end of the collection.
* ⚠️ If you decide to use `.GetEnumerator()` / `.GetVersionedEnumerator()`, instead of relying on `foreach` loops
* you MUST ensure that you always respect the result of `.MoveNext()` call, and
* you MUST NOT access `.Current` property before calling `.MoveNext()` or after reaching the end of the collection.
\
**Failing to do so WILL result in unpredictable behavior.**
### [Benchmark results](https://github.com/mlemanczyk/Recyclable.Collections/blob/main/Recyclable.Collections.Benchmarks/Results/Benchmarks.md)
## `RecyclableList<T>`
* Range: `int`
* Interfaces: `IList<T>`, `IEnumerable<T>`, `IDisposable`\
\
This is the direct equivalent of `List<T>` class, except that the arrays are taken from the shared pool, instead of directly being allocated. It proves to out perform `List<T>` & the standard arrays in certain operations and is fairly close in others. But they may perform worse in certain scenarios. If you're interested in details, please refer to benchmarks.
## `RecyclableLongList<T>`, `RecyclableSortableList<T>`, `RecyclableQueue<T>`, `RecyclableStack<T>`
* Range: `long`
* Interfaces: `IList<T>`, `ILongList<T>`, `IEnumerable<T>`, `IDisposable`
* All classes provide large storage capabilities, in practice limited only by the memory available in your system.
* All data is stored in blocks, with the provided `int blockSize` or the default, currently being `10,240` items in each block. It's recommended to use smaller numbers, if you foresee storing low no. of items on the list.
* There is no memory copying to increase the capacity of the recyclable classes, unless the no. of items exceeds the no. of allocated blocks. In that case, a new array of blocks is created to accommodate . If more capacity is needed, a new memory block is allocate & added to the internal list of blocks. This is the only list that may re-allocate memory block & copy their content to grow. That shouldn't be an issue considering limited no. of blocks stored by each class in practical scenarios. Currently I'm not planning any works around that. Shall there be a need to eliminate it, you're welcomed to file a PR with the proposed changes.
* ⚠️ `IndexOf` will automatically switch to parallel search mode. Collections bigger than (currently) 850_000 items will be scanned using parallelization. In either case it'll return the index of the first matching item. This note also applies to `Contains`, because it internally uses `IndexOf`. The value of 850_000 may become configurable in the future, unless it proves to result in much worse performance.
The parallelization utilizes default `Task` scheduling, resulting in non-deterministic order in which search tasks are executed. In most cases it resulted in significant performance improvements. Below you can find the benchmarks for various item counts.
As `RecyclableLongList<T>` is foreseen for use-cases with big data, you should automatically benefit from it in many use-cases.
### [Benchmark Results](https://github.com/mlemanczyk/Recyclable.Collections/blob/main/Recyclable.Collections.Benchmarks/Results/Benchmarks.md)
# Architecture Decisions
The architecture of the classes included in this project is driven primarily by performance & memory footprints. The following decisions were made to support it.
## Code duplication
There is high level of code duplication in each of the classes. When you look at them, you will see they are alike & some of the code could be extracted to extension methods. It has been done & has proven to perform worse. For this reason, properties & methods expected to be on the hot-path, duplicate much of the code to get the most out of it.
## Common base - `ILongList<T>`
Having a common base class for all the classes looks very attractive. But it is also proved to perform worse, if you start making properties & methods virtual & there are subsequent calls to `base`. For this reason, each of the classes is the root class & there are hardly any virtual methods & properties. All classes are meant to be `final`, but I've not marked them as `sealed`. If you feel that you'd benefit from it, you're welcomed to inherit from them. The protected stuff is there for your use.
If you desire to have a common base for calls, each of the classes implements `IList<T>` & `ILongList<T>` interfaces. `ILongList<T>` is a new interface, made alike `IList<T>`, but unlocking full support for `long` indexing & use. Considering the characteristics of the classes I feel that the generic name is justified, instead of `IRecyclableList<T>` what you could expect.
## Protection against incorrect use & state
Having protection against invalid state of the object is an important part of software engineering. The classes included in this project provide the same level of protection as the regular `array` & `List` class. They will raise exceptions when invalid index is given. But there is no additional protection that the content & state of the objects internally used by these classes are valid. You can break them, if you want to.
This decision is driven by performance measures. Considering wide use of the lists on the hot-paths, each additional check & operation starts counting. I've decided to eliminate all the checks for the sake of performance. If you feel that you need additional protection, feel free to inherit or reuse any of the provided classes to provide that.
## `this[int index]`, `Count`, `IndexOf(T item)` etc. may overflow, but...
All the classes included in this package support `long` indexing and addressing. The `LongCount` property will always provide the total no. of items stored on the list. Since `Count` is just `LongCount` type-casted to `int`, its value may overflow.
But `List<T>` addressing is kind of currently limited to `int` ranges anyway in practical scenarios. As such `Count` overflowing shouldn't be an issue when using the classes with existing code. If you use `Count` in your loops, it's recommended to replace it with `LongCount`, tough. You don't need to make any changes in `foreach` or other iterator loops. They are expected to transparently use the new classes & yield all items, beyond `int.MaxValue` range.
This is by design, for the best performance. Initially there have been checks in place to prevent overflowing & return `int.MaxValue` instead, but they proved to slow down the operations. Considering low risk for existing code, I've decided to take it out.
If you foresee the overflowing as an issue, you're welcomed to propose an improvement & file a PR with proposed solution.
# Auto-properties, new language features & code style
When you look at the code you may note, that it looks like in early c# days. Where the new language features yield the same or better performance, they are & will be used.
However, many of the new language features have proved to provide worse performance at some point in my testing. It includes auto-properties, for which I could see getters & setters generated in IL, instead of fields. Because all the classes are expected to be on the hot-paths, I've eliminated most of them.
Similarly, you will find places in code with hints or warnings disabled. The decision was always driven by performance measures. The use case & business logic was carefully reviewed to ensure it's safe to take shortcuts, before making the change. If you run into failing scenario, please feel free to create a work item against it and/or file a PR to fix it.
Finally, you'll find places with code commented out. That's to easily track what has been tried & what proved to perform worse.
At any time, if you know & can show in your benchmarks that the code can be simplified without impacting the performance, feel free to file a PR with proposed changes. Please include benchmark results from your testing to speed up things.
# Thread Safety
The `public static` members of the classes are thread safe. Any instance members are not guaranteed to be thread safe. You need to implement locking mechanism to safely use the classes in a multi-threading environment.
# Use
All the classes included in this package are meant to be direct replacements of their corresponding system classes.
* `IList<T>` ⇨ `ILongList<T>`
* `List<T>` ⇨ `RecyclableLongList<T>`
* `Queue<T>` ⇨ `RecyclableQueue<T>`
* `PriorityQueue<T>` ⇨ `RecyclableSortedList<T>`
* `SortedList<T>` ⇨ `RecyclableSortedList<T>`
* `Stack<T>` ⇨ `RecyclableStack<T>`
Examples of each of the above classes are provided below. Please refer automated tests for more.
## `RecyclableList<T>`
It's the closest equivalent to `List<T>` in regards to functionality. It supports items' removal & delivers the best performance in most scenarios. But it's limited to `int` range, tough. If you find that acceptable, my recommendation is to use this class as the direct replacement for `List<T>`.
Do note, that it implements `ILongList<T>` interface, too. This is to allow having common code base regardless, if you use the basic `RecyclableList<T>` or it's `long`-range version - `RecyclableLongList<T>`. By doing that you can switch between them with highly limited changes in code.
## `RecyclableLongList<T>`
```CSharp
public void RecyclableListExample()
{
int[] testNumbers = new[] { 1, 2, 2, 3 };
// Create
using var list = new RecyclableLongList<int>();
// Add items
foreach (var index in testNumbers)
{
list.Add(index);
}
foreach (var item in list)
{
Console.WriteLine(item);
}
// This will iterate over this[long index]
for (var itemIdx = 0; itemIdx <= list.LongCount; itemIdx++)
{
Console.WriteLine(list[itemIdx]);
}
// This will iterate over this[int index] and may overflow if there are > int.MaxValue no. of items on the list. See: Architecture Decisions
for (var itemIdx = 0; itemIdx <= list.LongCount; itemIdx++)
{
Console.WriteLine(list[itemIdx]);
}
list.Clear();
}
```
### [Benchmark results](https://github.com/mlemanczyk/Recyclable.Collections/blob/main/Recyclable.Collections.Benchmarks/Results/Benchmarks.md)
@@ -1,62 +1,62 @@
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_Add()
{
var data = TestObjects;
var dataCount = TestObjectCount;
var list = new long[dataCount];
for (var i = 0; i < dataCount; i++)
{
list[i] = data[i];
}
}
public void List_Add()
{
var data = TestObjects;
var dataCount = TestObjectCount;
var list = new List<long>();
for (var i = 0; i < dataCount; i++)
{
list.Add(data[i]);
}
}
public void PooledList_Add()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new PooledList<long>(ClearMode.Auto);
for (var i = 0; i < dataCount; i++)
{
list.Add(data[i]);
}
}
public void RecyclableList_Add()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new RecyclableList<long>();
for (var i = 0; i < dataCount; i++)
{
list.Add(data[i]);
}
}
public void RecyclableLongList_Add()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new RecyclableLongList<long>();
for (var i = 0; i < dataCount; i++)
{
list.Add(data[i]);
}
}
}
}
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_Add()
{
var data = TestObjects;
var dataCount = TestObjectCount;
var list = new long[dataCount];
for (var i = 0; i < dataCount; i++)
{
list[i] = data[i];
}
}
public void List_Add()
{
var data = TestObjects;
var dataCount = TestObjectCount;
var list = new List<long>();
for (var i = 0; i < dataCount; i++)
{
list.Add(data[i]);
}
}
public void PooledList_Add()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new PooledList<long>(ClearMode.Auto);
for (var i = 0; i < dataCount; i++)
{
list.Add(data[i]);
}
}
public void RecyclableList_Add()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new RecyclableList<long>();
for (var i = 0; i < dataCount; i++)
{
list.Add(data[i]);
}
}
public void RecyclableLongList_Add()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new RecyclableLongList<long>();
for (var i = 0; i < dataCount; i++)
{
list.Add(data[i]);
}
}
}
}
@@ -1,42 +1,42 @@
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_AddRangeWhenSourceIsArray()
{
var data = TestObjects;
var list = new long[TestObjectCount];
data.CopyTo(list, 0);
}
public void List_AddRangeWhenSourceIsArray()
{
var data = TestObjects;
var list = new List<long>();
list.AddRange(data);
}
public void PooledList_AddRangeWhenSourceIsArray()
{
var data = TestObjects;
using var list = new PooledList<long>(ClearMode.Auto);
list.AddRange(data);
}
public void RecyclableList_AddRangeWhenSourceIsArray()
{
var data = TestObjects;
using var list = new RecyclableList<long>();
list.AddRange(data);
}
public void RecyclableLongList_AddRangeWhenSourceIsArray()
{
var data = TestObjects;
using var list = new RecyclableLongList<long>(minBlockSize: BlockSize);
list.AddRange(data);
}
}
}
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_AddRangeWhenSourceIsArray()
{
var data = TestObjects;
var list = new long[TestObjectCount];
data.CopyTo(list, 0);
}
public void List_AddRangeWhenSourceIsArray()
{
var data = TestObjects;
var list = new List<long>();
list.AddRange(data);
}
public void PooledList_AddRangeWhenSourceIsArray()
{
var data = TestObjects;
using var list = new PooledList<long>(ClearMode.Auto);
list.AddRange(data);
}
public void RecyclableList_AddRangeWhenSourceIsArray()
{
var data = TestObjects;
using var list = new RecyclableList<long>();
list.AddRange(data);
}
public void RecyclableLongList_AddRangeWhenSourceIsArray()
{
var data = TestObjects;
using var list = new RecyclableLongList<long>(minBlockSize: BlockSize);
list.AddRange(data);
}
}
}
@@ -1,47 +1,47 @@
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_AddRangeWhenSourceIsIEnumerable()
{
var data = TestObjectsAsIEnumerable;
var list = new long[TestObjectCount];
int itemIndex = 0;
foreach (var item in data)
{
list[itemIndex++] = item;
}
}
public void List_AddRangeWhenSourceIsIEnumerable()
{
var data = TestObjectsAsIEnumerable;
var list = new List<long>();
list.AddRange(data);
}
public void PooledList_AddRangeWhenSourceIsIEnumerable()
{
var data = TestObjectsAsIEnumerable;
using var list = new PooledList<long>(ClearMode.Auto);
list.AddRange(data);
}
public void RecyclableList_AddRangeWhenSourceIsIEnumerable()
{
var data = TestObjectsAsIEnumerable;
using var list = new RecyclableList<long>();
list.AddRange(data);
}
public void RecyclableLongList_AddRangeWhenSourceIsIEnumerable()
{
var data = TestObjectsAsIEnumerable;
using var list = new RecyclableLongList<long>(minBlockSize: BlockSize);
list.AddRange(data);
}
}
}
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_AddRangeWhenSourceIsIEnumerable()
{
var data = TestObjectsAsIEnumerable;
var list = new long[TestObjectCount];
int itemIndex = 0;
foreach (var item in data)
{
list[itemIndex++] = item;
}
}
public void List_AddRangeWhenSourceIsIEnumerable()
{
var data = TestObjectsAsIEnumerable;
var list = new List<long>();
list.AddRange(data);
}
public void PooledList_AddRangeWhenSourceIsIEnumerable()
{
var data = TestObjectsAsIEnumerable;
using var list = new PooledList<long>(ClearMode.Auto);
list.AddRange(data);
}
public void RecyclableList_AddRangeWhenSourceIsIEnumerable()
{
var data = TestObjectsAsIEnumerable;
using var list = new RecyclableList<long>();
list.AddRange(data);
}
public void RecyclableLongList_AddRangeWhenSourceIsIEnumerable()
{
var data = TestObjectsAsIEnumerable;
using var list = new RecyclableLongList<long>(minBlockSize: BlockSize);
list.AddRange(data);
}
}
}
@@ -1,42 +1,42 @@
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_AddRangeWhenSourceIsIList()
{
var data = (IList<long>)TestObjectsAsRecyclableLongList;
var list = new long[TestObjectCount];
data.CopyTo(list, 0);
}
public void List_AddRangeWhenSourceIsIList()
{
var data = (IList<long>)TestObjectsAsRecyclableLongList;
var list = new List<long>();
list.AddRange(data);
}
public void PooledList_AddRangeWhenSourceIsIList()
{
var data = (IList<long>)TestObjectsAsRecyclableLongList;
using var list = new PooledList<long>(ClearMode.Auto);
list.AddRange(data);
}
public void RecyclableList_AddRangeWhenSourceIsIList()
{
var data = (IList<long>)TestObjectsAsRecyclableLongList;
using var list = new RecyclableList<long>();
list.AddRange(data);
}
public void RecyclableLongList_AddRangeWhenSourceIsIList()
{
var data = (IList<long>)TestObjectsAsRecyclableLongList;
using var list = new RecyclableLongList<long>(minBlockSize: BlockSize);
list.AddRange(data);
}
}
}
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_AddRangeWhenSourceIsIList()
{
var data = (IList<long>)TestObjectsAsRecyclableLongList;
var list = new long[TestObjectCount];
data.CopyTo(list, 0);
}
public void List_AddRangeWhenSourceIsIList()
{
var data = (IList<long>)TestObjectsAsRecyclableLongList;
var list = new List<long>();
list.AddRange(data);
}
public void PooledList_AddRangeWhenSourceIsIList()
{
var data = (IList<long>)TestObjectsAsRecyclableLongList;
using var list = new PooledList<long>(ClearMode.Auto);
list.AddRange(data);
}
public void RecyclableList_AddRangeWhenSourceIsIList()
{
var data = (IList<long>)TestObjectsAsRecyclableLongList;
using var list = new RecyclableList<long>();
list.AddRange(data);
}
public void RecyclableLongList_AddRangeWhenSourceIsIList()
{
var data = (IList<long>)TestObjectsAsRecyclableLongList;
using var list = new RecyclableLongList<long>(minBlockSize: BlockSize);
list.AddRange(data);
}
}
}
@@ -1,42 +1,42 @@
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_AddRangeWhenSourceIsList()
{
var data = TestObjectsAsList;
var list = new long[TestObjectCount];
data.CopyTo(list, 0);
}
public void List_AddRangeWhenSourceIsList()
{
var data = TestObjectsAsList;
var list = new List<long>();
list.AddRange(data);
}
public void PooledList_AddRangeWhenSourceIsList()
{
var data = TestObjectsAsList;
using var list = new PooledList<long>(ClearMode.Auto);
list.AddRange(data);
}
public void RecyclableList_AddRangeWhenSourceIsList()
{
var data = TestObjectsAsList;
using var list = new RecyclableList<long>();
list.AddRange(data);
}
public void RecyclableLongList_AddRangeWhenSourceIsList()
{
var data = TestObjectsAsList;
using var list = new RecyclableLongList<long>(minBlockSize: BlockSize);
list.AddRange(data);
}
}
}
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_AddRangeWhenSourceIsList()
{
var data = TestObjectsAsList;
var list = new long[TestObjectCount];
data.CopyTo(list, 0);
}
public void List_AddRangeWhenSourceIsList()
{
var data = TestObjectsAsList;
var list = new List<long>();
list.AddRange(data);
}
public void PooledList_AddRangeWhenSourceIsList()
{
var data = TestObjectsAsList;
using var list = new PooledList<long>(ClearMode.Auto);
list.AddRange(data);
}
public void RecyclableList_AddRangeWhenSourceIsList()
{
var data = TestObjectsAsList;
using var list = new RecyclableList<long>();
list.AddRange(data);
}
public void RecyclableLongList_AddRangeWhenSourceIsList()
{
var data = TestObjectsAsList;
using var list = new RecyclableLongList<long>(minBlockSize: BlockSize);
list.AddRange(data);
}
}
}
@@ -1,28 +1,28 @@
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void PooledList_AddRangeWhenSourceIsPooledList()
{
var data = TestObjectsAsPooledList;
using var list = new PooledList<long>(capacity: TestObjectCount, ClearMode.Auto);
list.AddRange(data);
}
public void RecyclableList_AddRangeWhenSourceIsRecyclableList()
{
var data = TestObjectsAsRecyclableList;
using var list = new RecyclableList<long>(TestObjectCount);
list.AddRange(data);
}
public void RecyclableLongList_AddRangeWhenSourceIsRecyclableLongList()
{
var data = TestObjectsAsRecyclableList;
using var list = new RecyclableLongList<long>(minBlockSize: BlockSize, initialCapacity: TestObjectCount);
list.AddRange(data);
}
}
}
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void PooledList_AddRangeWhenSourceIsPooledList()
{
var data = TestObjectsAsPooledList;
using var list = new PooledList<long>(capacity: TestObjectCount, ClearMode.Auto);
list.AddRange(data);
}
public void RecyclableList_AddRangeWhenSourceIsRecyclableList()
{
var data = TestObjectsAsRecyclableList;
using var list = new RecyclableList<long>(TestObjectCount);
list.AddRange(data);
}
public void RecyclableLongList_AddRangeWhenSourceIsRecyclableLongList()
{
var data = TestObjectsAsRecyclableList;
using var list = new RecyclableLongList<long>(minBlockSize: BlockSize, initialCapacity: TestObjectCount);
list.AddRange(data);
}
}
}
@@ -1,47 +1,47 @@
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_AddRange_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
var list = new long[dataCount];
data.CopyTo(list, 0);
}
public void List_AddRange_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
var list = new List<long>(dataCount);
list.AddRange(data);
}
public void PooledList_AddRange_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new PooledList<long>(capacity: dataCount, ClearMode.Auto);
list.AddRange(data);
}
public void RecyclableList_AddRange_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new RecyclableList<long>(dataCount);
list.AddRange(data);
}
public void RecyclableLongList_AddRange_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new RecyclableLongList<long>(minBlockSize: BlockSize, initialCapacity: dataCount);
list.AddRange(data);
}
}
}
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_AddRange_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
var list = new long[dataCount];
data.CopyTo(list, 0);
}
public void List_AddRange_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
var list = new List<long>(dataCount);
list.AddRange(data);
}
public void PooledList_AddRange_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new PooledList<long>(capacity: dataCount, ClearMode.Auto);
list.AddRange(data);
}
public void RecyclableList_AddRange_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new RecyclableList<long>(dataCount);
list.AddRange(data);
}
public void RecyclableLongList_AddRange_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new RecyclableLongList<long>(minBlockSize: BlockSize, initialCapacity: dataCount);
list.AddRange(data);
}
}
}
@@ -1,51 +1,51 @@
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void List_Add_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
var list = new List<long>(dataCount);
for (var i = 0; i < dataCount; i++)
{
list.Add(data[i]);
}
}
public void PooledList_Add_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new PooledList<long>(capacity: dataCount, ClearMode.Auto);
for (var i = 0; i < dataCount; i++)
{
list.Add(data[i]);
}
}
public void RecyclableList_Add_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new RecyclableList<long>(dataCount);
for (var i = 0; i < dataCount; i++)
{
list.Add(data[i]);
}
}
public void RecyclableLongList_Add_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new RecyclableLongList<long>(minBlockSize: BlockSize, initialCapacity: dataCount);
for (var i = 0; i < dataCount; i++)
{
list.Add(data[i]);
}
}
}
}
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void List_Add_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
var list = new List<long>(dataCount);
for (var i = 0; i < dataCount; i++)
{
list.Add(data[i]);
}
}
public void PooledList_Add_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new PooledList<long>(capacity: dataCount, ClearMode.Auto);
for (var i = 0; i < dataCount; i++)
{
list.Add(data[i]);
}
}
public void RecyclableList_Add_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new RecyclableList<long>(dataCount);
for (var i = 0; i < dataCount; i++)
{
list.Add(data[i]);
}
}
public void RecyclableLongList_Add_WithCapacity()
{
var data = TestObjects;
var dataCount = TestObjectCount;
using var list = new RecyclableLongList<long>(minBlockSize: BlockSize, initialCapacity: dataCount);
for (var i = 0; i < dataCount; i++)
{
list.Add(data[i]);
}
}
}
}
@@ -1,65 +1,65 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_BinarySearch_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.BinarySearch(list, data[i]));
DoNothing(Array.BinarySearch(list, data[^(i + 1)]));
}
}
public void List_BinarySearch_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.BinarySearch(data[i]));
DoNothing(list.BinarySearch(data[^(i + 1)]));
}
}
public void PooledList_BinarySearch_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.BinarySearch(data[i]));
DoNothing(list.BinarySearch(data[^(i + 1)]));
}
}
public void RecyclableList_BinarySearch_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.BinarySearch(data[i]));
DoNothing(list.BinarySearch(data[^(i + 1)]));
}
}
// public void RecyclableLongList_BinarySearch_BestAndWorstCases()
// {
// var data = TestObjects;
// var list = TestObjectsAsRecyclableLongList;
// var dataCount = TestObjectCountForSlowMethods;
// for (var i = 0; i < dataCount; i++)
// {
// DoNothing(list.BinarySearch(data[i]));
// DoNothing(list.BinarySearch(data[^(i + 1)]));
// }
// }
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_BinarySearch_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.BinarySearch(list, data[i]));
DoNothing(Array.BinarySearch(list, data[^(i + 1)]));
}
}
public void List_BinarySearch_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.BinarySearch(data[i]));
DoNothing(list.BinarySearch(data[^(i + 1)]));
}
}
public void PooledList_BinarySearch_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.BinarySearch(data[i]));
DoNothing(list.BinarySearch(data[^(i + 1)]));
}
}
public void RecyclableList_BinarySearch_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.BinarySearch(data[i]));
DoNothing(list.BinarySearch(data[^(i + 1)]));
}
}
// public void RecyclableLongList_BinarySearch_BestAndWorstCases()
// {
// var data = TestObjects;
// var list = TestObjectsAsRecyclableLongList;
// var dataCount = TestObjectCountForSlowMethods;
// for (var i = 0; i < dataCount; i++)
// {
// DoNothing(list.BinarySearch(data[i]));
// DoNothing(list.BinarySearch(data[^(i + 1)]));
// }
// }
}
}
@@ -1,60 +1,60 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_Contains_FirstItems()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.IndexOf(list, data[i]) >= 0);
}
}
public void List_Contains_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[i]));
}
}
public void PooledList_Contains_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[i]));
}
}
public void RecyclableList_Contains_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[i]));
}
}
public void RecyclableLongList_Contains_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[i]));
}
}
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_Contains_FirstItems()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.IndexOf(list, data[i]) >= 0);
}
}
public void List_Contains_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[i]));
}
}
public void PooledList_Contains_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[i]));
}
}
public void RecyclableList_Contains_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[i]));
}
}
public void RecyclableLongList_Contains_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[i]));
}
}
}
}
@@ -1,60 +1,60 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_Contains_LastItems()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[^(i + 1)]));
}
}
public void List_Contains_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[^(i + 1)]));
}
}
public void PooledList_Contains_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[^(i + 1)]));
}
}
public void RecyclableList_Contains_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[^(i + 1)]));
}
}
public void RecyclableLongList_Contains_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[^(i + 1)]));
}
}
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_Contains_LastItems()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[^(i + 1)]));
}
}
public void List_Contains_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[^(i + 1)]));
}
}
public void PooledList_Contains_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[^(i + 1)]));
}
}
public void RecyclableList_Contains_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[^(i + 1)]));
}
}
public void RecyclableLongList_Contains_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Contains(data[^(i + 1)]));
}
}
}
}
@@ -1,48 +1,48 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_ConvertAll()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
DoNothing(Array.ConvertAll(list, static (item) => (long) item));
}
public void List_ConvertAll()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
DoNothing(list.ConvertAll(static (item) => (long)item));
}
public void PooledList_ConvertAll()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCountForSlowMethods;
using var converted = list.ConvertAll(static (item) => (long)item);
DoNothing(converted);
}
public void RecyclableList_ConvertAll()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
using RecyclableList<long> converted = list.ConvertAll(static (item) => (long)item);
DoNothing(converted);
}
// public void RecyclableLongList_ConvertAll()
// {
// var data = TestObjects;
// var list = TestObjectsAsRecyclableLongList;
// var dataCount = TestObjectCountForSlowMethods;
// using RecyclableList<long> converted = list.ConvertAll(static (item) => (long)item);
// DoNothing(converted);
// }
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_ConvertAll()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
DoNothing(Array.ConvertAll(list, static (item) => (long) item));
}
public void List_ConvertAll()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
DoNothing(list.ConvertAll(static (item) => (long)item));
}
public void PooledList_ConvertAll()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCountForSlowMethods;
using var converted = list.ConvertAll(static (item) => (long)item);
DoNothing(converted);
}
public void RecyclableList_ConvertAll()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
using RecyclableList<long> converted = list.ConvertAll(static (item) => (long)item);
DoNothing(converted);
}
// public void RecyclableLongList_ConvertAll()
// {
// var data = TestObjects;
// var list = TestObjectsAsRecyclableLongList;
// var dataCount = TestObjectCountForSlowMethods;
// using RecyclableList<long> converted = list.ConvertAll(static (item) => (long)item);
// DoNothing(converted);
// }
}
}
@@ -1,35 +1,35 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_Count()
{
var data = TestObjects;
DoNothing(data.Length);
}
public void List_Count()
{
var data = TestObjectsAsList;
DoNothing(data.Count);
}
public void PooledList_Count()
{
var data = TestObjectsAsPooledList;
DoNothing(data.Count);
}
public void RecyclableList_Count()
{
var data = TestObjectsAsRecyclableList;
DoNothing(data.Count);
}
public void RecyclableLongList_Count()
{
var data = TestObjectsAsRecyclableLongList;
DoNothing(data.Count);
}
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_Count()
{
var data = TestObjects;
DoNothing(data.Length);
}
public void List_Count()
{
var data = TestObjectsAsList;
DoNothing(data.Count);
}
public void PooledList_Count()
{
var data = TestObjectsAsPooledList;
DoNothing(data.Count);
}
public void RecyclableList_Count()
{
var data = TestObjectsAsRecyclableList;
DoNothing(data.Count);
}
public void RecyclableLongList_Count()
{
var data = TestObjectsAsRecyclableLongList;
DoNothing(data.Count);
}
}
}
@@ -1,39 +1,39 @@
using Collections.Pooled;
#pragma warning disable CA1822, CA1825
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_Create()
{
var list = new long[0];
DoNothing(list);
}
public void List_Create()
{
var list = new List<long>();
DoNothing(list);
}
public void PooledList_Create()
{
using var list = new PooledList<long>(ClearMode.Auto);
DoNothing(list);
}
public void RecyclableList_Create()
{
using var list = new RecyclableList<long>();
DoNothing(list);
}
public void RecyclableLongList_Create()
{
using var list = new RecyclableLongList<long>();
DoNothing(list);
}
}
}
using Collections.Pooled;
#pragma warning disable CA1822, CA1825
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_Create()
{
var list = new long[0];
DoNothing(list);
}
public void List_Create()
{
var list = new List<long>();
DoNothing(list);
}
public void PooledList_Create()
{
using var list = new PooledList<long>(ClearMode.Auto);
DoNothing(list);
}
public void RecyclableList_Create()
{
using var list = new RecyclableList<long>();
DoNothing(list);
}
public void RecyclableLongList_Create()
{
using var list = new RecyclableLongList<long>();
DoNothing(list);
}
}
}
@@ -1,37 +1,37 @@
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_Create_WithCapacity()
{
var data = new long[TestObjectCount];
DoNothing(data);
}
public void List_Create_WithCapacity()
{
var list = new List<long>(TestObjectCount);
DoNothing(list);
}
public void PooledList_Create_WithCapacity()
{
using var list = new PooledList<long>(capacity: TestObjectCount, ClearMode.Auto);
DoNothing(list);
}
public void RecyclableList_Create_WithCapacity()
{
using var list = new RecyclableList<long>(TestObjectCount);
DoNothing(list);
}
public void RecyclableLongList_Create_WithCapacity()
{
using var list = new RecyclableLongList<long>(minBlockSize: BlockSize, initialCapacity: TestObjectCount);
DoNothing(list);
}
}
}
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_Create_WithCapacity()
{
var data = new long[TestObjectCount];
DoNothing(data);
}
public void List_Create_WithCapacity()
{
var list = new List<long>(TestObjectCount);
DoNothing(list);
}
public void PooledList_Create_WithCapacity()
{
using var list = new PooledList<long>(capacity: TestObjectCount, ClearMode.Auto);
DoNothing(list);
}
public void RecyclableList_Create_WithCapacity()
{
using var list = new RecyclableList<long>(TestObjectCount);
DoNothing(list);
}
public void RecyclableLongList_Create_WithCapacity()
{
using var list = new RecyclableLongList<long>(minBlockSize: BlockSize, initialCapacity: TestObjectCount);
DoNothing(list);
}
}
}
@@ -1,65 +1,65 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_Exists_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.Exists(list, (x) => x == data[i]));
DoNothing(Array.Exists(list, (x) => x == data[^(i + 1)]));
}
}
public void List_Exists_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Exists((x) => x == data[i]));
DoNothing(list.Exists((x) => x == data[^(i + 1)]));
}
}
public void PooledList_Exists_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Exists((x) => x == data[i]));
DoNothing(list.Exists((x) => x == data[^(i + 1)]));
}
}
public void RecyclableList_Exists_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Exists((x) => x == data[i]));
DoNothing(list.Exists((x) => x == data[^(i + 1)]));
}
}
// public void RecyclableLongList_Exists_BestAndWorstCases()
// {
// var data = TestObjects;
// var list = TestObjectsAsRecyclableLongList;
// var dataCount = TestObjectCountForSlowMethods;
// for (var i = 0; i < dataCount; i++)
// {
// DoNothing(list.Exists((x) => x == data[i]));
// DoNothing(list.Exists((x) => x == data[^(i + 1)]));
// }
// }
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_Exists_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.Exists(list, (x) => x == data[i]));
DoNothing(Array.Exists(list, (x) => x == data[^(i + 1)]));
}
}
public void List_Exists_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Exists((x) => x == data[i]));
DoNothing(list.Exists((x) => x == data[^(i + 1)]));
}
}
public void PooledList_Exists_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Exists((x) => x == data[i]));
DoNothing(list.Exists((x) => x == data[^(i + 1)]));
}
}
public void RecyclableList_Exists_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Exists((x) => x == data[i]));
DoNothing(list.Exists((x) => x == data[^(i + 1)]));
}
}
// public void RecyclableLongList_Exists_BestAndWorstCases()
// {
// var data = TestObjects;
// var list = TestObjectsAsRecyclableLongList;
// var dataCount = TestObjectCountForSlowMethods;
// for (var i = 0; i < dataCount; i++)
// {
// DoNothing(list.Exists((x) => x == data[i]));
// DoNothing(list.Exists((x) => x == data[^(i + 1)]));
// }
// }
}
}
@@ -1,80 +1,80 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_FindAll_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.FindAll(list, (item) => item == data[i]));
DoNothing(Array.FindAll(list, (item) => item == data[^(i + 1)]));
}
}
public void List_FindAll_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.FindAll((item) => item == data[i]));
DoNothing(list.FindAll((item) => item == data[^(i + 1)]));
}
}
public void PooledList_FindAll_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
var temp = list.FindAll((item) => item == data[i]);
DoNothing(temp);
temp.Dispose();
temp = list.FindAll((item) => item == data[^(i + 1)]);
DoNothing(temp);
temp.Dispose();
}
}
public void RecyclableList_FindAll_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
var temp = list.FindAll((item) => item == data[i]);
DoNothing(temp);
temp.Dispose();
temp = list.FindAll((item) => item == data[^(i + 1)]);
DoNothing(temp);
temp.Dispose();
}
}
// public void RecyclableLongList_FindAll_BestAndWorstCases()
// {
// var data = TestObjects;
// var list = TestObjectsAsRecyclableLongList;
// var dataCount = TestObjectCountForSlowMethods;
// for (var i = 0; i < dataCount; i++)
// {
// var temp = list.FindAll((item) => item == data[i]);
// DoNothing(temp);
// temp.Dispose();
// temp = list.FindAll((item) => item == data[^(i + 1)]);
// DoNothing(temp);
// temp.Dispose();
// }
// }
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_FindAll_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.FindAll(list, (item) => item == data[i]));
DoNothing(Array.FindAll(list, (item) => item == data[^(i + 1)]));
}
}
public void List_FindAll_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.FindAll((item) => item == data[i]));
DoNothing(list.FindAll((item) => item == data[^(i + 1)]));
}
}
public void PooledList_FindAll_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
var temp = list.FindAll((item) => item == data[i]);
DoNothing(temp);
temp.Dispose();
temp = list.FindAll((item) => item == data[^(i + 1)]);
DoNothing(temp);
temp.Dispose();
}
}
public void RecyclableList_FindAll_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
var temp = list.FindAll((item) => item == data[i]);
DoNothing(temp);
temp.Dispose();
temp = list.FindAll((item) => item == data[^(i + 1)]);
DoNothing(temp);
temp.Dispose();
}
}
// public void RecyclableLongList_FindAll_BestAndWorstCases()
// {
// var data = TestObjects;
// var list = TestObjectsAsRecyclableLongList;
// var dataCount = TestObjectCountForSlowMethods;
// for (var i = 0; i < dataCount; i++)
// {
// var temp = list.FindAll((item) => item == data[i]);
// DoNothing(temp);
// temp.Dispose();
// temp = list.FindAll((item) => item == data[^(i + 1)]);
// DoNothing(temp);
// temp.Dispose();
// }
// }
}
}
@@ -1,63 +1,63 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_FindLastIndex_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
// Console.WriteLine($"{nameof(TestObjectCount)}={TestObjectCount}");
// Console.WriteLine($"{nameof(TestObjectCountForSlowMethods)}={TestObjectCountForSlowMethods}");
for (var i = 0; i < dataCount; i++)
{
// Console.WriteLine($"data.Length={data.Length}");
// Console.WriteLine($"{nameof(TestObjectCount)}={TestObjectCount}");
// Console.WriteLine($"{nameof(TestObjectCountForSlowMethods)}={TestObjectCountForSlowMethods}");
DoNothing(Array.FindLastIndex(list, 0, (x) => x == data[i]));
DoNothing(Array.FindLastIndex(list, 0, (x) => x == data[^(i + 1)]));
}
}
public void List_FindLastIndex_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
// Console.WriteLine($"{nameof(TestObjectCount)}={TestObjectCount}");
// Console.WriteLine($"{nameof(TestObjectCountForSlowMethods)}={TestObjectCountForSlowMethods}");
for (var i = 0; i < dataCount; i++)
{
// Console.WriteLine($"data.Length={data.Length}");
// Console.WriteLine($"{nameof(TestObjectCount)}={TestObjectCount}");
// Console.WriteLine($"{nameof(TestObjectCountForSlowMethods)}={TestObjectCountForSlowMethods}");
DoNothing(list.FindLastIndex(0, (x) => x == data[i]));
DoNothing(list.FindLastIndex(0, (x) => x == data[^(i + 1)]));
}
}
public void RecyclableList_FindLastIndex_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.FindLastIndex(0, (x) => x == data[i]));
DoNothing(list.FindLastIndex(0, (x) => x == data[^(i + 1)]));
}
}
// public void RecyclableLongList_FindLastIndex_BestAndWorstCases()
// {
// var data = TestObjects;
// var list = TestObjectsAsRecyclableLongList;
// var dataCount = TestObjectCountForSlowMethods;
// for (var i = 0; i < dataCount; i++)
// {
// DoNothing(list.FindLastIndex(0, (x) => x == data[i]));
// DoNothing(list.FindLastIndex(0, (x) => x == data[^(i + 1)]));
// }
// }
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_FindLastIndex_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
// Console.WriteLine($"{nameof(TestObjectCount)}={TestObjectCount}");
// Console.WriteLine($"{nameof(TestObjectCountForSlowMethods)}={TestObjectCountForSlowMethods}");
for (var i = 0; i < dataCount; i++)
{
// Console.WriteLine($"data.Length={data.Length}");
// Console.WriteLine($"{nameof(TestObjectCount)}={TestObjectCount}");
// Console.WriteLine($"{nameof(TestObjectCountForSlowMethods)}={TestObjectCountForSlowMethods}");
DoNothing(Array.FindLastIndex(list, 0, (x) => x == data[i]));
DoNothing(Array.FindLastIndex(list, 0, (x) => x == data[^(i + 1)]));
}
}
public void List_FindLastIndex_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
// Console.WriteLine($"{nameof(TestObjectCount)}={TestObjectCount}");
// Console.WriteLine($"{nameof(TestObjectCountForSlowMethods)}={TestObjectCountForSlowMethods}");
for (var i = 0; i < dataCount; i++)
{
// Console.WriteLine($"data.Length={data.Length}");
// Console.WriteLine($"{nameof(TestObjectCount)}={TestObjectCount}");
// Console.WriteLine($"{nameof(TestObjectCountForSlowMethods)}={TestObjectCountForSlowMethods}");
DoNothing(list.FindLastIndex(0, (x) => x == data[i]));
DoNothing(list.FindLastIndex(0, (x) => x == data[^(i + 1)]));
}
}
public void RecyclableList_FindLastIndex_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.FindLastIndex(0, (x) => x == data[i]));
DoNothing(list.FindLastIndex(0, (x) => x == data[^(i + 1)]));
}
}
// public void RecyclableLongList_FindLastIndex_BestAndWorstCases()
// {
// var data = TestObjects;
// var list = TestObjectsAsRecyclableLongList;
// var dataCount = TestObjectCountForSlowMethods;
// for (var i = 0; i < dataCount; i++)
// {
// DoNothing(list.FindLastIndex(0, (x) => x == data[i]));
// DoNothing(list.FindLastIndex(0, (x) => x == data[^(i + 1)]));
// }
// }
}
}
@@ -1,53 +1,53 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_FindLast_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.FindLast(list, (x) => x == data[i]));
DoNothing(Array.FindLast(list, (x) => x == data[^(i + 1)]));
}
}
public void List_FindLast_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.FindLast((x) => x == data[i]));
DoNothing(list.FindLast((x) => x == data[^(i + 1)]));
}
}
public void RecyclableList_FindLast_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.FindLast((x) => x == data[i]));
DoNothing(list.FindLast((x) => x == data[^(i + 1)]));
}
}
// public void RecyclableLongList_FindLast_BestAndWorstCases()
// {
// var data = TestObjects;
// var list = TestObjectsAsRecyclableLongList;
// var dataCount = TestObjectCountForSlowMethods;
// for (var i = 0; i < dataCount; i++)
// {
// DoNothing(list.FindLast((x) => x == data[i]));
// DoNothing(list.FindLast((x) => x == data[^(i + 1)]));
// }
// }
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_FindLast_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.FindLast(list, (x) => x == data[i]));
DoNothing(Array.FindLast(list, (x) => x == data[^(i + 1)]));
}
}
public void List_FindLast_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.FindLast((x) => x == data[i]));
DoNothing(list.FindLast((x) => x == data[^(i + 1)]));
}
}
public void RecyclableList_FindLast_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.FindLast((x) => x == data[i]));
DoNothing(list.FindLast((x) => x == data[^(i + 1)]));
}
}
// public void RecyclableLongList_FindLast_BestAndWorstCases()
// {
// var data = TestObjects;
// var list = TestObjectsAsRecyclableLongList;
// var dataCount = TestObjectCountForSlowMethods;
// for (var i = 0; i < dataCount; i++)
// {
// DoNothing(list.FindLast((x) => x == data[i]));
// DoNothing(list.FindLast((x) => x == data[^(i + 1)]));
// }
// }
}
}
@@ -1,53 +1,53 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_Find_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.Find(list, (x) => x == data[i]));
DoNothing(Array.Find(list, (x) => x == data[^(i + 1)]));
}
}
public void List_Find_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Find((x) => x == data[i]));
DoNothing(list.Find((x) => x == data[^(i + 1)]));
}
}
public void RecyclableList_Find_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Find((x) => x == data[i]));
DoNothing(list.Find((x) => x == data[^(i + 1)]));
}
}
// public void RecyclableLongList_Find_BestAndWorstCases()
// {
// var data = TestObjects;
// var list = TestObjectsAsRecyclableLongList;
// var dataCount = TestObjectCountForSlowMethods;
// for (var i = 0; i < dataCount; i++)
// {
// DoNothing(list.Find((x) => x == data[i]));
// DoNothing(list.Find((x) => x == data[^(i + 1)]));
// }
// }
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_Find_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.Find(list, (x) => x == data[i]));
DoNothing(Array.Find(list, (x) => x == data[^(i + 1)]));
}
}
public void List_Find_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Find((x) => x == data[i]));
DoNothing(list.Find((x) => x == data[^(i + 1)]));
}
}
public void RecyclableList_Find_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Find((x) => x == data[i]));
DoNothing(list.Find((x) => x == data[^(i + 1)]));
}
}
// public void RecyclableLongList_Find_BestAndWorstCases()
// {
// var data = TestObjects;
// var list = TestObjectsAsRecyclableLongList;
// var dataCount = TestObjectCountForSlowMethods;
// for (var i = 0; i < dataCount; i++)
// {
// DoNothing(list.Find((x) => x == data[i]));
// DoNothing(list.Find((x) => x == data[^(i + 1)]));
// }
// }
}
}
@@ -1,85 +1,85 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_ForEach()
{
foreach (var item in TestObjects)
{
DoNothing(item);
}
}
public void Array_VersionedForEach()
{
foreach (var item in TestObjects)
{
DoNothing(item);
}
}
public void List_ForEach()
{
foreach (var item in TestObjectsAsList)
{
DoNothing(item);
}
}
public void List_VersionedForEach()
{
foreach (var item in TestObjectsAsList)
{
DoNothing(item);
}
}
public void PooledList_ForEach()
{
foreach (var item in TestObjectsAsPooledList)
{
DoNothing(item);
}
}
public void PooledList_VersionedForEach()
{
foreach (var item in TestObjectsAsPooledList)
{
DoNothing(item);
}
}
public void RecyclableList_ForEach()
{
foreach (var item in TestObjectsAsRecyclableList)
{
DoNothing(item);
}
}
public void RecyclableList_VersionedForEach()
{
foreach (var item in (IRecyclableVersionedList<long>)TestObjectsAsRecyclableList)
{
DoNothing(item);
}
}
public void RecyclableLongList_ForEach()
{
foreach (var item in TestObjectsAsRecyclableLongList)
{
DoNothing(item);
}
}
public void RecyclableLongList_VersionedForEach()
{
foreach (var item in (IRecyclableVersionedLongList<long>)TestObjectsAsRecyclableLongList)
{
DoNothing(item);
}
}
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_ForEach()
{
foreach (var item in TestObjects)
{
DoNothing(item);
}
}
public void Array_VersionedForEach()
{
foreach (var item in TestObjects)
{
DoNothing(item);
}
}
public void List_ForEach()
{
foreach (var item in TestObjectsAsList)
{
DoNothing(item);
}
}
public void List_VersionedForEach()
{
foreach (var item in TestObjectsAsList)
{
DoNothing(item);
}
}
public void PooledList_ForEach()
{
foreach (var item in TestObjectsAsPooledList)
{
DoNothing(item);
}
}
public void PooledList_VersionedForEach()
{
foreach (var item in TestObjectsAsPooledList)
{
DoNothing(item);
}
}
public void RecyclableList_ForEach()
{
foreach (var item in TestObjectsAsRecyclableList)
{
DoNothing(item);
}
}
public void RecyclableList_VersionedForEach()
{
foreach (var item in (IRecyclableVersionedList<long>)TestObjectsAsRecyclableList)
{
DoNothing(item);
}
}
public void RecyclableLongList_ForEach()
{
foreach (var item in TestObjectsAsRecyclableLongList)
{
DoNothing(item);
}
}
public void RecyclableLongList_VersionedForEach()
{
foreach (var item in (IRecyclableVersionedLongList<long>)TestObjectsAsRecyclableLongList)
{
DoNothing(item);
}
}
}
}
@@ -1,158 +1,158 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_GetItem()
{
var data = TestObjects;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(data[i]);
}
}
public void List_GetItem()
{
var data = TestObjectsAsList;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(data[i]);
}
}
public void PooledList_GetItem()
{
var data = TestObjectsAsPooledList;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(data[i]);
}
}
public void RecyclableList_GetItem()
{
var data = TestObjectsAsRecyclableList;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(data[i]);
}
}
public void RecyclableLongList_GetItem()
{
var data = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(data[i]);
}
}
public void RecyclableLongList_GetItem_AsSpan()
{
var data = TestObjectsAsRecyclableLongList;
int blockSize = data.BlockSize;
int nextItemBlockIndex = data.NextItemBlockIndex;
var memoryBlocks = new Span<long[]>(data.AsArray, 0, data.LastBlockWithData + 1);
for (var blockIndex = 0; blockIndex < nextItemBlockIndex; blockIndex++)
{
var memoryBlock = new Span<long>(memoryBlocks[blockIndex], 0, blockSize);
for (var itemIndex = 0; itemIndex < blockSize; itemIndex++)
{
DoNothing(memoryBlock[itemIndex]);
}
}
if (nextItemBlockIndex == data.LastBlockWithData)
{
var memoryBlock = new Span<long>(memoryBlocks[nextItemBlockIndex], 0, data.NextItemIndex);
for (var itemIndex = 0; itemIndex < memoryBlock.Length; itemIndex++)
{
DoNothing(memoryBlock[itemIndex]);
}
}
}
public void RecyclableLongList_GetItem_AsArray()
{
var data = TestObjectsAsRecyclableLongList;
int blockSize = data.BlockSize;
int nextItemBlockIndex = data.NextItemBlockIndex;
var memoryBlocks = data.AsArray;
for (var blockIndex = 0; blockIndex < nextItemBlockIndex; blockIndex++)
{
var memoryBlock = memoryBlocks[blockIndex];
for (var itemIndex = 0; itemIndex < blockSize; itemIndex++)
{
DoNothing(memoryBlock[itemIndex]);
}
}
if (nextItemBlockIndex == data.LastBlockWithData)
{
var memoryBlock = memoryBlocks[nextItemBlockIndex];
blockSize = data.NextItemIndex;
for (var itemIndex = 0; itemIndex < blockSize; itemIndex++)
{
DoNothing(memoryBlock[itemIndex]);
}
}
}
public void RecyclableLongList_GetItem_AsForEach()
{
var data = TestObjectsAsRecyclableLongList;
foreach (var item in data)
{
DoNothing(item);
}
}
public void RecyclableLongList_GetItem_AsWhile()
{
var data = TestObjectsAsRecyclableLongList;
long dataCount = TestObjectCount;
if (dataCount == 0)
{
return;
}
int blockSize = data.BlockSize;
int nextItemBlockIndex = data.NextItemBlockIndex;
int itemIndex = 0;
int blockIndex = 0;
var memoryBlocks = data.AsArray;
var memoryBlock = memoryBlocks[0];
while (blockIndex < nextItemBlockIndex)
{
DoNothing(memoryBlock[itemIndex]);
if (itemIndex + 1 < blockSize)
{
itemIndex++;
}
else
{
blockIndex++;
memoryBlock = memoryBlocks[blockIndex];
itemIndex = 0;
}
}
if (nextItemBlockIndex == data.LastBlockWithData)
{
memoryBlock = memoryBlocks[nextItemBlockIndex];
blockSize = data.NextItemIndex;
while (itemIndex < blockSize)
{
DoNothing(memoryBlock[itemIndex++]);
}
}
}
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_GetItem()
{
var data = TestObjects;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(data[i]);
}
}
public void List_GetItem()
{
var data = TestObjectsAsList;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(data[i]);
}
}
public void PooledList_GetItem()
{
var data = TestObjectsAsPooledList;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(data[i]);
}
}
public void RecyclableList_GetItem()
{
var data = TestObjectsAsRecyclableList;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(data[i]);
}
}
public void RecyclableLongList_GetItem()
{
var data = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(data[i]);
}
}
public void RecyclableLongList_GetItem_AsSpan()
{
var data = TestObjectsAsRecyclableLongList;
int blockSize = data.BlockSize;
int nextItemBlockIndex = data.NextItemBlockIndex;
var memoryBlocks = new Span<long[]>(data.AsArray, 0, data.LastBlockWithData + 1);
for (var blockIndex = 0; blockIndex < nextItemBlockIndex; blockIndex++)
{
var memoryBlock = new Span<long>(memoryBlocks[blockIndex], 0, blockSize);
for (var itemIndex = 0; itemIndex < blockSize; itemIndex++)
{
DoNothing(memoryBlock[itemIndex]);
}
}
if (nextItemBlockIndex == data.LastBlockWithData)
{
var memoryBlock = new Span<long>(memoryBlocks[nextItemBlockIndex], 0, data.NextItemIndex);
for (var itemIndex = 0; itemIndex < memoryBlock.Length; itemIndex++)
{
DoNothing(memoryBlock[itemIndex]);
}
}
}
public void RecyclableLongList_GetItem_AsArray()
{
var data = TestObjectsAsRecyclableLongList;
int blockSize = data.BlockSize;
int nextItemBlockIndex = data.NextItemBlockIndex;
var memoryBlocks = data.AsArray;
for (var blockIndex = 0; blockIndex < nextItemBlockIndex; blockIndex++)
{
var memoryBlock = memoryBlocks[blockIndex];
for (var itemIndex = 0; itemIndex < blockSize; itemIndex++)
{
DoNothing(memoryBlock[itemIndex]);
}
}
if (nextItemBlockIndex == data.LastBlockWithData)
{
var memoryBlock = memoryBlocks[nextItemBlockIndex];
blockSize = data.NextItemIndex;
for (var itemIndex = 0; itemIndex < blockSize; itemIndex++)
{
DoNothing(memoryBlock[itemIndex]);
}
}
}
public void RecyclableLongList_GetItem_AsForEach()
{
var data = TestObjectsAsRecyclableLongList;
foreach (var item in data)
{
DoNothing(item);
}
}
public void RecyclableLongList_GetItem_AsWhile()
{
var data = TestObjectsAsRecyclableLongList;
long dataCount = TestObjectCount;
if (dataCount == 0)
{
return;
}
int blockSize = data.BlockSize;
int nextItemBlockIndex = data.NextItemBlockIndex;
int itemIndex = 0;
int blockIndex = 0;
var memoryBlocks = data.AsArray;
var memoryBlock = memoryBlocks[0];
while (blockIndex < nextItemBlockIndex)
{
DoNothing(memoryBlock[itemIndex]);
if (itemIndex + 1 < blockSize)
{
itemIndex++;
}
else
{
blockIndex++;
memoryBlock = memoryBlocks[blockIndex];
itemIndex = 0;
}
}
if (nextItemBlockIndex == data.LastBlockWithData)
{
memoryBlock = memoryBlocks[nextItemBlockIndex];
blockSize = data.NextItemIndex;
while (itemIndex < blockSize)
{
DoNothing(memoryBlock[itemIndex++]);
}
}
}
}
}
@@ -1,77 +1,77 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_IndexOf_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.IndexOf(list, data[i]));
DoNothing(Array.IndexOf(list, data[^(i + 1)]));
}
}
public void List_IndexOf_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
DoNothing(list.IndexOf(data[^(i + 1)]));
}
}
public void PooledList_IndexOf_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
DoNothing(list.IndexOf(data[^(i + 1)]));
}
}
public void RecyclableList_IndexOf_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
DoNothing(list.IndexOf(data[^(i + 1)]));
}
}
public void RecyclableLongList_IndexOf_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
DoNothing(list.IndexOf(data[^(i + 1)]));
}
}
public void RecyclableLongList_LongIndexOf_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
DoNothing(list.LongIndexOf(data[^(i + 1)]));
}
}
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_IndexOf_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.IndexOf(list, data[i]));
DoNothing(Array.IndexOf(list, data[^(i + 1)]));
}
}
public void List_IndexOf_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
DoNothing(list.IndexOf(data[^(i + 1)]));
}
}
public void PooledList_IndexOf_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
DoNothing(list.IndexOf(data[^(i + 1)]));
}
}
public void RecyclableList_IndexOf_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
DoNothing(list.IndexOf(data[^(i + 1)]));
}
}
public void RecyclableLongList_IndexOf_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
DoNothing(list.IndexOf(data[^(i + 1)]));
}
}
public void RecyclableLongList_LongIndexOf_BestAndWorstCases()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
DoNothing(list.LongIndexOf(data[^(i + 1)]));
}
}
}
}
@@ -1,71 +1,71 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_IndexOf_FirstItems()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.IndexOf(list, data[i]));
}
}
public void List_IndexOf_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
}
}
public void PooledList_IndexOf_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
}
}
public void RecyclableList_IndexOf_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
}
}
public void RecyclableLongList_IndexOf_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
}
}
public void RecyclableLongList_LongIndexOf_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.LongIndexOf(data[i]));
}
}
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_IndexOf_FirstItems()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.IndexOf(list, data[i]));
}
}
public void List_IndexOf_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
}
}
public void PooledList_IndexOf_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
}
}
public void RecyclableList_IndexOf_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
}
}
public void RecyclableLongList_IndexOf_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[i]));
}
}
public void RecyclableLongList_LongIndexOf_FirstItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.LongIndexOf(data[i]));
}
}
}
}
@@ -1,71 +1,71 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_IndexOf_LastItems()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.IndexOf(list, data[^(i + 1)]));
}
}
public void List_IndexOf_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[^(i + 1)]));
}
}
public void PooledList_IndexOf_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[^(i + 1)]));
}
}
public void RecyclableList_IndexOf_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[^(i + 1)]));
}
}
public void RecyclableLongList_IndexOf_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[^(i + 1)]));
}
}
public void RecyclableLongList_LongIndexOf_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.LongIndexOf(data[^(i + 1)]));
}
}
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_IndexOf_LastItems()
{
var data = TestObjects;
var list = TestObjects;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(Array.IndexOf(list, data[^(i + 1)]));
}
}
public void List_IndexOf_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[^(i + 1)]));
}
}
public void PooledList_IndexOf_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsPooledList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[^(i + 1)]));
}
}
public void RecyclableList_IndexOf_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[^(i + 1)]));
}
}
public void RecyclableLongList_IndexOf_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.IndexOf(data[^(i + 1)]));
}
}
public void RecyclableLongList_LongIndexOf_LastItems()
{
var data = TestObjects;
var list = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCountForSlowMethods;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.LongIndexOf(data[^(i + 1)]));
}
}
}
}
@@ -1,17 +1,17 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_LongCount()
{
var data = TestObjects;
DoNothing(data.LongLength);
}
public void RecyclableLongList_LongCount()
{
var data = TestObjectsAsRecyclableLongList;
DoNothing(data.LongCount);
}
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_LongCount()
{
var data = TestObjects;
DoNothing(data.LongLength);
}
public void RecyclableLongList_LongCount()
{
var data = TestObjectsAsRecyclableLongList;
DoNothing(data.LongCount);
}
}
}
@@ -1,102 +1,102 @@
using System.Reflection;
using BenchmarkDotNet.Attributes;
namespace Recyclable.Collections.Benchmarks
{
public enum RecyclableCollectionsBenchmarkType
{
Unknown, Add_WithCapacity, Add, AddRange_WithCapacity, AddRangeWhenSourceIsArray, AddRangeWhenSourceIsIEnumerable,
AddRangeWhenSourceIsIList, AddRangeWhenSourceIsList, AddRangeWhenSourceIsSameType, BinarySearch_BestAndWorstCases,
Contains_FirstItems, Contains_LastItems, ConvertAll, Count, Create_WithCapacity, Create, Exists_BestAndWorstCases,
Find_BestAndWorstCases, FindAll_BestAndWorstCases, FindLast_BestAndWorstCases, FindLastIndex_BestAndWorstCases,
ForEach, GetItem, IndexOf_BestAndWorstCases,IndexOf_FirstItems, IndexOf_LastItems, LongCount,
Remove_FirstItems, Remove_LastItems, RemoveAt_FirstItems, RemoveAt_LastItems,
SetItem, VersionedForEach
}
public partial class RecyclableCollectionsBenchmarks : RecyclableBenchmarkBase<RecyclableCollectionsBenchmarkSource>
{
public RecyclableCollectionsBenchmarks()
{
TestObjectCount = 1;
//BaseDataType = RecyclableCollectionsBenchmarkSource.List;
DataType = RecyclableCollectionsBenchmarkSource.RecyclableList;
}
[Params
(
RecyclableCollectionsBenchmarkType.Add_WithCapacity,
RecyclableCollectionsBenchmarkType.Add,
RecyclableCollectionsBenchmarkType.AddRange_WithCapacity,
RecyclableCollectionsBenchmarkType.AddRangeWhenSourceIsArray,
RecyclableCollectionsBenchmarkType.AddRangeWhenSourceIsIEnumerable,
RecyclableCollectionsBenchmarkType.AddRangeWhenSourceIsIList,
RecyclableCollectionsBenchmarkType.AddRangeWhenSourceIsList,
RecyclableCollectionsBenchmarkType.AddRangeWhenSourceIsSameType,
RecyclableCollectionsBenchmarkType.BinarySearch_BestAndWorstCases,
RecyclableCollectionsBenchmarkType.Contains_FirstItems,
RecyclableCollectionsBenchmarkType.Contains_LastItems,
RecyclableCollectionsBenchmarkType.ConvertAll,
RecyclableCollectionsBenchmarkType.Count,
RecyclableCollectionsBenchmarkType.Create_WithCapacity,
RecyclableCollectionsBenchmarkType.Create,
RecyclableCollectionsBenchmarkType.Exists_BestAndWorstCases,
RecyclableCollectionsBenchmarkType.Find_BestAndWorstCases,
RecyclableCollectionsBenchmarkType.FindAll_BestAndWorstCases,
RecyclableCollectionsBenchmarkType.FindLast_BestAndWorstCases,
RecyclableCollectionsBenchmarkType.FindLastIndex_BestAndWorstCases,
RecyclableCollectionsBenchmarkType.ForEach,
RecyclableCollectionsBenchmarkType.GetItem,
RecyclableCollectionsBenchmarkType.IndexOf_BestAndWorstCases,
RecyclableCollectionsBenchmarkType.IndexOf_FirstItems,
RecyclableCollectionsBenchmarkType.IndexOf_LastItems,
RecyclableCollectionsBenchmarkType.LongCount,
RecyclableCollectionsBenchmarkType.Remove_FirstItems,
RecyclableCollectionsBenchmarkType.Remove_LastItems,
RecyclableCollectionsBenchmarkType.RemoveAt_FirstItems,
RecyclableCollectionsBenchmarkType.RemoveAt_LastItems,
RecyclableCollectionsBenchmarkType.SetItem,
RecyclableCollectionsBenchmarkType.VersionedForEach
)]
public RecyclableCollectionsBenchmarkType TestCase { get; set; } = RecyclableCollectionsBenchmarkType.IndexOf_BestAndWorstCases;
[Params
(
RecyclableCollectionsBenchmarkSource.List
)]
public override RecyclableCollectionsBenchmarkSource BaseDataType { get => base.BaseDataType; set => base.BaseDataType = value; }
[Params
(
RecyclableCollectionsBenchmarkSource.Array,
RecyclableCollectionsBenchmarkSource.List,
RecyclableCollectionsBenchmarkSource.PooledList,
RecyclableCollectionsBenchmarkSource.RecyclableList,
RecyclableCollectionsBenchmarkSource.RecyclableLongList
)]
public override RecyclableCollectionsBenchmarkSource DataType { get => base.DataType; set => base.DataType = value; }
protected override Action? GetTestMethod(RecyclableCollectionsBenchmarkSource benchmarkType)
=> TestCase switch
{
RecyclableCollectionsBenchmarkType.AddRangeWhenSourceIsSameType => (Action?)GetType().GetMethod($"{benchmarkType}_AddRangeWhenSourceIs{benchmarkType}", BindingFlags.Instance | BindingFlags.Public)?.CreateDelegate(typeof(Action), this),
_ => (Action?) GetType().GetMethod($"{benchmarkType}_{TestCase}", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)?.CreateDelegate(typeof(Action), this) ?? throw new InvalidOperationException($"Method {benchmarkType}_{TestCase} not found in class {GetType().FullName}")
};
public override void Setup()
{
base.Setup();
switch(TestCase)
{
case RecyclableCollectionsBenchmarkType.AddRangeWhenSourceIsIList:
PrepareData(RecyclableCollectionsBenchmarkSource.RecyclableLongList);
break;
case RecyclableCollectionsBenchmarkType.AddRangeWhenSourceIsSameType:
PrepareData(RecyclableCollectionsBenchmarkSource.RecyclableList);
break;
}
}
}
}
using System.Reflection;
using BenchmarkDotNet.Attributes;
namespace Recyclable.Collections.Benchmarks
{
public enum RecyclableCollectionsBenchmarkType
{
Unknown, Add_WithCapacity, Add, AddRange_WithCapacity, AddRangeWhenSourceIsArray, AddRangeWhenSourceIsIEnumerable,
AddRangeWhenSourceIsIList, AddRangeWhenSourceIsList, AddRangeWhenSourceIsSameType, BinarySearch_BestAndWorstCases,
Contains_FirstItems, Contains_LastItems, ConvertAll, Count, Create_WithCapacity, Create, Exists_BestAndWorstCases,
Find_BestAndWorstCases, FindAll_BestAndWorstCases, FindLast_BestAndWorstCases, FindLastIndex_BestAndWorstCases,
ForEach, GetItem, IndexOf_BestAndWorstCases,IndexOf_FirstItems, IndexOf_LastItems, LongCount,
Remove_FirstItems, Remove_LastItems, RemoveAt_FirstItems, RemoveAt_LastItems,
SetItem, VersionedForEach
}
public partial class RecyclableCollectionsBenchmarks : RecyclableBenchmarkBase<RecyclableCollectionsBenchmarkSource>
{
public RecyclableCollectionsBenchmarks()
{
TestObjectCount = 1;
//BaseDataType = RecyclableCollectionsBenchmarkSource.List;
DataType = RecyclableCollectionsBenchmarkSource.RecyclableList;
}
[Params
(
RecyclableCollectionsBenchmarkType.Add_WithCapacity,
RecyclableCollectionsBenchmarkType.Add,
RecyclableCollectionsBenchmarkType.AddRange_WithCapacity,
RecyclableCollectionsBenchmarkType.AddRangeWhenSourceIsArray,
RecyclableCollectionsBenchmarkType.AddRangeWhenSourceIsIEnumerable,
RecyclableCollectionsBenchmarkType.AddRangeWhenSourceIsIList,
RecyclableCollectionsBenchmarkType.AddRangeWhenSourceIsList,
RecyclableCollectionsBenchmarkType.AddRangeWhenSourceIsSameType,
RecyclableCollectionsBenchmarkType.BinarySearch_BestAndWorstCases,
RecyclableCollectionsBenchmarkType.Contains_FirstItems,
RecyclableCollectionsBenchmarkType.Contains_LastItems,
RecyclableCollectionsBenchmarkType.ConvertAll,
RecyclableCollectionsBenchmarkType.Count,
RecyclableCollectionsBenchmarkType.Create_WithCapacity,
RecyclableCollectionsBenchmarkType.Create,
RecyclableCollectionsBenchmarkType.Exists_BestAndWorstCases,
RecyclableCollectionsBenchmarkType.Find_BestAndWorstCases,
RecyclableCollectionsBenchmarkType.FindAll_BestAndWorstCases,
RecyclableCollectionsBenchmarkType.FindLast_BestAndWorstCases,
RecyclableCollectionsBenchmarkType.FindLastIndex_BestAndWorstCases,
RecyclableCollectionsBenchmarkType.ForEach,
RecyclableCollectionsBenchmarkType.GetItem,
RecyclableCollectionsBenchmarkType.IndexOf_BestAndWorstCases,
RecyclableCollectionsBenchmarkType.IndexOf_FirstItems,
RecyclableCollectionsBenchmarkType.IndexOf_LastItems,
RecyclableCollectionsBenchmarkType.LongCount,
RecyclableCollectionsBenchmarkType.Remove_FirstItems,
RecyclableCollectionsBenchmarkType.Remove_LastItems,
RecyclableCollectionsBenchmarkType.RemoveAt_FirstItems,
RecyclableCollectionsBenchmarkType.RemoveAt_LastItems,
RecyclableCollectionsBenchmarkType.SetItem,
RecyclableCollectionsBenchmarkType.VersionedForEach
)]
public RecyclableCollectionsBenchmarkType TestCase { get; set; } = RecyclableCollectionsBenchmarkType.IndexOf_BestAndWorstCases;
[Params
(
RecyclableCollectionsBenchmarkSource.List
)]
public override RecyclableCollectionsBenchmarkSource BaseDataType { get => base.BaseDataType; set => base.BaseDataType = value; }
[Params
(
RecyclableCollectionsBenchmarkSource.Array,
RecyclableCollectionsBenchmarkSource.List,
RecyclableCollectionsBenchmarkSource.PooledList,
RecyclableCollectionsBenchmarkSource.RecyclableList,
RecyclableCollectionsBenchmarkSource.RecyclableLongList
)]
public override RecyclableCollectionsBenchmarkSource DataType { get => base.DataType; set => base.DataType = value; }
protected override Action? GetTestMethod(RecyclableCollectionsBenchmarkSource benchmarkType)
=> TestCase switch
{
RecyclableCollectionsBenchmarkType.AddRangeWhenSourceIsSameType => (Action?)GetType().GetMethod($"{benchmarkType}_AddRangeWhenSourceIs{benchmarkType}", BindingFlags.Instance | BindingFlags.Public)?.CreateDelegate(typeof(Action), this),
_ => (Action?) GetType().GetMethod($"{benchmarkType}_{TestCase}", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)?.CreateDelegate(typeof(Action), this) ?? throw new InvalidOperationException($"Method {benchmarkType}_{TestCase} not found in class {GetType().FullName}")
};
public override void Setup()
{
base.Setup();
switch(TestCase)
{
case RecyclableCollectionsBenchmarkType.AddRangeWhenSourceIsIList:
PrepareData(RecyclableCollectionsBenchmarkSource.RecyclableLongList);
break;
case RecyclableCollectionsBenchmarkType.AddRangeWhenSourceIsSameType:
PrepareData(RecyclableCollectionsBenchmarkSource.RecyclableList);
break;
}
}
}
}
@@ -1,51 +1,51 @@
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void List_RemoveAt_FirstItems()
{
var data = TestObjects;
var list = new List<long>(data);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
list.RemoveAt(0);
}
}
public void PooledList_RemoveAt_FirstItems()
{
var data = TestObjects;
using var list = new PooledList<long>(data, ClearMode.Auto);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
list.RemoveAt(0);
}
}
public void RecyclableList_RemoveAt_FirstItems()
{
var data = TestObjects;
using var list = new RecyclableList<long>(data);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
list.RemoveAt(0);
}
}
public void RecyclableLongList_RemoveAt_FirstItems()
{
var data = TestObjects;
using var list = new RecyclableLongList<long>(data, minBlockSize: BlockSize, initialCapacity: base.TestObjectCount);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
list.RemoveAt(0);
}
}
}
}
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void List_RemoveAt_FirstItems()
{
var data = TestObjects;
var list = new List<long>(data);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
list.RemoveAt(0);
}
}
public void PooledList_RemoveAt_FirstItems()
{
var data = TestObjects;
using var list = new PooledList<long>(data, ClearMode.Auto);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
list.RemoveAt(0);
}
}
public void RecyclableList_RemoveAt_FirstItems()
{
var data = TestObjects;
using var list = new RecyclableList<long>(data);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
list.RemoveAt(0);
}
}
public void RecyclableLongList_RemoveAt_FirstItems()
{
var data = TestObjects;
using var list = new RecyclableLongList<long>(data, minBlockSize: BlockSize, initialCapacity: base.TestObjectCount);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
list.RemoveAt(0);
}
}
}
}
@@ -1,51 +1,51 @@
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void List_RemoveAt_LastItems()
{
var data = TestObjects;
var list = new List<long>(data);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--)
{
list.RemoveAt(i);
}
}
public void PooledList_RemoveAt_LastItems()
{
var data = TestObjects;
using var list = new PooledList<long>(data, ClearMode.Auto);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--)
{
list.RemoveAt(i);
}
}
public void RecyclableList_RemoveAt_LastItems()
{
var data = TestObjects;
using var list = new RecyclableList<long>(data);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--)
{
list.RemoveAt(i);
}
}
public void RecyclableLongList_RemoveAt_LastItems()
{
var data = TestObjects;
using var list = new RecyclableLongList<long>(data, minBlockSize: BlockSize, initialCapacity: base.TestObjectCount);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--)
{
list.RemoveAt(i);
}
}
}
}
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void List_RemoveAt_LastItems()
{
var data = TestObjects;
var list = new List<long>(data);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--)
{
list.RemoveAt(i);
}
}
public void PooledList_RemoveAt_LastItems()
{
var data = TestObjects;
using var list = new PooledList<long>(data, ClearMode.Auto);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--)
{
list.RemoveAt(i);
}
}
public void RecyclableList_RemoveAt_LastItems()
{
var data = TestObjects;
using var list = new RecyclableList<long>(data);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--)
{
list.RemoveAt(i);
}
}
public void RecyclableLongList_RemoveAt_LastItems()
{
var data = TestObjects;
using var list = new RecyclableLongList<long>(data, minBlockSize: BlockSize, initialCapacity: base.TestObjectCount);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--)
{
list.RemoveAt(i);
}
}
}
}
@@ -1,51 +1,51 @@
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void List_Remove_FirstItems()
{
var data = TestObjects;
var list = new List<long>(data);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Remove(data[i]));
}
}
public void PooledList_Remove_FirstItems()
{
var data = TestObjects;
using var list = new PooledList<long>(data, ClearMode.Auto);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Remove(data[i]));
}
}
public void RecyclableList_Remove_FirstItems()
{
var data = TestObjects;
using var list = new RecyclableList<long>(data);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Remove(data[i]));
}
}
public void RecyclableLongList_Remove_FirstItems()
{
var data = TestObjects;
using var list = new RecyclableLongList<long>(data, minBlockSize: BlockSize, initialCapacity: base.TestObjectCount);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Remove(data[i]));
}
}
}
}
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void List_Remove_FirstItems()
{
var data = TestObjects;
var list = new List<long>(data);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Remove(data[i]));
}
}
public void PooledList_Remove_FirstItems()
{
var data = TestObjects;
using var list = new PooledList<long>(data, ClearMode.Auto);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Remove(data[i]));
}
}
public void RecyclableList_Remove_FirstItems()
{
var data = TestObjects;
using var list = new RecyclableList<long>(data);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Remove(data[i]));
}
}
public void RecyclableLongList_Remove_FirstItems()
{
var data = TestObjects;
using var list = new RecyclableLongList<long>(data, minBlockSize: BlockSize, initialCapacity: base.TestObjectCount);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
DoNothing(list.Remove(data[i]));
}
}
}
}
@@ -1,51 +1,51 @@
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void List_Remove_LastItems()
{
var data = TestObjects;
var list = new List<long>(data);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--)
{
DoNothing(list.Remove(data[i]));
}
}
public void PooledList_Remove_LastItems()
{
var data = TestObjects;
using var list = new PooledList<long>(data, ClearMode.Auto);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--)
{
DoNothing(list.Remove(data[i]));
}
}
public void RecyclableList_Remove_LastItems()
{
var data = TestObjects;
using var list = new RecyclableList<long>(data);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--)
{
DoNothing(list.Remove(data[i]));
}
}
public void RecyclableLongList_Remove_LastItems()
{
var data = TestObjects;
using var list = new RecyclableLongList<long>(data, minBlockSize: BlockSize, initialCapacity:base.TestObjectCount);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--)
{
DoNothing(list.Remove(data[i]));
}
}
}
}
using Collections.Pooled;
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void List_Remove_LastItems()
{
var data = TestObjects;
var list = new List<long>(data);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--)
{
DoNothing(list.Remove(data[i]));
}
}
public void PooledList_Remove_LastItems()
{
var data = TestObjects;
using var list = new PooledList<long>(data, ClearMode.Auto);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--)
{
DoNothing(list.Remove(data[i]));
}
}
public void RecyclableList_Remove_LastItems()
{
var data = TestObjects;
using var list = new RecyclableList<long>(data);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--)
{
DoNothing(list.Remove(data[i]));
}
}
public void RecyclableLongList_Remove_LastItems()
{
var data = TestObjects;
using var list = new RecyclableLongList<long>(data, minBlockSize: BlockSize, initialCapacity:base.TestObjectCount);
var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount;
for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--)
{
DoNothing(list.Remove(data[i]));
}
}
}
}
@@ -1,56 +1,56 @@
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_SetItem()
{
var data = TestObjects;
var dataCount = TestObjectCount;
long index = 0;
for (var i = 0; i < dataCount; i++)
{
data[index] = TestObjects[index++];
}
}
public void List_SetItem()
{
var data = TestObjectsAsList;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
data[i] = TestObjects[i];
}
}
public void PooledList_SetItem()
{
var data = TestObjectsAsPooledList;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
data[i] = TestObjects[i];
}
}
public void RecyclableList_SetItem()
{
var data = TestObjectsAsRecyclableList;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
data[i] = TestObjects[i];
}
}
public void RecyclableLongList_SetItem()
{
var data = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
data[i] = TestObjects[i];
}
}
}
}
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
{
public void Array_SetItem()
{
var data = TestObjects;
var dataCount = TestObjectCount;
long index = 0;
for (var i = 0; i < dataCount; i++)
{
data[index] = TestObjects[index++];
}
}
public void List_SetItem()
{
var data = TestObjectsAsList;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
data[i] = TestObjects[i];
}
}
public void PooledList_SetItem()
{
var data = TestObjectsAsPooledList;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
data[i] = TestObjects[i];
}
}
public void RecyclableList_SetItem()
{
var data = TestObjectsAsRecyclableList;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
data[i] = TestObjects[i];
}
}
public void RecyclableLongList_SetItem()
{
var data = TestObjectsAsRecyclableLongList;
var dataCount = TestObjectCount;
for (var i = 0; i < dataCount; i++)
{
data[i] = TestObjects[i];
}
}
}
}
@@ -1,19 +1,19 @@
namespace Recyclable.Collections.Benchmarks.Core
{
public static class DataGenerator
{
public static long[] EnumerateTestObjects(long testObjectCount)
{
var result = new long[testObjectCount];
Console.WriteLine("******* TEST DATA ARRAY CREATED *******");
Span<long> resultSpan = result;
for (int i = 0; i < testObjectCount; i++)
{
resultSpan[i] = i;
}
return result;
}
}
namespace Recyclable.Collections.Benchmarks.Core
{
public static class DataGenerator
{
public static long[] EnumerateTestObjects(long testObjectCount)
{
var result = new long[testObjectCount];
Console.WriteLine("******* TEST DATA ARRAY CREATED *******");
Span<long> resultSpan = result;
for (int i = 0; i < testObjectCount; i++)
{
resultSpan[i] = i;
}
return result;
}
}
}
@@ -1,70 +1,70 @@
using BenchmarkDotNet.Attributes;
using System.Buffers;
namespace Recyclable.Collections.Benchmarks.POC
{
public enum ArraySizeLimitPocBenchmarkType
{
ArraySizeLimit
}
public class ArraySizeLimitPocBenchmarks : PocBenchmarkBase<ArraySizeLimitPocBenchmarkType>
{
public static void Run()
{
var benchmark = new ArraySizeLimitPocBenchmarks();
benchmark.Setup();
FindArraySizeLimit();
benchmark.Cleanup();
}
[Params(2_147_483_605)]
public override int TestObjectCount { get => base.TestObjectCount; set => base.TestObjectCount = value; }
[Benchmark(OperationsPerInvoke = 1)]
public void Max_Allowed_Size_2_147_483_605()
{
long[] arr = ArrayPool<long>.Shared.Rent(TestObjectCount);
DoNothing(arr);
}
[Benchmark(OperationsPerInvoke = 1)]
public static void FindArraySizeLimit()
{
var pool = ArrayPool<long>.Shared;
var bufferSize = int.MaxValue / 2;
var oldValue = 0;
while (true)
{
try
{
Console.Write($"Allocating {bufferSize}...");
var arr = pool.Rent(bufferSize);
Console.WriteLine("success");
pool.Return(arr);
arr = null;
var nextValue = bufferSize - oldValue;
oldValue = bufferSize;
bufferSize = checked(bufferSize + nextValue);
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
}
catch (Exception)
{
Console.WriteLine("error");
var nextValue = (bufferSize - oldValue) >> 1;
if (nextValue == 0)
{
break;
}
bufferSize = oldValue + nextValue;
}
}
Console.WriteLine("All done");
throw new Exception($"Everything was successful. You can create {oldValue} byte big arrays. Exception is logged to prevent more benchmarks.");
}
}
}
using BenchmarkDotNet.Attributes;
using System.Buffers;
namespace Recyclable.Collections.Benchmarks.POC
{
public enum ArraySizeLimitPocBenchmarkType
{
ArraySizeLimit
}
public class ArraySizeLimitPocBenchmarks : PocBenchmarkBase<ArraySizeLimitPocBenchmarkType>
{
public static void Run()
{
var benchmark = new ArraySizeLimitPocBenchmarks();
benchmark.Setup();
FindArraySizeLimit();
benchmark.Cleanup();
}
[Params(2_147_483_605)]
public override int TestObjectCount { get => base.TestObjectCount; set => base.TestObjectCount = value; }
[Benchmark(OperationsPerInvoke = 1)]
public void Max_Allowed_Size_2_147_483_605()
{
long[] arr = ArrayPool<long>.Shared.Rent(TestObjectCount);
DoNothing(arr);
}
[Benchmark(OperationsPerInvoke = 1)]
public static void FindArraySizeLimit()
{
var pool = ArrayPool<long>.Shared;
var bufferSize = int.MaxValue / 2;
var oldValue = 0;
while (true)
{
try
{
Console.Write($"Allocating {bufferSize}...");
var arr = pool.Rent(bufferSize);
Console.WriteLine("success");
pool.Return(arr);
arr = null;
var nextValue = bufferSize - oldValue;
oldValue = bufferSize;
bufferSize = checked(bufferSize + nextValue);
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
}
catch (Exception)
{
Console.WriteLine("error");
var nextValue = (bufferSize - oldValue) >> 1;
if (nextValue == 0)
{
break;
}
bufferSize = oldValue + nextValue;
}
}
Console.WriteLine("All done");
throw new Exception($"Everything was successful. You can create {oldValue} byte big arrays. Exception is logged to prevent more benchmarks.");
}
}
}
@@ -1,63 +1,63 @@
using BenchmarkDotNet.Attributes;
namespace Recyclable.Collections.Benchmarks.POC
{
public enum BoolOrComparePocBenchmarkType
{
Bool,
Compare,
}
[MemoryDiagnoser]
public class BoolOrComparePocBenchmarks : PocBenchmarkBase<BoolOrComparePocBenchmarkType>
{
[Params(BoolOrComparePocBenchmarkType.Bool, BoolOrComparePocBenchmarkType.Compare)]
public override BoolOrComparePocBenchmarkType DataType { get => base.DataType; set => base.DataType = value; }
[Params(8)]
public override int TestObjectCount { get => base.TestObjectCount; set => base.TestObjectCount = value; }
[Params(true)]
public bool TestBool = true;
public static void Run()
{
var benchmark = new BoolOrComparePocBenchmarks();
benchmark.Setup();
benchmark.Bool();
benchmark.Compare();
benchmark.Cleanup();
}
public void Bool()
{
if (TestBool)
{
DoNothing(TestBool);
}
else
{
DoNothing(TestBool);
}
}
public void Compare()
{
if (TestObjectCount >= RecyclableDefaults.MinPooledArrayLength)
{
DoNothing(TestBool);
}
else
{
DoNothing(TestBool);
}
}
protected override Action? GetTestMethod(BoolOrComparePocBenchmarkType benchmarkType) => benchmarkType switch
{
BoolOrComparePocBenchmarkType.Bool => Bool,
BoolOrComparePocBenchmarkType.Compare => Compare,
_ => throw CreateUnknownBenchmarkTypeException(benchmarkType),
};
}
using BenchmarkDotNet.Attributes;
namespace Recyclable.Collections.Benchmarks.POC
{
public enum BoolOrComparePocBenchmarkType
{
Bool,
Compare,
}
[MemoryDiagnoser]
public class BoolOrComparePocBenchmarks : PocBenchmarkBase<BoolOrComparePocBenchmarkType>
{
[Params(BoolOrComparePocBenchmarkType.Bool, BoolOrComparePocBenchmarkType.Compare)]
public override BoolOrComparePocBenchmarkType DataType { get => base.DataType; set => base.DataType = value; }
[Params(8)]
public override int TestObjectCount { get => base.TestObjectCount; set => base.TestObjectCount = value; }
[Params(true)]
public bool TestBool = true;
public static void Run()
{
var benchmark = new BoolOrComparePocBenchmarks();
benchmark.Setup();
benchmark.Bool();
benchmark.Compare();
benchmark.Cleanup();
}
public void Bool()
{
if (TestBool)
{
DoNothing(TestBool);
}
else
{
DoNothing(TestBool);
}
}
public void Compare()
{
if (TestObjectCount >= RecyclableDefaults.MinPooledArrayLength)
{
DoNothing(TestBool);
}
else
{
DoNothing(TestBool);
}
}
protected override Action? GetTestMethod(BoolOrComparePocBenchmarkType benchmarkType) => benchmarkType switch
{
BoolOrComparePocBenchmarkType.Bool => Bool,
BoolOrComparePocBenchmarkType.Compare => Compare,
_ => throw CreateUnknownBenchmarkTypeException(benchmarkType),
};
}
}
@@ -1,346 +1,346 @@
using BenchmarkDotNet.Attributes;
namespace Recyclable.Collections.Benchmarks.POC
{
public enum DelegateVsComparerPocBenchmarkType
{
Comparer,
Delegate,
Func,
ListOfBoxedStructs,
ListOfClasses,
ListOfComparers,
ListOfDelegates,
ListOfFuncs,
ListOfLocalFuncs,
ListOfStructs,
ListOfUnboxedReadOnlyStructs,
ListOfUnboxedStructs,
StaticFunc,
}
[MemoryDiagnoser]
public class DelegateVsComparerPocBenchmarks : PocBenchmarkBase<DelegateVsComparerPocBenchmarkType>
{
[Params
(
DelegateVsComparerPocBenchmarkType.Delegate,
DelegateVsComparerPocBenchmarkType.Func,
DelegateVsComparerPocBenchmarkType.ListOfBoxedStructs,
DelegateVsComparerPocBenchmarkType.ListOfClasses,
DelegateVsComparerPocBenchmarkType.ListOfComparers,
DelegateVsComparerPocBenchmarkType.ListOfDelegates,
DelegateVsComparerPocBenchmarkType.ListOfFuncs,
DelegateVsComparerPocBenchmarkType.ListOfLocalFuncs,
DelegateVsComparerPocBenchmarkType.ListOfStructs,
DelegateVsComparerPocBenchmarkType.ListOfUnboxedReadOnlyStructs,
DelegateVsComparerPocBenchmarkType.ListOfUnboxedStructs,
DelegateVsComparerPocBenchmarkType.StaticFunc
)]
public override DelegateVsComparerPocBenchmarkType DataType { get => base.DataType; set => base.DataType = value; }
[Params(DelegateVsComparerPocBenchmarkType.Comparer)]
public override DelegateVsComparerPocBenchmarkType BaseDataType { get => base.BaseDataType; set => base.BaseDataType = value; }
#region Dummy classes
private delegate T Compare<T>(T x, T y);
public readonly struct MyComparerReadOnlyStruct<T> : IComparer<T>
{
public int Compare(T? x, T? y) => Comparer<T>.Default.Compare(x, y);
}
private struct MyComparerStruct<T> : IComparer<T>
{
public readonly int Compare(T? x, T? y) => Comparer<T>.Default.Compare(x, y);
}
private class MyComparer : IComparer<long>
{
public int Compare(long x, long y) => (x - y) switch
{
> 0 => 1,
< 0 => -1,
_ => 0,
};
}
#endregion
public static void Run()
{
var benchmark = new DelegateVsComparerPocBenchmarks();
benchmark.Setup();
benchmark.Comparer();
benchmark.Delegate();
benchmark.Func();
benchmark.ListOfBoxedStructs();
benchmark.ListOfClasses();
benchmark.ListOfComparers();
benchmark.ListOfDelegates();
benchmark.ListOfFuncs();
benchmark.ListOfLocalFuncs();
benchmark.ListOfStructs();
benchmark.ListOfUnboxedReadOnlyStructs();
benchmark.ListOfUnboxedStructs();
benchmark.StaticFunc();
benchmark.Cleanup();
}
private static readonly Comparer<long> _comparer = Comparer<long>.Default;
private IEnumerable<long>? _testObjects;
private IEnumerable<Func<long, long, int>>? _testListOfFunctions;
private IEnumerable<IComparer<long>>? _testListOfComparers;
private IEnumerable<Compare<long>>? _testListOfDelegates;
private IEnumerable<Func<long, long, int>>? _testListOfLocalFunctions;
private IEnumerable<MyComparer>? _testListOfClasses;
private IEnumerable<MyComparerStruct<long>>? _testListOfStructs;
private IEnumerable<IComparer<long>>? _testListOfBoxedStructs;
private IEnumerable<TComparer> TestListOfUnboxedStructs<TComparer, TValue>() where TComparer : IComparer<TValue>, new()
{
foreach (var _ in _testObjects!)
{
yield return new TComparer();
}
}
private static IEnumerable<Func<long, long, int>> GetLocalFunctions(IEnumerable<long> testObjects)
{
static int compareFunc(long x, long y)
{
return (x - y) switch
{
> 0 => 1,
< 0 => -1,
_ => 0
};
}
foreach (var _ in testObjects)
{
yield return compareFunc;
}
}
private void TestWithComparer(IComparer<long> comparer)
{
var testData = _testObjects!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length - 1; itemIdx++)
{
_ = comparer.Compare(testData[itemIdx], testData[itemIdx + 1]);
}
}
private void TestWithDelegate(Compare<long> compareFunc)
{
var testData = _testObjects!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length - 1; itemIdx++)
{
_ = compareFunc(testData[itemIdx], testData[itemIdx + 1]);
}
}
private void TestWithFunc(Func<long, long, int> compareFunc)
{
var testData = _testObjects!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length - 1; itemIdx++)
{
_ = compareFunc(testData[itemIdx], testData[itemIdx + 1]);
}
}
public void Comparer()
{
TestWithComparer(_comparer);
}
public void Func()
{
TestWithFunc((x, y) => (x - y) switch
{
> 0 => 1,
< 0 => -1,
_ => 0
});
}
public void StaticFunc()
{
TestWithFunc(static (x, y) => (x - y) switch
{
> 0 => 1,
< 0 => -1,
_ => 0
});
}
public void Delegate()
{
TestWithDelegate((x, y) => (x - y) switch
{
> 0 => 1,
< 0 => -1,
_ => 0
});
}
public void ListOfFuncs()
{
const long x = 2, y = 5;
var testData = _testListOfFunctions!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var compareFunc = testData[itemIdx];
_ = compareFunc(x, y);
}
}
public void ListOfLocalFuncs()
{
const long x = 2, y = 5;
var testData = _testListOfLocalFunctions!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var compareFunc = testData[itemIdx];
_ = compareFunc(x, y);
}
}
public void ListOfComparers()
{
const long x = 2, y = 5;
var testData = _testListOfComparers!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var comparer = testData[itemIdx];
_ = comparer.Compare(x, y);
}
}
public void ListOfClasses()
{
const long x = 2, y = 5;
var testData = _testListOfClasses!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var comparer = testData[itemIdx];
_ = comparer.Compare(x, y);
}
}
public void ListOfStructs()
{
const long x = 2, y = 5;
var testData = _testListOfStructs!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var comparer = testData[itemIdx];
_ = comparer.Compare(x, y);
}
}
public void ListOfBoxedStructs()
{
const long x = 2, y = 5;
var testData = _testListOfBoxedStructs!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var comparer = testData[itemIdx];
_ = comparer.Compare(x, y);
}
}
public void ListOfUnboxedStructs()
{
const long x = 2, y = 5;
var testData = TestListOfUnboxedStructs<MyComparerStruct<long>, long>().ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var comparer = testData[itemIdx];
_ = comparer.Compare(x, y);
}
}
public void ListOfUnboxedReadOnlyStructs()
{
const long x = 2, y = 5;
var testData = TestListOfUnboxedStructs<MyComparerReadOnlyStruct<long>, long>().ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var comparer = testData[itemIdx];
_ = comparer.Compare(x, y);
}
}
public void ListOfDelegates()
{
const long x = 2, y = 5;
var testData = _testListOfDelegates!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var compareDelegate = testData[itemIdx];
_ = compareDelegate(x, y);
}
}
protected override Action? GetTestMethod(DelegateVsComparerPocBenchmarkType benchmarkType) => benchmarkType switch
{
DelegateVsComparerPocBenchmarkType.Comparer => Comparer,
DelegateVsComparerPocBenchmarkType.Delegate => Delegate,
DelegateVsComparerPocBenchmarkType.Func => Func,
DelegateVsComparerPocBenchmarkType.ListOfBoxedStructs => ListOfBoxedStructs,
DelegateVsComparerPocBenchmarkType.ListOfClasses => ListOfClasses,
DelegateVsComparerPocBenchmarkType.ListOfComparers => ListOfComparers,
DelegateVsComparerPocBenchmarkType.ListOfDelegates => ListOfDelegates,
DelegateVsComparerPocBenchmarkType.ListOfFuncs => ListOfFuncs,
DelegateVsComparerPocBenchmarkType.ListOfLocalFuncs => ListOfLocalFuncs,
DelegateVsComparerPocBenchmarkType.ListOfStructs => ListOfStructs,
DelegateVsComparerPocBenchmarkType.ListOfUnboxedReadOnlyStructs => ListOfUnboxedReadOnlyStructs,
DelegateVsComparerPocBenchmarkType.ListOfUnboxedStructs => ListOfUnboxedStructs,
DelegateVsComparerPocBenchmarkType.StaticFunc => StaticFunc,
_ => throw CreateUnknownBenchmarkTypeException(benchmarkType),
};
public override void Setup()
{
Console.WriteLine("******* SETTING UP COMMON TEST DATA *******");
_testObjects = Enumerable.Range(1, TestObjectCount).Select(x => (long)x);
base.Setup();
}
public override void Cleanup()
{
_testObjects = null;
base.Cleanup();
}
protected override void PrepareData<T>(T benchmarkType)
{
switch (benchmarkType)
{
case DelegateVsComparerPocBenchmarkType.ListOfBoxedStructs:
_testListOfBoxedStructs = _testObjects!.Select(_ => (IComparer<long>)new MyComparerStruct<long>());
break;
case DelegateVsComparerPocBenchmarkType.ListOfClasses:
_testListOfClasses = _testObjects!.Select(_ => new MyComparer());
break;
case DelegateVsComparerPocBenchmarkType.ListOfComparers:
_testListOfComparers = _testObjects!.Select(_ => (IComparer<long>)new MyComparer());
break;
case DelegateVsComparerPocBenchmarkType.ListOfDelegates:
_testListOfDelegates = _testObjects!.Select(_ => new Compare<long>((x, y) => new MyComparer().Compare(x, y)));
break;
case DelegateVsComparerPocBenchmarkType.ListOfFuncs:
_testListOfFunctions = _testObjects!.Select<long, Func<long, long, int>>(static _ => new MyComparer().Compare);
break;
case DelegateVsComparerPocBenchmarkType.ListOfLocalFuncs:
_testListOfLocalFunctions = GetLocalFunctions(_testObjects!);
break;
case DelegateVsComparerPocBenchmarkType.ListOfStructs:
_testListOfStructs = _testObjects!.Select(_ => new MyComparerStruct<long>());
break;
}
}
}
}
using BenchmarkDotNet.Attributes;
namespace Recyclable.Collections.Benchmarks.POC
{
public enum DelegateVsComparerPocBenchmarkType
{
Comparer,
Delegate,
Func,
ListOfBoxedStructs,
ListOfClasses,
ListOfComparers,
ListOfDelegates,
ListOfFuncs,
ListOfLocalFuncs,
ListOfStructs,
ListOfUnboxedReadOnlyStructs,
ListOfUnboxedStructs,
StaticFunc,
}
[MemoryDiagnoser]
public class DelegateVsComparerPocBenchmarks : PocBenchmarkBase<DelegateVsComparerPocBenchmarkType>
{
[Params
(
DelegateVsComparerPocBenchmarkType.Delegate,
DelegateVsComparerPocBenchmarkType.Func,
DelegateVsComparerPocBenchmarkType.ListOfBoxedStructs,
DelegateVsComparerPocBenchmarkType.ListOfClasses,
DelegateVsComparerPocBenchmarkType.ListOfComparers,
DelegateVsComparerPocBenchmarkType.ListOfDelegates,
DelegateVsComparerPocBenchmarkType.ListOfFuncs,
DelegateVsComparerPocBenchmarkType.ListOfLocalFuncs,
DelegateVsComparerPocBenchmarkType.ListOfStructs,
DelegateVsComparerPocBenchmarkType.ListOfUnboxedReadOnlyStructs,
DelegateVsComparerPocBenchmarkType.ListOfUnboxedStructs,
DelegateVsComparerPocBenchmarkType.StaticFunc
)]
public override DelegateVsComparerPocBenchmarkType DataType { get => base.DataType; set => base.DataType = value; }
[Params(DelegateVsComparerPocBenchmarkType.Comparer)]
public override DelegateVsComparerPocBenchmarkType BaseDataType { get => base.BaseDataType; set => base.BaseDataType = value; }
#region Dummy classes
private delegate T Compare<T>(T x, T y);
public readonly struct MyComparerReadOnlyStruct<T> : IComparer<T>
{
public int Compare(T? x, T? y) => Comparer<T>.Default.Compare(x, y);
}
private struct MyComparerStruct<T> : IComparer<T>
{
public readonly int Compare(T? x, T? y) => Comparer<T>.Default.Compare(x, y);
}
private class MyComparer : IComparer<long>
{
public int Compare(long x, long y) => (x - y) switch
{
> 0 => 1,
< 0 => -1,
_ => 0,
};
}
#endregion
public static void Run()
{
var benchmark = new DelegateVsComparerPocBenchmarks();
benchmark.Setup();
benchmark.Comparer();
benchmark.Delegate();
benchmark.Func();
benchmark.ListOfBoxedStructs();
benchmark.ListOfClasses();
benchmark.ListOfComparers();
benchmark.ListOfDelegates();
benchmark.ListOfFuncs();
benchmark.ListOfLocalFuncs();
benchmark.ListOfStructs();
benchmark.ListOfUnboxedReadOnlyStructs();
benchmark.ListOfUnboxedStructs();
benchmark.StaticFunc();
benchmark.Cleanup();
}
private static readonly Comparer<long> _comparer = Comparer<long>.Default;
private IEnumerable<long>? _testObjects;
private IEnumerable<Func<long, long, int>>? _testListOfFunctions;
private IEnumerable<IComparer<long>>? _testListOfComparers;
private IEnumerable<Compare<long>>? _testListOfDelegates;
private IEnumerable<Func<long, long, int>>? _testListOfLocalFunctions;
private IEnumerable<MyComparer>? _testListOfClasses;
private IEnumerable<MyComparerStruct<long>>? _testListOfStructs;
private IEnumerable<IComparer<long>>? _testListOfBoxedStructs;
private IEnumerable<TComparer> TestListOfUnboxedStructs<TComparer, TValue>() where TComparer : IComparer<TValue>, new()
{
foreach (var _ in _testObjects!)
{
yield return new TComparer();
}
}
private static IEnumerable<Func<long, long, int>> GetLocalFunctions(IEnumerable<long> testObjects)
{
static int compareFunc(long x, long y)
{
return (x - y) switch
{
> 0 => 1,
< 0 => -1,
_ => 0
};
}
foreach (var _ in testObjects)
{
yield return compareFunc;
}
}
private void TestWithComparer(IComparer<long> comparer)
{
var testData = _testObjects!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length - 1; itemIdx++)
{
_ = comparer.Compare(testData[itemIdx], testData[itemIdx + 1]);
}
}
private void TestWithDelegate(Compare<long> compareFunc)
{
var testData = _testObjects!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length - 1; itemIdx++)
{
_ = compareFunc(testData[itemIdx], testData[itemIdx + 1]);
}
}
private void TestWithFunc(Func<long, long, int> compareFunc)
{
var testData = _testObjects!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length - 1; itemIdx++)
{
_ = compareFunc(testData[itemIdx], testData[itemIdx + 1]);
}
}
public void Comparer()
{
TestWithComparer(_comparer);
}
public void Func()
{
TestWithFunc((x, y) => (x - y) switch
{
> 0 => 1,
< 0 => -1,
_ => 0
});
}
public void StaticFunc()
{
TestWithFunc(static (x, y) => (x - y) switch
{
> 0 => 1,
< 0 => -1,
_ => 0
});
}
public void Delegate()
{
TestWithDelegate((x, y) => (x - y) switch
{
> 0 => 1,
< 0 => -1,
_ => 0
});
}
public void ListOfFuncs()
{
const long x = 2, y = 5;
var testData = _testListOfFunctions!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var compareFunc = testData[itemIdx];
_ = compareFunc(x, y);
}
}
public void ListOfLocalFuncs()
{
const long x = 2, y = 5;
var testData = _testListOfLocalFunctions!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var compareFunc = testData[itemIdx];
_ = compareFunc(x, y);
}
}
public void ListOfComparers()
{
const long x = 2, y = 5;
var testData = _testListOfComparers!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var comparer = testData[itemIdx];
_ = comparer.Compare(x, y);
}
}
public void ListOfClasses()
{
const long x = 2, y = 5;
var testData = _testListOfClasses!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var comparer = testData[itemIdx];
_ = comparer.Compare(x, y);
}
}
public void ListOfStructs()
{
const long x = 2, y = 5;
var testData = _testListOfStructs!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var comparer = testData[itemIdx];
_ = comparer.Compare(x, y);
}
}
public void ListOfBoxedStructs()
{
const long x = 2, y = 5;
var testData = _testListOfBoxedStructs!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var comparer = testData[itemIdx];
_ = comparer.Compare(x, y);
}
}
public void ListOfUnboxedStructs()
{
const long x = 2, y = 5;
var testData = TestListOfUnboxedStructs<MyComparerStruct<long>, long>().ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var comparer = testData[itemIdx];
_ = comparer.Compare(x, y);
}
}
public void ListOfUnboxedReadOnlyStructs()
{
const long x = 2, y = 5;
var testData = TestListOfUnboxedStructs<MyComparerReadOnlyStruct<long>, long>().ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var comparer = testData[itemIdx];
_ = comparer.Compare(x, y);
}
}
public void ListOfDelegates()
{
const long x = 2, y = 5;
var testData = _testListOfDelegates!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
{
var compareDelegate = testData[itemIdx];
_ = compareDelegate(x, y);
}
}
protected override Action? GetTestMethod(DelegateVsComparerPocBenchmarkType benchmarkType) => benchmarkType switch
{
DelegateVsComparerPocBenchmarkType.Comparer => Comparer,
DelegateVsComparerPocBenchmarkType.Delegate => Delegate,
DelegateVsComparerPocBenchmarkType.Func => Func,
DelegateVsComparerPocBenchmarkType.ListOfBoxedStructs => ListOfBoxedStructs,
DelegateVsComparerPocBenchmarkType.ListOfClasses => ListOfClasses,
DelegateVsComparerPocBenchmarkType.ListOfComparers => ListOfComparers,
DelegateVsComparerPocBenchmarkType.ListOfDelegates => ListOfDelegates,
DelegateVsComparerPocBenchmarkType.ListOfFuncs => ListOfFuncs,
DelegateVsComparerPocBenchmarkType.ListOfLocalFuncs => ListOfLocalFuncs,
DelegateVsComparerPocBenchmarkType.ListOfStructs => ListOfStructs,
DelegateVsComparerPocBenchmarkType.ListOfUnboxedReadOnlyStructs => ListOfUnboxedReadOnlyStructs,
DelegateVsComparerPocBenchmarkType.ListOfUnboxedStructs => ListOfUnboxedStructs,
DelegateVsComparerPocBenchmarkType.StaticFunc => StaticFunc,
_ => throw CreateUnknownBenchmarkTypeException(benchmarkType),
};
public override void Setup()
{
Console.WriteLine("******* SETTING UP COMMON TEST DATA *******");
_testObjects = Enumerable.Range(1, TestObjectCount).Select(x => (long)x);
base.Setup();
}
public override void Cleanup()
{
_testObjects = null;
base.Cleanup();
}
protected override void PrepareData<T>(T benchmarkType)
{
switch (benchmarkType)
{
case DelegateVsComparerPocBenchmarkType.ListOfBoxedStructs:
_testListOfBoxedStructs = _testObjects!.Select(_ => (IComparer<long>)new MyComparerStruct<long>());
break;
case DelegateVsComparerPocBenchmarkType.ListOfClasses:
_testListOfClasses = _testObjects!.Select(_ => new MyComparer());
break;
case DelegateVsComparerPocBenchmarkType.ListOfComparers:
_testListOfComparers = _testObjects!.Select(_ => (IComparer<long>)new MyComparer());
break;
case DelegateVsComparerPocBenchmarkType.ListOfDelegates:
_testListOfDelegates = _testObjects!.Select(_ => new Compare<long>((x, y) => new MyComparer().Compare(x, y)));
break;
case DelegateVsComparerPocBenchmarkType.ListOfFuncs:
_testListOfFunctions = _testObjects!.Select<long, Func<long, long, int>>(static _ => new MyComparer().Compare);
break;
case DelegateVsComparerPocBenchmarkType.ListOfLocalFuncs:
_testListOfLocalFunctions = GetLocalFunctions(_testObjects!);
break;
case DelegateVsComparerPocBenchmarkType.ListOfStructs:
_testListOfStructs = _testObjects!.Select(_ => new MyComparerStruct<long>());
break;
}
}
}
}
@@ -1,40 +1,40 @@
using BenchmarkDotNet.Attributes;
using FluentAssertions;
using Recyclable.CollectionsTests;
namespace Recyclable.Collections.Benchmarks.POC
{
public class EqualVsContainsInConsecutiveOrderBenchmarks : IDisposable
{
private const int TestItemsCount = 10000;
private readonly IEnumerable<long> _list1 = RecyclableLongListTestData.CreateTestData(TestItemsCount).ToRecyclableList();
private readonly IEnumerable<long> _list2 = RecyclableLongListTestData.CreateTestData(TestItemsCount).ToArray();
[Benchmark(Baseline = true)]
public void Equal()
{
_ = _list1.Should().Equal(_list2);
}
[Benchmark]
public void ContainsInConsecutiveOrder()
{
_ = _list1.Should().ContainInConsecutiveOrder(_list2);
}
public void Dispose()
{
if (_list1 is IDisposable disposable1)
{
disposable1.Dispose();
}
if (_list2 is IDisposable disposable2)
{
disposable2.Dispose();
}
}
}
}
using BenchmarkDotNet.Attributes;
using FluentAssertions;
using Recyclable.CollectionsTests;
namespace Recyclable.Collections.Benchmarks.POC
{
public class EqualVsContainsInConsecutiveOrderBenchmarks : IDisposable
{
private const int TestItemsCount = 10000;
private readonly IEnumerable<long> _list1 = RecyclableLongListTestData.CreateTestData(TestItemsCount).ToRecyclableList();
private readonly IEnumerable<long> _list2 = RecyclableLongListTestData.CreateTestData(TestItemsCount).ToArray();
[Benchmark(Baseline = true)]
public void Equal()
{
_ = _list1.Should().Equal(_list2);
}
[Benchmark]
public void ContainsInConsecutiveOrder()
{
_ = _list1.Should().ContainInConsecutiveOrder(_list2);
}
public void Dispose()
{
if (_list1 is IDisposable disposable1)
{
disposable1.Dispose();
}
if (_list2 is IDisposable disposable2)
{
disposable2.Dispose();
}
}
}
}
@@ -1,57 +1,57 @@
using BenchmarkDotNet.Attributes;
using System.Runtime.CompilerServices;
#pragma warning disable CA1822
namespace Recyclable.Collections.Benchmarks.POC
{
public class GreaterOperatorVsDoesntEqualOperatorBenchmarks
{
private static readonly long num = RecyclableDefaults.MinItemsCountForParallelization;
public void Setup()
{
Console.WriteLine($"*** We're comparing if {num} is < 0 ***");
}
public void Cleanup() { }
#pragma warning disable RCS1163, IDE0060
[MethodImpl(MethodImplOptions.NoInlining)]
private static void DoNothing<T>(T item)
{
}
#pragma warning restore RCS1163
[Benchmark(Baseline = true)]
public void CompareWithGreater()
{
DoNothing(num > 0 ? 1 : 0);
}
[Benchmark]
public void CompareWithGreaterEqual()
{
DoNothing(num >= 0 ? 1 : 0);
}
[Benchmark]
public void CompareWithDoesntEqual()
{
DoNothing(num != 0 ? 1 : 0);
}
[Benchmark]
public void CompareWithLess()
{
DoNothing(num < 0 ? 1 : 0);
}
[Benchmark]
public void CompareWithEqual()
{
DoNothing(num == 0 ? 0 : 1);
}
}
}
using BenchmarkDotNet.Attributes;
using System.Runtime.CompilerServices;
#pragma warning disable CA1822
namespace Recyclable.Collections.Benchmarks.POC
{
public class GreaterOperatorVsDoesntEqualOperatorBenchmarks
{
private static readonly long num = RecyclableDefaults.MinItemsCountForParallelization;
public void Setup()
{
Console.WriteLine($"*** We're comparing if {num} is < 0 ***");
}
public void Cleanup() { }
#pragma warning disable RCS1163, IDE0060
[MethodImpl(MethodImplOptions.NoInlining)]
private static void DoNothing<T>(T item)
{
}
#pragma warning restore RCS1163
[Benchmark(Baseline = true)]
public void CompareWithGreater()
{
DoNothing(num > 0 ? 1 : 0);
}
[Benchmark]
public void CompareWithGreaterEqual()
{
DoNothing(num >= 0 ? 1 : 0);
}
[Benchmark]
public void CompareWithDoesntEqual()
{
DoNothing(num != 0 ? 1 : 0);
}
[Benchmark]
public void CompareWithLess()
{
DoNothing(num < 0 ? 1 : 0);
}
[Benchmark]
public void CompareWithEqual()
{
DoNothing(num == 0 ? 0 : 1);
}
}
}
@@ -1,54 +1,54 @@
using BenchmarkDotNet.Attributes;
using System.Runtime.CompilerServices;
#pragma warning disable CA1822
namespace Recyclable.Collections.Benchmarks.POC
{
public class LessOperatorVsAndOperatorBenchmarks
{
private const int bitShift = 1 << 31;
private static readonly Random _rnd = new(1);
private static int num = -Math.Abs(_rnd.Next());
public void Setup()
{
num = -1;
Console.WriteLine($"*** We're comparing if {num} is < 0 ***");
}
public void Cleanup() { }
#pragma warning disable RCS1163, IDE0060
[MethodImpl(MethodImplOptions.NoInlining)]
private static void DoNothing<T>(T item)
{
}
#pragma warning restore RCS1163
[Benchmark(Baseline = true)]
public void CompareWithLess()
{
DoNothing(num < 0);
}
[Benchmark]
public void CompareWithAnd()
{
DoNothing((num & bitShift) != 0);
}
[Benchmark]
public void CompareWithEqual()
{
DoNothing(num == -1);
}
[Benchmark]
public void CompareToZero()
{
DoNothing(num == 0);
}
}
}
using BenchmarkDotNet.Attributes;
using System.Runtime.CompilerServices;
#pragma warning disable CA1822
namespace Recyclable.Collections.Benchmarks.POC
{
public class LessOperatorVsAndOperatorBenchmarks
{
private const int bitShift = 1 << 31;
private static readonly Random _rnd = new(1);
private static int num = -Math.Abs(_rnd.Next());
public void Setup()
{
num = -1;
Console.WriteLine($"*** We're comparing if {num} is < 0 ***");
}
public void Cleanup() { }
#pragma warning disable RCS1163, IDE0060
[MethodImpl(MethodImplOptions.NoInlining)]
private static void DoNothing<T>(T item)
{
}
#pragma warning restore RCS1163
[Benchmark(Baseline = true)]
public void CompareWithLess()
{
DoNothing(num < 0);
}
[Benchmark]
public void CompareWithAnd()
{
DoNothing((num & bitShift) != 0);
}
[Benchmark]
public void CompareWithEqual()
{
DoNothing(num == -1);
}
[Benchmark]
public void CompareToZero()
{
DoNothing(num == 0);
}
}
}
@@ -1,376 +1,376 @@
using BenchmarkDotNet.Attributes;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
#pragma warning disable xUnit1026, RCS1163, IDE0060
namespace Recyclable.Collections.Benchmarks.POC
{
[MemoryDiagnoser]
public class ModuloPocBenchmarks
{
public static void Run()
{
var benchmark = new ModuloPocBenchmarks();
benchmark.Setup();
benchmark.AddWithFor();
benchmark.AddWithForOptimized();
benchmark.DividerAndModulusWithOperator();
benchmark.MathDivRem();
benchmark.MixedApproachWithForLoopOptimized();
benchmark.MixedApproachWithSubtractWithStackalloc();
benchmark.MixedApproachWithSubtractWithWhile();
benchmark.MixedApproachWithSubtractWithWhileAsVector();
benchmark.MixedApproachWithSubtractWithWhileWithSse2();
benchmark.SubtractWithWhile();
benchmark.SubtractWithWhileWithSse2();
benchmark.XDivYMulX();
benchmark.XDivYMulXWithLocalVar();
}
private long _cutoff;
private Vector128<long> _increment;
private long _lastCaseIndex;
[GlobalSetup]
public void Setup()
{
_cutoff = checked((long)BlockSize << 4);
_increment = Vector128.Create(-BlockSize, 1L);
_lastCaseIndex = 0;
}
private static void DoNothing<T>(T result, T remainder, [CallerMemberName] string? callerName = null)
{
//Console.WriteLine($"{callerName}: result = {result}, remainder = {remainder}");
}
//[Params(123, 10, 2, int.MaxValue, int.MaxValue / 2, int.MaxValue / 3, int.MaxValue / 4, int.MaxValue / 6, int.MaxValue / 10, int.MaxValue / 15, int.MaxValue / 20)]
[Params(int.MaxValue, int.MaxValue / 2, int.MaxValue / 3, int.MaxValue / 4, int.MaxValue / 6, int.MaxValue / 8, int.MaxValue / 10, int.MaxValue / 100)]
//[Params(int.MaxValue / 10, int.MaxValue / 20, int.MaxValue / 100)]
public int BlockSize { get; set; } = int.MaxValue;
[Params(int.MaxValue, int.MaxValue / 1000, int.MaxValue / 10000)]
//[Params(int.MaxValue)]
//[Params(int.MaxValue * 100L)]
public long TestNumber { get; set; } = int.MaxValue;
private void CalculateDividerAndModulusWithOperator(long i)
{
DoNothing(i / BlockSize, i % BlockSize);
}
private void CalculateWithMathDivRem(long i)
{
DoNothing(Math.DivRem(i, BlockSize, out long remainder), remainder);
}
private void CalculateWithAddWithFor(long i)
{
int result = 0;
int remainder = 0;
int blockSize = BlockSize;
for (long iteration = 0; iteration < i; iteration++)
{
if (remainder > blockSize - 1)
{
remainder = 0;
result++;
}
remainder++;
}
DoNothing(result, remainder);
}
private void CalculateWithAddWithForOptimized(long i)
{
long total = 0;
int result = 0;
int blockSize = BlockSize;
while (total + blockSize <= i)
{
total += blockSize;
result++;
}
DoNothing(result, i - total);
}
private void CalculateWithXDivYMulX(long i)
{
DoNothing(i / BlockSize, i - (i / BlockSize * BlockSize));
}
private void CalculateWithXDivYMulXWithLocalVar(long i)
{
int blockSize = BlockSize;
int result = (int)(i / blockSize);
DoNothing(result, i - (result * blockSize));
}
private void CalculateWithSubtractWithWhile(long i)
{
int result = 0;
long remainder = i;
int blockSize = BlockSize;
while (remainder >= blockSize)
{
remainder -= blockSize;
result++;
}
DoNothing(result, remainder);
}
private void CalculateWithSubtractWithWhileWithSse2(long i)
{
var state = Vector128.Create(i, 0);
int blockSize = BlockSize;
var inc = _increment;
while (state.GetElement(0) >= blockSize)
{
state = Sse2.Add(state, inc);
}
DoNothing(state.GetElement(1), state.GetElement(0));
}
private void CalculateWithMixedApproachWithSubtractWithWhile(long i)
{
long remainder;
if (i >= _cutoff)
{
DoNothing(Math.DivRem(i, BlockSize, out remainder), remainder);
}
else
{
int result = 0;
int blockSize = BlockSize;
remainder = i;
while (remainder >= blockSize)
{
remainder -= blockSize;
result++;
}
DoNothing(result, remainder);
}
}
private void CalculateWithMixedApproachWithSubtractWithWhileAsVector(long i)
{
if (i >= _cutoff)
{
DoNothing(Math.DivRem(i, BlockSize, out long remainder), remainder);
}
else
{
var state = new Vector<long>(new[] { 0L, i, 0, 0 });
var increment = new Vector<long>(new[] { 1L, -BlockSize, 0, 0 });
int blockSize = BlockSize;
while (state[1] >= blockSize)
{
state += increment;
}
DoNothing(state[0], state[1]);
}
}
private void CalculateWithMixedApproachWithSubtractWithWhileWithSse2(long i)
{
if (i >= _cutoff)
{
DoNothing(Math.DivRem(i, BlockSize, out long remainder), remainder);
}
else
{
var increment = _increment;
var state = Vector128.Create(i, 0);
int blockSize = BlockSize;
while (state.GetElement(0) >= blockSize)
{
state = Sse2.Add(state, increment);
}
DoNothing(state.GetElement(1), state.GetElement(0));
}
}
private void CalculateWithMixedApproachWithSubtractWithStackalloc(long i)
{
if (i >= _cutoff)
{
DoNothing(Math.DivRem(i, BlockSize, out long remainder), remainder);
}
else
{
Span<long> state = stackalloc long[2] { i, 0 };// Vector128.Create(BlockSize, 0);
int blockSize = BlockSize;
while (state[0] >= blockSize)
{
state[0] -= blockSize;
state[1]++;
}
DoNothing(state[1], state[0]);
}
}
private void CalculateWithMixedApproachWithForLoopOptimized(long i)
{
long total;
if (i >= _cutoff)
{
DoNothing(Math.DivRem(i, BlockSize, out total), total);
}
else
{
total = 0;
int result = 0;
int blockSize = BlockSize;
while (total + blockSize <= i)
{
total += blockSize;
result++;
}
DoNothing(result, i - total);
}
}
[Benchmark(Baseline = true)]
public void DividerAndModulusWithOperator()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateDividerAndModulusWithOperator(i);
}
}
[Benchmark]
public void MathDivRem()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithMathDivRem(i);
}
}
[Benchmark]
public void AddWithFor()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithAddWithFor(i);
}
}
[Benchmark(Baseline = true)]
public void AddWithForOptimized()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithAddWithForOptimized(i);
}
}
[Benchmark]
public void XDivYMulX()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithXDivYMulX(i);
}
}
[Benchmark]
public void XDivYMulXWithLocalVar()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithXDivYMulXWithLocalVar(i);
}
}
[Benchmark]
public void SubtractWithWhile()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithSubtractWithWhile(i);
}
}
[Benchmark]
public void SubtractWithWhileWithSse2()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithSubtractWithWhileWithSse2(i);
}
}
[Benchmark(Baseline = false)]
public void MixedApproachWithSubtractWithWhile()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithMixedApproachWithSubtractWithWhile(i);
}
}
[Benchmark(Baseline = false)]
public void MixedApproachWithSubtractWithWhileAsVector()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithMixedApproachWithSubtractWithWhileAsVector(i);
}
}
[Benchmark(Baseline = false)]
public void MixedApproachWithSubtractWithWhileWithSse2()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithMixedApproachWithSubtractWithWhileWithSse2(i);
}
}
[Benchmark(Baseline = false)]
public void MixedApproachWithSubtractWithStackalloc()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithMixedApproachWithSubtractWithStackalloc(i);
}
}
[Benchmark(Baseline = false)]
public void MixedApproachWithForLoopOptimized()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithMixedApproachWithForLoopOptimized(i);
}
}
}
using BenchmarkDotNet.Attributes;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
#pragma warning disable xUnit1026, RCS1163, IDE0060
namespace Recyclable.Collections.Benchmarks.POC
{
[MemoryDiagnoser]
public class ModuloPocBenchmarks
{
public static void Run()
{
var benchmark = new ModuloPocBenchmarks();
benchmark.Setup();
benchmark.AddWithFor();
benchmark.AddWithForOptimized();
benchmark.DividerAndModulusWithOperator();
benchmark.MathDivRem();
benchmark.MixedApproachWithForLoopOptimized();
benchmark.MixedApproachWithSubtractWithStackalloc();
benchmark.MixedApproachWithSubtractWithWhile();
benchmark.MixedApproachWithSubtractWithWhileAsVector();
benchmark.MixedApproachWithSubtractWithWhileWithSse2();
benchmark.SubtractWithWhile();
benchmark.SubtractWithWhileWithSse2();
benchmark.XDivYMulX();
benchmark.XDivYMulXWithLocalVar();
}
private long _cutoff;
private Vector128<long> _increment;
private long _lastCaseIndex;
[GlobalSetup]
public void Setup()
{
_cutoff = checked((long)BlockSize << 4);
_increment = Vector128.Create(-BlockSize, 1L);
_lastCaseIndex = 0;
}
private static void DoNothing<T>(T result, T remainder, [CallerMemberName] string? callerName = null)
{
//Console.WriteLine($"{callerName}: result = {result}, remainder = {remainder}");
}
//[Params(123, 10, 2, int.MaxValue, int.MaxValue / 2, int.MaxValue / 3, int.MaxValue / 4, int.MaxValue / 6, int.MaxValue / 10, int.MaxValue / 15, int.MaxValue / 20)]
[Params(int.MaxValue, int.MaxValue / 2, int.MaxValue / 3, int.MaxValue / 4, int.MaxValue / 6, int.MaxValue / 8, int.MaxValue / 10, int.MaxValue / 100)]
//[Params(int.MaxValue / 10, int.MaxValue / 20, int.MaxValue / 100)]
public int BlockSize { get; set; } = int.MaxValue;
[Params(int.MaxValue, int.MaxValue / 1000, int.MaxValue / 10000)]
//[Params(int.MaxValue)]
//[Params(int.MaxValue * 100L)]
public long TestNumber { get; set; } = int.MaxValue;
private void CalculateDividerAndModulusWithOperator(long i)
{
DoNothing(i / BlockSize, i % BlockSize);
}
private void CalculateWithMathDivRem(long i)
{
DoNothing(Math.DivRem(i, BlockSize, out long remainder), remainder);
}
private void CalculateWithAddWithFor(long i)
{
int result = 0;
int remainder = 0;
int blockSize = BlockSize;
for (long iteration = 0; iteration < i; iteration++)
{
if (remainder > blockSize - 1)
{
remainder = 0;
result++;
}
remainder++;
}
DoNothing(result, remainder);
}
private void CalculateWithAddWithForOptimized(long i)
{
long total = 0;
int result = 0;
int blockSize = BlockSize;
while (total + blockSize <= i)
{
total += blockSize;
result++;
}
DoNothing(result, i - total);
}
private void CalculateWithXDivYMulX(long i)
{
DoNothing(i / BlockSize, i - (i / BlockSize * BlockSize));
}
private void CalculateWithXDivYMulXWithLocalVar(long i)
{
int blockSize = BlockSize;
int result = (int)(i / blockSize);
DoNothing(result, i - (result * blockSize));
}
private void CalculateWithSubtractWithWhile(long i)
{
int result = 0;
long remainder = i;
int blockSize = BlockSize;
while (remainder >= blockSize)
{
remainder -= blockSize;
result++;
}
DoNothing(result, remainder);
}
private void CalculateWithSubtractWithWhileWithSse2(long i)
{
var state = Vector128.Create(i, 0);
int blockSize = BlockSize;
var inc = _increment;
while (state.GetElement(0) >= blockSize)
{
state = Sse2.Add(state, inc);
}
DoNothing(state.GetElement(1), state.GetElement(0));
}
private void CalculateWithMixedApproachWithSubtractWithWhile(long i)
{
long remainder;
if (i >= _cutoff)
{
DoNothing(Math.DivRem(i, BlockSize, out remainder), remainder);
}
else
{
int result = 0;
int blockSize = BlockSize;
remainder = i;
while (remainder >= blockSize)
{
remainder -= blockSize;
result++;
}
DoNothing(result, remainder);
}
}
private void CalculateWithMixedApproachWithSubtractWithWhileAsVector(long i)
{
if (i >= _cutoff)
{
DoNothing(Math.DivRem(i, BlockSize, out long remainder), remainder);
}
else
{
var state = new Vector<long>(new[] { 0L, i, 0, 0 });
var increment = new Vector<long>(new[] { 1L, -BlockSize, 0, 0 });
int blockSize = BlockSize;
while (state[1] >= blockSize)
{
state += increment;
}
DoNothing(state[0], state[1]);
}
}
private void CalculateWithMixedApproachWithSubtractWithWhileWithSse2(long i)
{
if (i >= _cutoff)
{
DoNothing(Math.DivRem(i, BlockSize, out long remainder), remainder);
}
else
{
var increment = _increment;
var state = Vector128.Create(i, 0);
int blockSize = BlockSize;
while (state.GetElement(0) >= blockSize)
{
state = Sse2.Add(state, increment);
}
DoNothing(state.GetElement(1), state.GetElement(0));
}
}
private void CalculateWithMixedApproachWithSubtractWithStackalloc(long i)
{
if (i >= _cutoff)
{
DoNothing(Math.DivRem(i, BlockSize, out long remainder), remainder);
}
else
{
Span<long> state = stackalloc long[2] { i, 0 };// Vector128.Create(BlockSize, 0);
int blockSize = BlockSize;
while (state[0] >= blockSize)
{
state[0] -= blockSize;
state[1]++;
}
DoNothing(state[1], state[0]);
}
}
private void CalculateWithMixedApproachWithForLoopOptimized(long i)
{
long total;
if (i >= _cutoff)
{
DoNothing(Math.DivRem(i, BlockSize, out total), total);
}
else
{
total = 0;
int result = 0;
int blockSize = BlockSize;
while (total + blockSize <= i)
{
total += blockSize;
result++;
}
DoNothing(result, i - total);
}
}
[Benchmark(Baseline = true)]
public void DividerAndModulusWithOperator()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateDividerAndModulusWithOperator(i);
}
}
[Benchmark]
public void MathDivRem()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithMathDivRem(i);
}
}
[Benchmark]
public void AddWithFor()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithAddWithFor(i);
}
}
[Benchmark(Baseline = true)]
public void AddWithForOptimized()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithAddWithForOptimized(i);
}
}
[Benchmark]
public void XDivYMulX()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithXDivYMulX(i);
}
}
[Benchmark]
public void XDivYMulXWithLocalVar()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithXDivYMulXWithLocalVar(i);
}
}
[Benchmark]
public void SubtractWithWhile()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithSubtractWithWhile(i);
}
}
[Benchmark]
public void SubtractWithWhileWithSse2()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithSubtractWithWhileWithSse2(i);
}
}
[Benchmark(Baseline = false)]
public void MixedApproachWithSubtractWithWhile()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithMixedApproachWithSubtractWithWhile(i);
}
}
[Benchmark(Baseline = false)]
public void MixedApproachWithSubtractWithWhileAsVector()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithMixedApproachWithSubtractWithWhileAsVector(i);
}
}
[Benchmark(Baseline = false)]
public void MixedApproachWithSubtractWithWhileWithSse2()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithMixedApproachWithSubtractWithWhileWithSse2(i);
}
}
[Benchmark(Baseline = false)]
public void MixedApproachWithSubtractWithStackalloc()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithMixedApproachWithSubtractWithStackalloc(i);
}
}
[Benchmark(Baseline = false)]
public void MixedApproachWithForLoopOptimized()
{
long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--)
{
CalculateWithMixedApproachWithForLoopOptimized(i);
}
}
}
}
@@ -1,142 +1,142 @@
using BenchmarkDotNet.Attributes;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace Recyclable.Collections.Benchmarks.POC
{
public abstract class PocBenchmarkBase<TBenchmarkType>
{
public virtual TBenchmarkType? DataType { get; set; }
public virtual TBenchmarkType? BaseDataType { get; set; }
private Action? _testMethod;
protected Action TestMethod
{
get => _testMethod ?? throw new OperationCanceledException("Skip - No actual defined");
set => _testMethod = value;
}
private Action? _baselineMethod;
protected Action BaselineMethod
{
get => _baselineMethod ?? throw new OperationCanceledException("Skip - No baseline defined");
set => _baselineMethod = value;
}
[Benchmark(Baseline = true)]
public void Baseline() => BaselineMethod.Invoke();
[Benchmark]
public void Actual() => TestMethod.Invoke();
// 0 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524_288
// 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912
// 1073741824 2147483648
// [Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 32, 40, 50, 60, 80, 90, 100, 128, 256, 512, 1024, 2048, 4096, 8192, 1_000_000)]
// [Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 32, 40, 50, 60, 80, 90, 100, 128, 256, 512, 1024, 2048, 4096, 8192, 1_000_000)]
// [Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 32, 40, 50, 60, 80, 90, 100, 128, 256, 512, 1024, 2048, 4096, 8192, 100_000_000)]
// [Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 32, 40, 50, 60, 80, 90, 100, 128, 256, 512, 1024, 2048, 4096, 8192, 850_000 - 1, 1_000_000, RecyclableDefaults.MaxPooledBlockSize)]
// [Params(1_000_000, 10_000_000, 100_000_000, 1_000_000_000)]
// [Params(1, 10, 64, 128, 192, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144)]
// [Params(524_288, 786_432, 1_048_576)]
// [Params(262_144, 393_216, 524_288)]
// [Params(850_000_000)]
// [Params(100_000_000)]
// [Params(RecyclableDefaults.MinItemsCountForParallelization)]
// [Params(127)]
// [Params(16)]
// [Params(10, 15, 128)]
// [Params(1)]
// [Params(RecyclableDefaults.MaxPooledBlockSize)]
// [Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 32, 40, 50, 60, 80, 90, 100, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 10_000_000, 33_554_432, 50_000_000, 100_000_000)]
// *** [Params(RecyclableDefaults.MinPooledArrayLength - 3, RecyclableDefaults.MinPooledArrayLength - 2, RecyclableDefaults.MinPooledArrayLength - 1, RecyclableDefaults.MinPooledArrayLength, RecyclableDefaults.MinPooledArrayLength + 1, RecyclableDefaults.MinPooledArrayLength + 2, RecyclableDefaults.MinPooledArrayLength + 3)]
[Params(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 32, 40, RecyclableDefaults.MinPooledArrayLength - 1, RecyclableDefaults.MinPooledArrayLength, 40, 50, 60, 70, 80, 90, 100, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, RecyclableDefaults.MinItemsCountForParallelization)]
// *** [Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 32, 40, RecyclableDefaults.MinPooledArrayLength - 1, RecyclableDefaults.MinPooledArrayLength, 40, 50, 60, 70, 80, 90, 100, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, RecyclableDefaults.MinItemsCountForParallelization)]
// [Params(256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, RecyclableDefaults.MinItemsCountForParallelization)]
// [Params(RecyclableDefaults.MinItemsCountForParallelization, 1_048_576, 2_097_152, 4_194_304, 8_388_608, 16_777_216, 33_554_432, RecyclableDefaults.MaxPooledBlockSize)]
// *** [Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, RecyclableDefaults.MinPooledArrayLength - 1, RecyclableDefaults.MinPooledArrayLength, 40, 50, 60, 64, 70, 80, 90, 100, 127, 128)]
// [Params(1, 10, 1024, 10240, RecyclableDefaults.MinPooledArrayLength, RecyclableDefaults.MinItemsCountForParallelization)]
// [Params(RecyclableDefaults.MinItemsCountForParallelization * 3)]
// [Params(1_048_576)]
public virtual int TestObjectCount { get; set; } = 0; //8_192;//131_072; //1; //33_554_432; //524_288; //131_072;
public virtual int TestObjectCountForSlowMethods { get; private set; }
// [Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200)]
// [Params(10, 20, 50, 100)]
// [Params(1, 10)]
public int Divider = 10;
//public int BlockSize = 1_048_576;//int.MaxValue - 1;
public int BlockSize { get; private set; }
public byte BlockSizePow2BitShift => checked((byte)(31 - BitOperations.LeadingZeroCount((uint)BlockSize)));
[MethodImpl(MethodImplOptions.NoInlining)]
protected static void DoNothing<T>(in T item)
{
_ = item;
}
protected static Exception CreateUnknownBenchmarkTypeException<T>(T benchmarkType)
{
return new InvalidOperationException($"******* UNKNOWN BENCHMARK TYPE {{{benchmarkType}}} *******");
}
protected static Exception CreateMethodNotFoundException(in string methodName, in string? className)
{
return new MethodAccessException($"******* METHOD {{{methodName}}} NOT FOUND IN CLASS {{{className}}} *******");
}
protected virtual void PrepareData<T>(T benchmarkType) { }
protected virtual Action? GetTestMethod(TBenchmarkType? benchmarkType) => null;
[GlobalCleanup]
public virtual void Cleanup()
{
_baselineMethod = null;
_testMethod = null;
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
}
[GlobalSetup]
public virtual void Setup()
{
Console.WriteLine($"******* SETTING UP TEST CASE FOR BENCHMARK {{{DataType}}} *******");
TestObjectCountForSlowMethods = (TestObjectCount / 2) + 1;
BlockSize = TestObjectCount switch
{
0 => 1,
> 0 and <= 10_240 => TestObjectCount,
_ => TestObjectCount / Divider > 0 ? TestObjectCount / Divider : TestObjectCount
};
if (BlockSize > 0)
{
BlockSize = checked((int)BitOperations.RoundUpToPowerOf2((uint)BlockSize));
}
Console.WriteLine($"******* BaseDataType = {BaseDataType}; default = {default(TBenchmarkType)} *******");
if (!EqualityComparer<TBenchmarkType>.Default.Equals(BaseDataType, default))
{
Console.WriteLine("******* SETTING UP BASELINE DATA *******");
PrepareData(BaseDataType);
_baselineMethod = GetTestMethod(BaseDataType);
}
//~ We don't want to prepare data if baseline data is the same is benchmark data.
//~ One would override another - waste of time & resources.
if (!EqualityComparer<TBenchmarkType>.Default.Equals(BaseDataType, DataType))
{
Console.WriteLine("******* SETTING UP TEST CASE DATA *******");
//~ If BaseDataType == null, then we'll come here only if DataType != null.
PrepareData(DataType!);
_testMethod = GetTestMethod(DataType);
}
Console.WriteLine("******* DATA PREPARED *******");
}
}
}
using BenchmarkDotNet.Attributes;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace Recyclable.Collections.Benchmarks.POC
{
public abstract class PocBenchmarkBase<TBenchmarkType>
{
public virtual TBenchmarkType? DataType { get; set; }
public virtual TBenchmarkType? BaseDataType { get; set; }
private Action? _testMethod;
protected Action TestMethod
{
get => _testMethod ?? throw new OperationCanceledException("Skip - No actual defined");
set => _testMethod = value;
}
private Action? _baselineMethod;
protected Action BaselineMethod
{
get => _baselineMethod ?? throw new OperationCanceledException("Skip - No baseline defined");
set => _baselineMethod = value;
}
[Benchmark(Baseline = true)]
public void Baseline() => BaselineMethod.Invoke();
[Benchmark]
public void Actual() => TestMethod.Invoke();
// 0 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524_288
// 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912
// 1073741824 2147483648
// [Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 32, 40, 50, 60, 80, 90, 100, 128, 256, 512, 1024, 2048, 4096, 8192, 1_000_000)]
// [Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 32, 40, 50, 60, 80, 90, 100, 128, 256, 512, 1024, 2048, 4096, 8192, 1_000_000)]
// [Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 32, 40, 50, 60, 80, 90, 100, 128, 256, 512, 1024, 2048, 4096, 8192, 100_000_000)]
// [Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 32, 40, 50, 60, 80, 90, 100, 128, 256, 512, 1024, 2048, 4096, 8192, 850_000 - 1, 1_000_000, RecyclableDefaults.MaxPooledBlockSize)]
// [Params(1_000_000, 10_000_000, 100_000_000, 1_000_000_000)]
// [Params(1, 10, 64, 128, 192, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144)]
// [Params(524_288, 786_432, 1_048_576)]
// [Params(262_144, 393_216, 524_288)]
// [Params(850_000_000)]
// [Params(100_000_000)]
// [Params(RecyclableDefaults.MinItemsCountForParallelization)]
// [Params(127)]
// [Params(16)]
// [Params(10, 15, 128)]
// [Params(1)]
// [Params(RecyclableDefaults.MaxPooledBlockSize)]
// [Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 32, 40, 50, 60, 80, 90, 100, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 10_000_000, 33_554_432, 50_000_000, 100_000_000)]
// *** [Params(RecyclableDefaults.MinPooledArrayLength - 3, RecyclableDefaults.MinPooledArrayLength - 2, RecyclableDefaults.MinPooledArrayLength - 1, RecyclableDefaults.MinPooledArrayLength, RecyclableDefaults.MinPooledArrayLength + 1, RecyclableDefaults.MinPooledArrayLength + 2, RecyclableDefaults.MinPooledArrayLength + 3)]
[Params(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 32, 40, RecyclableDefaults.MinPooledArrayLength - 1, RecyclableDefaults.MinPooledArrayLength, 40, 50, 60, 70, 80, 90, 100, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, RecyclableDefaults.MinItemsCountForParallelization)]
// *** [Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 32, 40, RecyclableDefaults.MinPooledArrayLength - 1, RecyclableDefaults.MinPooledArrayLength, 40, 50, 60, 70, 80, 90, 100, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, RecyclableDefaults.MinItemsCountForParallelization)]
// [Params(256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, RecyclableDefaults.MinItemsCountForParallelization)]
// [Params(RecyclableDefaults.MinItemsCountForParallelization, 1_048_576, 2_097_152, 4_194_304, 8_388_608, 16_777_216, 33_554_432, RecyclableDefaults.MaxPooledBlockSize)]
// *** [Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, RecyclableDefaults.MinPooledArrayLength - 1, RecyclableDefaults.MinPooledArrayLength, 40, 50, 60, 64, 70, 80, 90, 100, 127, 128)]
// [Params(1, 10, 1024, 10240, RecyclableDefaults.MinPooledArrayLength, RecyclableDefaults.MinItemsCountForParallelization)]
// [Params(RecyclableDefaults.MinItemsCountForParallelization * 3)]
// [Params(1_048_576)]
public virtual int TestObjectCount { get; set; } = 0; //8_192;//131_072; //1; //33_554_432; //524_288; //131_072;
public virtual int TestObjectCountForSlowMethods { get; private set; }
// [Params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200)]
// [Params(10, 20, 50, 100)]
// [Params(1, 10)]
public int Divider = 10;
//public int BlockSize = 1_048_576;//int.MaxValue - 1;
public int BlockSize { get; private set; }
public byte BlockSizePow2BitShift => checked((byte)(31 - BitOperations.LeadingZeroCount((uint)BlockSize)));
[MethodImpl(MethodImplOptions.NoInlining)]
protected static void DoNothing<T>(in T item)
{
_ = item;
}
protected static Exception CreateUnknownBenchmarkTypeException<T>(T benchmarkType)
{
return new InvalidOperationException($"******* UNKNOWN BENCHMARK TYPE {{{benchmarkType}}} *******");
}
protected static Exception CreateMethodNotFoundException(in string methodName, in string? className)
{
return new MethodAccessException($"******* METHOD {{{methodName}}} NOT FOUND IN CLASS {{{className}}} *******");
}
protected virtual void PrepareData<T>(T benchmarkType) { }
protected virtual Action? GetTestMethod(TBenchmarkType? benchmarkType) => null;
[GlobalCleanup]
public virtual void Cleanup()
{
_baselineMethod = null;
_testMethod = null;
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
}
[GlobalSetup]
public virtual void Setup()
{
Console.WriteLine($"******* SETTING UP TEST CASE FOR BENCHMARK {{{DataType}}} *******");
TestObjectCountForSlowMethods = (TestObjectCount / 2) + 1;
BlockSize = TestObjectCount switch
{
0 => 1,
> 0 and <= 10_240 => TestObjectCount,
_ => TestObjectCount / Divider > 0 ? TestObjectCount / Divider : TestObjectCount
};
if (BlockSize > 0)
{
BlockSize = checked((int)BitOperations.RoundUpToPowerOf2((uint)BlockSize));
}
Console.WriteLine($"******* BaseDataType = {BaseDataType}; default = {default(TBenchmarkType)} *******");
if (!EqualityComparer<TBenchmarkType>.Default.Equals(BaseDataType, default))
{
Console.WriteLine("******* SETTING UP BASELINE DATA *******");
PrepareData(BaseDataType);
_baselineMethod = GetTestMethod(BaseDataType);
}
//~ We don't want to prepare data if baseline data is the same is benchmark data.
//~ One would override another - waste of time & resources.
if (!EqualityComparer<TBenchmarkType>.Default.Equals(BaseDataType, DataType))
{
Console.WriteLine("******* SETTING UP TEST CASE DATA *******");
//~ If BaseDataType == null, then we'll come here only if DataType != null.
PrepareData(DataType!);
_testMethod = GetTestMethod(DataType);
}
Console.WriteLine("******* DATA PREPARED *******");
}
}
}
@@ -1,47 +1,47 @@
using System.Reflection;
using BenchmarkDotNet.Attributes;
using Collections.Pooled;
using Recyclable.Collections.Benchmarks.Core;
namespace Recyclable.Collections.Benchmarks.POC
{
[MemoryDiagnoser]
public partial class RecyclableLongListPocBenchmarks<T> : PocBenchmarkBase<T>
{
private long[]? _testArray;
public long[] TestObjects => _testArray ?? throw new NullReferenceException("Something is wrong and test objects are null");
public RecyclableLongList<long> TestObjectsAsRecyclableLongList => _testRecyclableLongList ?? throw new NullReferenceException($"Something is wrong and {nameof(RecyclableLongList<long>)} is null");
private RecyclableLongList<long>? _testRecyclableLongList;
private PooledList<long>? _testPooledList;
internal PooledList<long> TestPooledList => _testPooledList ?? throw new NullReferenceException($"Something is wrong and {nameof(PooledList<long>)} is null");
private static readonly MethodInfo _ensureCapacityNewFunc;
static RecyclableLongListPocBenchmarks()
{
_ensureCapacityNewFunc = typeof(RecyclableLongList<long>).GetMethod("EnsureCapacity", BindingFlags.Static | BindingFlags.NonPublic)
?? throw new NullReferenceException($"Method EnsureCapacity not found in class {nameof(RecyclableLongList<long>)}");
}
public override void Setup()
{
Console.WriteLine("******* SETTING UP EXPECTED RESULTS DATA *******");
_testArray = DataGenerator.EnumerateTestObjects(TestObjectCount);
base.Setup();
}
public override void Cleanup()
{
_testArray = null;
_testRecyclableLongList?.Dispose();
_testRecyclableLongList = null;
_testPooledList?.Dispose();
_testPooledList = null;
base.Cleanup();
}
}
}
using System.Reflection;
using BenchmarkDotNet.Attributes;
using Collections.Pooled;
using Recyclable.Collections.Benchmarks.Core;
namespace Recyclable.Collections.Benchmarks.POC
{
[MemoryDiagnoser]
public partial class RecyclableLongListPocBenchmarks<T> : PocBenchmarkBase<T>
{
private long[]? _testArray;
public long[] TestObjects => _testArray ?? throw new NullReferenceException("Something is wrong and test objects are null");
public RecyclableLongList<long> TestObjectsAsRecyclableLongList => _testRecyclableLongList ?? throw new NullReferenceException($"Something is wrong and {nameof(RecyclableLongList<long>)} is null");
private RecyclableLongList<long>? _testRecyclableLongList;
private PooledList<long>? _testPooledList;
internal PooledList<long> TestPooledList => _testPooledList ?? throw new NullReferenceException($"Something is wrong and {nameof(PooledList<long>)} is null");
private static readonly MethodInfo _ensureCapacityNewFunc;
static RecyclableLongListPocBenchmarks()
{
_ensureCapacityNewFunc = typeof(RecyclableLongList<long>).GetMethod("EnsureCapacity", BindingFlags.Static | BindingFlags.NonPublic)
?? throw new NullReferenceException($"Method EnsureCapacity not found in class {nameof(RecyclableLongList<long>)}");
}
public override void Setup()
{
Console.WriteLine("******* SETTING UP EXPECTED RESULTS DATA *******");
_testArray = DataGenerator.EnumerateTestObjects(TestObjectCount);
base.Setup();
}
public override void Cleanup()
{
_testArray = null;
_testRecyclableLongList?.Dispose();
_testRecyclableLongList = null;
_testPooledList?.Dispose();
_testPooledList = null;
base.Cleanup();
}
}
}
@@ -1,26 +1,26 @@
BenchmarkDotNet=v0.13.3, OS=ubuntu 20.04
Intel Xeon Platinum 8272CL CPU 2.60GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK=7.0.203
[Host] : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2
DefaultJob : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2
| Method | Mean | Error | StdDev | Ratio | RatioSD |
|------------------------- |--------:|---------:|---------:|------:|--------:|
| RecyclableLongList_IndexOfV1 | 2.274 s | 0.0447 s | 0.0894 s | 1.00 | 0.04 |
| RecyclableLongList_IndexOfV2 | 2.302 s | 0.0456 s | 0.0750 s | 1.00 | 0.00 |
| RecyclableLongList_IndexOf | 2.234 s | 0.0446 s | 0.0681 s | 0.97 | 0.05 |
// * Hints *
Outliers
RecyclableLongListPocBenchmarks.RecyclableLongList_IndexOfV2: Default -> 2 outliers were removed (2.54 s, 2.56 s)
RecyclableLongListPocBenchmarks.RecyclableLongList_IndexOf: Default -> 1 outlier was detected (2.06 s)
// * Legends *
Mean : Arithmetic mean of all measurements
Error : Half of 99.9% confidence interval
StdDev : Standard deviation of all measurements
Ratio : Mean of the ratio distribution ([Current]/[Baseline])
RatioSD : Standard deviation of the ratio distribution ([Current]/[Baseline])
1 s : 1 Second (1 sec)
BenchmarkDotNet=v0.13.3, OS=ubuntu 20.04
Intel Xeon Platinum 8272CL CPU 2.60GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK=7.0.203
[Host] : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2
DefaultJob : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2
| Method | Mean | Error | StdDev | Ratio | RatioSD |
|------------------------- |--------:|---------:|---------:|------:|--------:|
| RecyclableLongList_IndexOfV1 | 2.274 s | 0.0447 s | 0.0894 s | 1.00 | 0.04 |
| RecyclableLongList_IndexOfV2 | 2.302 s | 0.0456 s | 0.0750 s | 1.00 | 0.00 |
| RecyclableLongList_IndexOf | 2.234 s | 0.0446 s | 0.0681 s | 0.97 | 0.05 |
// * Hints *
Outliers
RecyclableLongListPocBenchmarks.RecyclableLongList_IndexOfV2: Default -> 2 outliers were removed (2.54 s, 2.56 s)
RecyclableLongListPocBenchmarks.RecyclableLongList_IndexOf: Default -> 1 outlier was detected (2.06 s)
// * Legends *
Mean : Arithmetic mean of all measurements
Error : Half of 99.9% confidence interval
StdDev : Standard deviation of all measurements
Ratio : Mean of the ratio distribution ([Current]/[Baseline])
RatioSD : Standard deviation of the ratio distribution ([Current]/[Baseline])
1 s : 1 Second (1 sec)
@@ -1,364 +1,364 @@
using BenchmarkDotNet.Attributes;
#pragma warning disable RCS1085
namespace Recyclable.Collections.Benchmarks.POC
{
public class RefVsInstanceMemberPocBenchmarks
{
public static void Run()
{
var benchmark = new RefVsInstanceMemberPocBenchmarks();
benchmark.ArrayInstanceMember2();
benchmark.ArrayInstanceMember3();
benchmark.ArrayInstanceMember4();
benchmark.ArrayInstanceMember5();
benchmark.ArrayInstanceMember6();
benchmark.ArrayInstanceMemberLocalVar5();
benchmark.ArrayInstanceMemberLocalVar6();
benchmark.IntInstanceMember2();
benchmark.IntInstanceMember3();
benchmark.IntInstanceMember4();
benchmark.IntInstanceMember5();
benchmark.RefToArray2();
benchmark.RefToArray3();
benchmark.RefToArray4();
benchmark.RefToArray5();
benchmark.RefToArray6();
benchmark.RefToInt2();
benchmark.RefToInt3();
}
private readonly TestCase _testCase = new();
private static void DoNothing(int count)
{
_ = count;
}
private class TestCase
{
private int[] _items = new int[123];
private int _count = 123;
private static int Add(int count)
{
return count;
}
public int Count
{
get => _count;
set => _count = value;
}
public ref int GetCountAsRefCount2()
{
ref int count = ref _count;
count = 234;
return ref count;
}
public ref int GetCountAsRefCount3()
{
ref int count = ref _count;
count = 234;
_ = TestCase.Add(count);
return ref count;
}
public ref int GetCountAsRefCount4()
{
ref int count = ref _count;
count = 234;
_ = TestCase.Add(count);
_ = TestCase.Add(count);
return ref count;
}
public ref int GetCountAsRefCount5()
{
ref int count = ref _count;
count = 234;
_ = TestCase.Add(count);
_ = TestCase.Add(count);
_ = TestCase.Add(count);
return ref count;
}
public int GetCountAsInstanceMember2()
{
_count = 234;
return _count;
}
public int GetCountAsInstanceMember3()
{
_count = 234;
_ = TestCase.Add(_count);
return _count;
}
public int GetCountAsInstanceMember4()
{
_count = 234;
_ = TestCase.Add(_count);
_ = TestCase.Add(_count);
return _count;
}
public int GetCountAsInstanceMember5()
{
_count = 234;
_ = TestCase.Add(_count);
_ = TestCase.Add(_count);
_ = TestCase.Add(_count);
return _count;
}
public int GetLengthAsArray2()
{
_ = TestCase.Add(_items.Length);
return _items.Length;
}
public int GetLengthAsArray3()
{
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
return _items.Length;
}
public int GetLengthAsArray4()
{
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
return _items.Length;
}
public int GetLengthAsArray5()
{
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
return _items.Length;
}
public int GetLengthAsArray6()
{
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
return _items.Length;
}
public int GetLengthAsArrayLocalVar5()
{
var items = _items;
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
return items.Length;
}
public int GetLengthAsArrayLocalVar6()
{
var items = _items;
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
return items.Length;
}
public int GetLengthAsRefArray2()
{
ref int[] items = ref _items;
_ = TestCase.Add(items.Length);
return items.Length;
}
public int GetLengthAsRefArray3()
{
ref int[] items = ref _items;
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
return items.Length;
}
public int GetLengthAsRefArray4()
{
ref int[] items = ref _items;
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
return items.Length;
}
public int GetLengthAsRefArray5()
{
ref int[] items = ref _items;
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
return items.Length;
}
public int GetLengthAsRefArray6()
{
ref int[] items = ref _items;
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
return items.Length;
}
}
[Benchmark]
public void RefToInt2()
{
var count = _testCase.GetCountAsRefCount2();
DoNothing(count);
}
[Benchmark]
public void RefToInt3()
{
var count = _testCase.GetCountAsRefCount3();
DoNothing(count);
}
[Benchmark]
public void RefToInt4()
{
var count = _testCase.GetCountAsRefCount4();
DoNothing(count);
}
[Benchmark]
public void RefToInt5()
{
var count = _testCase.GetCountAsRefCount5();
DoNothing(count);
}
[Benchmark(Baseline = true)]
public void IntInstanceMember2()
{
var count = _testCase.GetCountAsInstanceMember2();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void IntInstanceMember3()
{
var count = _testCase.GetCountAsInstanceMember3();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void IntInstanceMember4()
{
var count = _testCase.GetCountAsInstanceMember4();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void IntInstanceMember5()
{
var count = _testCase.GetCountAsInstanceMember5();
DoNothing(count);
}
[Benchmark]
public void RefToArray2()
{
var count = _testCase.GetLengthAsRefArray2();
DoNothing(count);
}
[Benchmark]
public void RefToArray3()
{
var count = _testCase.GetLengthAsRefArray3();
DoNothing(count);
}
[Benchmark]
public void RefToArray4()
{
var count = _testCase.GetLengthAsRefArray4();
DoNothing(count);
}
[Benchmark]
public void RefToArray5()
{
var count = _testCase.GetLengthAsRefArray5();
DoNothing(count);
}
[Benchmark]
public void RefToArray6()
{
var count = _testCase.GetLengthAsRefArray6();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void ArrayInstanceMember2()
{
var count = _testCase.GetLengthAsArray2();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void ArrayInstanceMember3()
{
var count = _testCase.GetLengthAsArray3();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void ArrayInstanceMember4()
{
var count = _testCase.GetLengthAsArray4();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void ArrayInstanceMember5()
{
var count = _testCase.GetLengthAsArray5();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void ArrayInstanceMember6()
{
var count = _testCase.GetLengthAsArray6();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void ArrayInstanceMemberLocalVar5()
{
var count = _testCase.GetLengthAsArrayLocalVar5();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void ArrayInstanceMemberLocalVar6()
{
var count = _testCase.GetLengthAsArrayLocalVar6();
DoNothing(count);
}
}
}
using BenchmarkDotNet.Attributes;
#pragma warning disable RCS1085
namespace Recyclable.Collections.Benchmarks.POC
{
public class RefVsInstanceMemberPocBenchmarks
{
public static void Run()
{
var benchmark = new RefVsInstanceMemberPocBenchmarks();
benchmark.ArrayInstanceMember2();
benchmark.ArrayInstanceMember3();
benchmark.ArrayInstanceMember4();
benchmark.ArrayInstanceMember5();
benchmark.ArrayInstanceMember6();
benchmark.ArrayInstanceMemberLocalVar5();
benchmark.ArrayInstanceMemberLocalVar6();
benchmark.IntInstanceMember2();
benchmark.IntInstanceMember3();
benchmark.IntInstanceMember4();
benchmark.IntInstanceMember5();
benchmark.RefToArray2();
benchmark.RefToArray3();
benchmark.RefToArray4();
benchmark.RefToArray5();
benchmark.RefToArray6();
benchmark.RefToInt2();
benchmark.RefToInt3();
}
private readonly TestCase _testCase = new();
private static void DoNothing(int count)
{
_ = count;
}
private class TestCase
{
private int[] _items = new int[123];
private int _count = 123;
private static int Add(int count)
{
return count;
}
public int Count
{
get => _count;
set => _count = value;
}
public ref int GetCountAsRefCount2()
{
ref int count = ref _count;
count = 234;
return ref count;
}
public ref int GetCountAsRefCount3()
{
ref int count = ref _count;
count = 234;
_ = TestCase.Add(count);
return ref count;
}
public ref int GetCountAsRefCount4()
{
ref int count = ref _count;
count = 234;
_ = TestCase.Add(count);
_ = TestCase.Add(count);
return ref count;
}
public ref int GetCountAsRefCount5()
{
ref int count = ref _count;
count = 234;
_ = TestCase.Add(count);
_ = TestCase.Add(count);
_ = TestCase.Add(count);
return ref count;
}
public int GetCountAsInstanceMember2()
{
_count = 234;
return _count;
}
public int GetCountAsInstanceMember3()
{
_count = 234;
_ = TestCase.Add(_count);
return _count;
}
public int GetCountAsInstanceMember4()
{
_count = 234;
_ = TestCase.Add(_count);
_ = TestCase.Add(_count);
return _count;
}
public int GetCountAsInstanceMember5()
{
_count = 234;
_ = TestCase.Add(_count);
_ = TestCase.Add(_count);
_ = TestCase.Add(_count);
return _count;
}
public int GetLengthAsArray2()
{
_ = TestCase.Add(_items.Length);
return _items.Length;
}
public int GetLengthAsArray3()
{
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
return _items.Length;
}
public int GetLengthAsArray4()
{
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
return _items.Length;
}
public int GetLengthAsArray5()
{
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
return _items.Length;
}
public int GetLengthAsArray6()
{
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
_ = TestCase.Add(_items.Length);
return _items.Length;
}
public int GetLengthAsArrayLocalVar5()
{
var items = _items;
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
return items.Length;
}
public int GetLengthAsArrayLocalVar6()
{
var items = _items;
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
return items.Length;
}
public int GetLengthAsRefArray2()
{
ref int[] items = ref _items;
_ = TestCase.Add(items.Length);
return items.Length;
}
public int GetLengthAsRefArray3()
{
ref int[] items = ref _items;
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
return items.Length;
}
public int GetLengthAsRefArray4()
{
ref int[] items = ref _items;
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
return items.Length;
}
public int GetLengthAsRefArray5()
{
ref int[] items = ref _items;
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
return items.Length;
}
public int GetLengthAsRefArray6()
{
ref int[] items = ref _items;
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
_ = TestCase.Add(items.Length);
return items.Length;
}
}
[Benchmark]
public void RefToInt2()
{
var count = _testCase.GetCountAsRefCount2();
DoNothing(count);
}
[Benchmark]
public void RefToInt3()
{
var count = _testCase.GetCountAsRefCount3();
DoNothing(count);
}
[Benchmark]
public void RefToInt4()
{
var count = _testCase.GetCountAsRefCount4();
DoNothing(count);
}
[Benchmark]
public void RefToInt5()
{
var count = _testCase.GetCountAsRefCount5();
DoNothing(count);
}
[Benchmark(Baseline = true)]
public void IntInstanceMember2()
{
var count = _testCase.GetCountAsInstanceMember2();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void IntInstanceMember3()
{
var count = _testCase.GetCountAsInstanceMember3();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void IntInstanceMember4()
{
var count = _testCase.GetCountAsInstanceMember4();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void IntInstanceMember5()
{
var count = _testCase.GetCountAsInstanceMember5();
DoNothing(count);
}
[Benchmark]
public void RefToArray2()
{
var count = _testCase.GetLengthAsRefArray2();
DoNothing(count);
}
[Benchmark]
public void RefToArray3()
{
var count = _testCase.GetLengthAsRefArray3();
DoNothing(count);
}
[Benchmark]
public void RefToArray4()
{
var count = _testCase.GetLengthAsRefArray4();
DoNothing(count);
}
[Benchmark]
public void RefToArray5()
{
var count = _testCase.GetLengthAsRefArray5();
DoNothing(count);
}
[Benchmark]
public void RefToArray6()
{
var count = _testCase.GetLengthAsRefArray6();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void ArrayInstanceMember2()
{
var count = _testCase.GetLengthAsArray2();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void ArrayInstanceMember3()
{
var count = _testCase.GetLengthAsArray3();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void ArrayInstanceMember4()
{
var count = _testCase.GetLengthAsArray4();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void ArrayInstanceMember5()
{
var count = _testCase.GetLengthAsArray5();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void ArrayInstanceMember6()
{
var count = _testCase.GetLengthAsArray6();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void ArrayInstanceMemberLocalVar5()
{
var count = _testCase.GetLengthAsArrayLocalVar5();
DoNothing(count);
}
[Benchmark(Baseline = false)]
public void ArrayInstanceMemberLocalVar6()
{
var count = _testCase.GetLengthAsArrayLocalVar6();
DoNothing(count);
}
}
}
@@ -1,45 +1,45 @@
using BenchmarkDotNet.Attributes;
namespace Recyclable.Collections.Benchmarks.POC
{
public enum RoundBenchmarkType
{
MathFloor,
MathRound,
WithTypecasting,
}
public class RoundBenchmarks : PocBenchmarkBase<RoundBenchmarkType>
{
[Params(RoundBenchmarkType.MathFloor)]
public override RoundBenchmarkType BaseDataType { get => base.BaseDataType; set => base.BaseDataType = value; }
[Params(RoundBenchmarkType.MathRound, RoundBenchmarkType.WithTypecasting)]
public override RoundBenchmarkType DataType { get => base.DataType; set => base.DataType = value; }
private const double OptimalParallelSearchStep = 0.329;
public void RoundWithMathFloor()
{
DoNothing((long)Math.Floor(TestObjectCount * OptimalParallelSearchStep));
}
public void RoundWithMathRound()
{
DoNothing((long)Math.Round(TestObjectCount * OptimalParallelSearchStep, 0));
}
public void RoundWithTypeCasting()
{
DoNothing((long)(TestObjectCount * OptimalParallelSearchStep));
}
protected override Action GetTestMethod(RoundBenchmarkType benchmarkType) => benchmarkType switch
{
RoundBenchmarkType.MathFloor => RoundWithMathFloor,
RoundBenchmarkType.MathRound => RoundWithMathRound,
RoundBenchmarkType.WithTypecasting => RoundWithTypeCasting,
_ => throw CreateUnknownBenchmarkTypeException(benchmarkType),
};
}
}
using BenchmarkDotNet.Attributes;
namespace Recyclable.Collections.Benchmarks.POC
{
public enum RoundBenchmarkType
{
MathFloor,
MathRound,
WithTypecasting,
}
public class RoundBenchmarks : PocBenchmarkBase<RoundBenchmarkType>
{
[Params(RoundBenchmarkType.MathFloor)]
public override RoundBenchmarkType BaseDataType { get => base.BaseDataType; set => base.BaseDataType = value; }
[Params(RoundBenchmarkType.MathRound, RoundBenchmarkType.WithTypecasting)]
public override RoundBenchmarkType DataType { get => base.DataType; set => base.DataType = value; }
private const double OptimalParallelSearchStep = 0.329;
public void RoundWithMathFloor()
{
DoNothing((long)Math.Floor(TestObjectCount * OptimalParallelSearchStep));
}
public void RoundWithMathRound()
{
DoNothing((long)Math.Round(TestObjectCount * OptimalParallelSearchStep, 0));
}
public void RoundWithTypeCasting()
{
DoNothing((long)(TestObjectCount * OptimalParallelSearchStep));
}
protected override Action GetTestMethod(RoundBenchmarkType benchmarkType) => benchmarkType switch
{
RoundBenchmarkType.MathFloor => RoundWithMathFloor,
RoundBenchmarkType.MathRound => RoundWithMathRound,
RoundBenchmarkType.WithTypecasting => RoundWithTypeCasting,
_ => throw CreateUnknownBenchmarkTypeException(benchmarkType),
};
}
}
@@ -1,147 +1,147 @@
using BenchmarkDotNet.Attributes;
namespace Recyclable.Collections.Benchmarks.POC
{
static class RoundUpToPowerOf2Alternatives
{
private static readonly int[] PowersOfTwo =
{
1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576,
2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824
};
public static int BinarySearch(int value)
{
if (value <= 0)
{
throw new ArgumentException("Input must be a positive integer.");
}
// Find the smallest power of 2 greater than or equal to value using a lookup table
int index = Array.BinarySearch(PowersOfTwo, value);
if (index < 0)
{
// If value is not a power of 2, use the next power of 2 from the table
index = ~index;
}
return PowersOfTwo[index];
}
public static int RightBitShifting(int value)
{
// Decrease value by 1 to handle the case where the input is already a power of 2
value--;
value |= value >> 1;
value |= value >> 2;
value |= value >> 4;
value |= value >> 8;
value |= value >> 16;
return value + 1;
}
public static int LeftBitShifting(int value)
{
if (value == 0)
{
return 1;
}
// Ensure value is a power of 2 or the next power of 2
int result = 1;
while (result < value)
{
result = checked(result << 1);
}
return result;
}
}
public enum RoundUpToPowerOf2BenchmarkType
{
Undefined,
BitOperations,
RightBitShifting,
LeftBitShifting,
BinarySearch,
IsPowOf2,
IsPowOf2_GreaterThanFirst,
IsPowOf2_WithIf,
IsPowOf2_GreaterEqual,
}
public class RoundUpToPowerOf2vsOtherBenchmarks : PocBenchmarkBase<RoundUpToPowerOf2BenchmarkType>
{
// [Params(-850_000, 850_000)]
[Params(-int.MaxValue, int.MaxValue)]
public override int TestObjectCount { get => base.TestObjectCount; set => base.TestObjectCount = value; }
[Params(RoundUpToPowerOf2BenchmarkType.IsPowOf2)]
public override RoundUpToPowerOf2BenchmarkType BaseDataType { get => base.BaseDataType; set => base.BaseDataType = value; }
[Params(RoundUpToPowerOf2BenchmarkType.IsPowOf2_GreaterThanFirst, RoundUpToPowerOf2BenchmarkType.IsPowOf2_WithIf, RoundUpToPowerOf2BenchmarkType.IsPowOf2_GreaterEqual)]
// [Params(RoundUpToPowerOf2BenchmarkType.RightBitShifting, RoundUpToPowerOf2BenchmarkType.LeftBitShifting, RoundUpToPowerOf2BenchmarkType.BinarySearch)]
public override RoundUpToPowerOf2BenchmarkType DataType { get => base.DataType; set => base.DataType = value; }
private void BitOperations()
{
DoNothing(System.Numerics.BitOperations.RoundUpToPowerOf2((uint)base.TestObjectCount));
}
private void RightBitShifting()
{
DoNothing(RoundUpToPowerOf2Alternatives.RightBitShifting(TestObjectCount));
}
private void LeftBitShifting()
{
DoNothing(RoundUpToPowerOf2Alternatives.LeftBitShifting(TestObjectCount));
}
private void BinarySearch()
{
DoNothing(RoundUpToPowerOf2Alternatives.BinarySearch(TestObjectCount));
}
private void IsPowOf2()
{
DoNothing((TestObjectCount & (TestObjectCount - 1)) == 0 && TestObjectCount > 0);
}
private void IsPowOf2_GreaterThanFirst()
{
DoNothing(TestObjectCount > 0 && (TestObjectCount & (TestObjectCount - 1)) == 0);
}
private void IsPowOf2_WithIf()
{
DoNothing(TestObjectCount > 0 ? (TestObjectCount & (TestObjectCount - 1)) == 0 : false);
}
private void IsPowOf2_GreaterEqual()
{
DoNothing(TestObjectCount >= 0 && TestObjectCount != 0 && (TestObjectCount & (TestObjectCount - 1)) == 0);
}
protected override Action? GetTestMethod(RoundUpToPowerOf2BenchmarkType benchmarkType) => benchmarkType switch
{
RoundUpToPowerOf2BenchmarkType.BitOperations => BitOperations,
RoundUpToPowerOf2BenchmarkType.RightBitShifting => RightBitShifting,
RoundUpToPowerOf2BenchmarkType.LeftBitShifting => LeftBitShifting,
RoundUpToPowerOf2BenchmarkType.BinarySearch => BinarySearch,
RoundUpToPowerOf2BenchmarkType.IsPowOf2 => IsPowOf2,
RoundUpToPowerOf2BenchmarkType.IsPowOf2_GreaterThanFirst => IsPowOf2_GreaterThanFirst,
RoundUpToPowerOf2BenchmarkType.IsPowOf2_WithIf => IsPowOf2_WithIf,
RoundUpToPowerOf2BenchmarkType.IsPowOf2_GreaterEqual => IsPowOf2_GreaterEqual,
_ => throw CreateUnknownBenchmarkTypeException(benchmarkType),
};
protected override void PrepareData<T>(T benchmarkType)
{
}
}
using BenchmarkDotNet.Attributes;
namespace Recyclable.Collections.Benchmarks.POC
{
static class RoundUpToPowerOf2Alternatives
{
private static readonly int[] PowersOfTwo =
{
1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576,
2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824
};
public static int BinarySearch(int value)
{
if (value <= 0)
{
throw new ArgumentException("Input must be a positive integer.");
}
// Find the smallest power of 2 greater than or equal to value using a lookup table
int index = Array.BinarySearch(PowersOfTwo, value);
if (index < 0)
{
// If value is not a power of 2, use the next power of 2 from the table
index = ~index;
}
return PowersOfTwo[index];
}
public static int RightBitShifting(int value)
{
// Decrease value by 1 to handle the case where the input is already a power of 2
value--;
value |= value >> 1;
value |= value >> 2;
value |= value >> 4;
value |= value >> 8;
value |= value >> 16;
return value + 1;
}
public static int LeftBitShifting(int value)
{
if (value == 0)
{
return 1;
}
// Ensure value is a power of 2 or the next power of 2
int result = 1;
while (result < value)
{
result = checked(result << 1);
}
return result;
}
}
public enum RoundUpToPowerOf2BenchmarkType
{
Undefined,
BitOperations,
RightBitShifting,
LeftBitShifting,
BinarySearch,
IsPowOf2,
IsPowOf2_GreaterThanFirst,
IsPowOf2_WithIf,
IsPowOf2_GreaterEqual,
}
public class RoundUpToPowerOf2vsOtherBenchmarks : PocBenchmarkBase<RoundUpToPowerOf2BenchmarkType>
{
// [Params(-850_000, 850_000)]
[Params(-int.MaxValue, int.MaxValue)]
public override int TestObjectCount { get => base.TestObjectCount; set => base.TestObjectCount = value; }
[Params(RoundUpToPowerOf2BenchmarkType.IsPowOf2)]
public override RoundUpToPowerOf2BenchmarkType BaseDataType { get => base.BaseDataType; set => base.BaseDataType = value; }
[Params(RoundUpToPowerOf2BenchmarkType.IsPowOf2_GreaterThanFirst, RoundUpToPowerOf2BenchmarkType.IsPowOf2_WithIf, RoundUpToPowerOf2BenchmarkType.IsPowOf2_GreaterEqual)]
// [Params(RoundUpToPowerOf2BenchmarkType.RightBitShifting, RoundUpToPowerOf2BenchmarkType.LeftBitShifting, RoundUpToPowerOf2BenchmarkType.BinarySearch)]
public override RoundUpToPowerOf2BenchmarkType DataType { get => base.DataType; set => base.DataType = value; }
private void BitOperations()
{
DoNothing(System.Numerics.BitOperations.RoundUpToPowerOf2((uint)base.TestObjectCount));
}
private void RightBitShifting()
{
DoNothing(RoundUpToPowerOf2Alternatives.RightBitShifting(TestObjectCount));
}
private void LeftBitShifting()
{
DoNothing(RoundUpToPowerOf2Alternatives.LeftBitShifting(TestObjectCount));
}
private void BinarySearch()
{
DoNothing(RoundUpToPowerOf2Alternatives.BinarySearch(TestObjectCount));
}
private void IsPowOf2()
{
DoNothing((TestObjectCount & (TestObjectCount - 1)) == 0 && TestObjectCount > 0);
}
private void IsPowOf2_GreaterThanFirst()
{
DoNothing(TestObjectCount > 0 && (TestObjectCount & (TestObjectCount - 1)) == 0);
}
private void IsPowOf2_WithIf()
{
DoNothing(TestObjectCount > 0 ? (TestObjectCount & (TestObjectCount - 1)) == 0 : false);
}
private void IsPowOf2_GreaterEqual()
{
DoNothing(TestObjectCount >= 0 && TestObjectCount != 0 && (TestObjectCount & (TestObjectCount - 1)) == 0);
}
protected override Action? GetTestMethod(RoundUpToPowerOf2BenchmarkType benchmarkType) => benchmarkType switch
{
RoundUpToPowerOf2BenchmarkType.BitOperations => BitOperations,
RoundUpToPowerOf2BenchmarkType.RightBitShifting => RightBitShifting,
RoundUpToPowerOf2BenchmarkType.LeftBitShifting => LeftBitShifting,
RoundUpToPowerOf2BenchmarkType.BinarySearch => BinarySearch,
RoundUpToPowerOf2BenchmarkType.IsPowOf2 => IsPowOf2,
RoundUpToPowerOf2BenchmarkType.IsPowOf2_GreaterThanFirst => IsPowOf2_GreaterThanFirst,
RoundUpToPowerOf2BenchmarkType.IsPowOf2_WithIf => IsPowOf2_WithIf,
RoundUpToPowerOf2BenchmarkType.IsPowOf2_GreaterEqual => IsPowOf2_GreaterEqual,
_ => throw CreateUnknownBenchmarkTypeException(benchmarkType),
};
protected override void PrepareData<T>(T benchmarkType)
{
}
}
}
@@ -1,64 +1,64 @@
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
namespace Recyclable.Collections.Benchmarks.POC
{
public enum SpanConstructorVsTypecastingPocBenchmarkType
{
Unknown,
Constructor,
TypeCasting,
}
[MemoryDiagnoser]
public class SpanConstructorVsTypecastingPocBenchmarks : PocBenchmarkBase<SpanConstructorVsTypecastingPocBenchmarkType>
{
private int[]? TestData { get; set; }
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
private void DoNothing(ReadOnlySpan<int> span) { }
[Params(SpanConstructorVsTypecastingPocBenchmarkType.TypeCasting)]
public override SpanConstructorVsTypecastingPocBenchmarkType BaseDataType { get => base.BaseDataType; set => base.BaseDataType = value; }
[Params(SpanConstructorVsTypecastingPocBenchmarkType.Constructor)]
public override SpanConstructorVsTypecastingPocBenchmarkType DataType { get => base.DataType; set => base.DataType = value; }
[Params(8)]
public override int TestObjectCount { get => base.TestObjectCount; set => base.TestObjectCount = value; }
public static void Run()
{
var benchmark = new SpanConstructorVsTypecastingPocBenchmarks();
benchmark.Setup();
benchmark.Constructor();
benchmark.TypeCasting();
benchmark.Cleanup();
}
public void Constructor()
{
ReadOnlySpan<int> span = new(TestData);
DoNothing(span);
}
public void TypeCasting()
{
ReadOnlySpan<int> span = TestData;
DoNothing(span);
}
public override void Setup()
{
TestData = new int[TestObjectCount];
base.Setup();
}
protected override Action? GetTestMethod(SpanConstructorVsTypecastingPocBenchmarkType benchmarkType) => benchmarkType switch
{
SpanConstructorVsTypecastingPocBenchmarkType.Constructor => Constructor,
SpanConstructorVsTypecastingPocBenchmarkType.TypeCasting => TypeCasting,
_ => throw CreateUnknownBenchmarkTypeException(benchmarkType),
};
}
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
namespace Recyclable.Collections.Benchmarks.POC
{
public enum SpanConstructorVsTypecastingPocBenchmarkType
{
Unknown,
Constructor,
TypeCasting,
}
[MemoryDiagnoser]
public class SpanConstructorVsTypecastingPocBenchmarks : PocBenchmarkBase<SpanConstructorVsTypecastingPocBenchmarkType>
{
private int[]? TestData { get; set; }
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
private void DoNothing(ReadOnlySpan<int> span) { }
[Params(SpanConstructorVsTypecastingPocBenchmarkType.TypeCasting)]
public override SpanConstructorVsTypecastingPocBenchmarkType BaseDataType { get => base.BaseDataType; set => base.BaseDataType = value; }
[Params(SpanConstructorVsTypecastingPocBenchmarkType.Constructor)]
public override SpanConstructorVsTypecastingPocBenchmarkType DataType { get => base.DataType; set => base.DataType = value; }
[Params(8)]
public override int TestObjectCount { get => base.TestObjectCount; set => base.TestObjectCount = value; }
public static void Run()
{
var benchmark = new SpanConstructorVsTypecastingPocBenchmarks();
benchmark.Setup();
benchmark.Constructor();
benchmark.TypeCasting();
benchmark.Cleanup();
}
public void Constructor()
{
ReadOnlySpan<int> span = new(TestData);
DoNothing(span);
}
public void TypeCasting()
{
ReadOnlySpan<int> span = TestData;
DoNothing(span);
}
public override void Setup()
{
TestData = new int[TestObjectCount];
base.Setup();
}
protected override Action? GetTestMethod(SpanConstructorVsTypecastingPocBenchmarkType benchmarkType) => benchmarkType switch
{
SpanConstructorVsTypecastingPocBenchmarkType.Constructor => Constructor,
SpanConstructorVsTypecastingPocBenchmarkType.TypeCasting => TypeCasting,
_ => throw CreateUnknownBenchmarkTypeException(benchmarkType),
};
}
}
@@ -1,99 +1,99 @@
using BenchmarkDotNet.Attributes;
namespace Recyclable.Collections.Benchmarks.POC
{
public enum SpanVsArrayBenchmarkType
{
ArrayGetWithVar,
ArrayGetWithField,
SpanGet,
ArraySetWithVar,
ArraySetWithField,
SpanSet
}
public class SpanVsArrayBenchmarks : RecyclableBenchmarkBase<SpanVsArrayBenchmarkType>
{
// [Params(SpanVsArrayBenchmarkType.ArrayGetWithVar, SpanVsArrayBenchmarkType.SpanGet)]
// [Params(SpanVsArrayBenchmarkType.ArraySetWithVar, SpanVsArrayBenchmarkType.SpanSet)]
[Params
(
SpanVsArrayBenchmarkType.ArrayGetWithVar, SpanVsArrayBenchmarkType.SpanGet,
SpanVsArrayBenchmarkType.ArraySetWithField, SpanVsArrayBenchmarkType.ArraySetWithVar, SpanVsArrayBenchmarkType.SpanSet
)]
public override SpanVsArrayBenchmarkType DataType { get => base.DataType; set => base.DataType = value; }
[Params(SpanVsArrayBenchmarkType.ArrayGetWithField)]
// [Params(SpanVsArrayBenchmarkType.ArraySetWithField)]
public override SpanVsArrayBenchmarkType BaseDataType { get => base.BaseDataType; set => base.BaseDataType = value; }
protected override Action GetTestMethod(SpanVsArrayBenchmarkType benchmarkType) => benchmarkType switch
{
SpanVsArrayBenchmarkType.ArrayGetWithVar => ArrayGetWithVar,
SpanVsArrayBenchmarkType.ArrayGetWithField => ArrayGetWithField,
SpanVsArrayBenchmarkType.SpanGet => SpanGet,
SpanVsArrayBenchmarkType.ArraySetWithVar => ArraySetWithVar,
SpanVsArrayBenchmarkType.ArraySetWithField => ArraySetWithField,
SpanVsArrayBenchmarkType.SpanSet => SpanSet,
_ => throw CreateUnknownBenchmarkTypeException(benchmarkType),
};
public void ArrayGetWithVar()
{
var data = TestObjects;
var dataCount = TestObjectCount;
for (long i = 0; i < dataCount; i++)
{
DoNothing(data[i]);
}
}
public void ArrayGetWithField()
{
var dataCount = TestObjectCount;
for (long i = 0; i < dataCount; i++)
{
DoNothing(_testObjects![i]);
}
}
public void SpanGet()
{
var dataCount = TestObjectCount;
Span<long> data = new(_testObjects, 0, dataCount);
for (int i = 0; i < dataCount; i++)
{
DoNothing(data[i]);
}
}
public void ArraySetWithVar()
{
var data = TestObjects;
var dataCount = TestObjectCount;
for (long i = 0; i < dataCount; i++)
{
data[i] = i;
}
}
public void ArraySetWithField()
{
var dataCount = TestObjectCount;
for (long i = 0; i < dataCount; i++)
{
_testObjects![i] = i;
}
}
public void SpanSet()
{
var dataCount = TestObjectCount;
Span<long> data = new(_testObjects, 0, dataCount);
for (int i = 0; i < dataCount; i++)
{
data[i] = i;
}
}
}
}
using BenchmarkDotNet.Attributes;
namespace Recyclable.Collections.Benchmarks.POC
{
public enum SpanVsArrayBenchmarkType
{
ArrayGetWithVar,
ArrayGetWithField,
SpanGet,
ArraySetWithVar,
ArraySetWithField,
SpanSet
}
public class SpanVsArrayBenchmarks : RecyclableBenchmarkBase<SpanVsArrayBenchmarkType>
{
// [Params(SpanVsArrayBenchmarkType.ArrayGetWithVar, SpanVsArrayBenchmarkType.SpanGet)]
// [Params(SpanVsArrayBenchmarkType.ArraySetWithVar, SpanVsArrayBenchmarkType.SpanSet)]
[Params
(
SpanVsArrayBenchmarkType.ArrayGetWithVar, SpanVsArrayBenchmarkType.SpanGet,
SpanVsArrayBenchmarkType.ArraySetWithField, SpanVsArrayBenchmarkType.ArraySetWithVar, SpanVsArrayBenchmarkType.SpanSet
)]
public override SpanVsArrayBenchmarkType DataType { get => base.DataType; set => base.DataType = value; }
[Params(SpanVsArrayBenchmarkType.ArrayGetWithField)]
// [Params(SpanVsArrayBenchmarkType.ArraySetWithField)]
public override SpanVsArrayBenchmarkType BaseDataType { get => base.BaseDataType; set => base.BaseDataType = value; }
protected override Action GetTestMethod(SpanVsArrayBenchmarkType benchmarkType) => benchmarkType switch
{
SpanVsArrayBenchmarkType.ArrayGetWithVar => ArrayGetWithVar,
SpanVsArrayBenchmarkType.ArrayGetWithField => ArrayGetWithField,
SpanVsArrayBenchmarkType.SpanGet => SpanGet,
SpanVsArrayBenchmarkType.ArraySetWithVar => ArraySetWithVar,
SpanVsArrayBenchmarkType.ArraySetWithField => ArraySetWithField,
SpanVsArrayBenchmarkType.SpanSet => SpanSet,
_ => throw CreateUnknownBenchmarkTypeException(benchmarkType),
};
public void ArrayGetWithVar()
{
var data = TestObjects;
var dataCount = TestObjectCount;
for (long i = 0; i < dataCount; i++)
{
DoNothing(data[i]);
}
}
public void ArrayGetWithField()
{
var dataCount = TestObjectCount;
for (long i = 0; i < dataCount; i++)
{
DoNothing(_testObjects![i]);
}
}
public void SpanGet()
{
var dataCount = TestObjectCount;
Span<long> data = new(_testObjects, 0, dataCount);
for (int i = 0; i < dataCount; i++)
{
DoNothing(data[i]);
}
}
public void ArraySetWithVar()
{
var data = TestObjects;
var dataCount = TestObjectCount;
for (long i = 0; i < dataCount; i++)
{
data[i] = i;
}
}
public void ArraySetWithField()
{
var dataCount = TestObjectCount;
for (long i = 0; i < dataCount; i++)
{
_testObjects![i] = i;
}
}
public void SpanSet()
{
var dataCount = TestObjectCount;
Span<long> data = new(_testObjects, 0, dataCount);
for (int i = 0; i < dataCount; i++)
{
data[i] = i;
}
}
}
}
File diff suppressed because it is too large. Load diff
@@ -1,63 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Threading.Tasks.Sources;
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.ObjectPool;
namespace Recyclable.Collections.Benchmarks.POC
{
[MemoryDiagnoser]
public class TaskRunVsTaskFactoryStartNewBenchmarks
{
public int NumberOfTasks = 100;
private static readonly TaskFactory TaskPool = new();
[Benchmark]
public void OwnTaskFactory()
{
for (var i = 0; i < NumberOfTasks; i++)
{
_ = TaskPool.StartNew(() => {});
}
}
[Benchmark]
public void TaskRun()
{
for (var i = 0; i < NumberOfTasks; i++)
{
_ = Task.Run(() => { });
}
}
[Benchmark(Baseline = true)]
public void TaskFactoryStartNew()
{
for (var i = 0; i < NumberOfTasks; i++)
{
_ = Task.Factory.StartNew(() => { });
}
}
[Benchmark]
public void TaskConstructor()
{
for (var i = 0; i < NumberOfTasks; i++)
{
new Task(() => { }).Start();
}
}
[Benchmark]
public void TaskConstructorWithKnownScheduler()
{
TaskScheduler scheduler = TaskScheduler.Current;
for (var i = 0; i < NumberOfTasks; i++)
{
new Task(() => { }).Start(scheduler);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Threading.Tasks.Sources;
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.ObjectPool;
namespace Recyclable.Collections.Benchmarks.POC
{
[MemoryDiagnoser]
public class TaskRunVsTaskFactoryStartNewBenchmarks
{
public int NumberOfTasks = 100;
private static readonly TaskFactory TaskPool = new();
[Benchmark]
public void OwnTaskFactory()
{
for (var i = 0; i < NumberOfTasks; i++)
{
_ = TaskPool.StartNew(() => {});
}
}
[Benchmark]
public void TaskRun()
{
for (var i = 0; i < NumberOfTasks; i++)
{
_ = Task.Run(() => { });
}
}
[Benchmark(Baseline = true)]
public void TaskFactoryStartNew()
{
for (var i = 0; i < NumberOfTasks; i++)
{
_ = Task.Factory.StartNew(() => { });
}
}
[Benchmark]
public void TaskConstructor()
{
for (var i = 0; i < NumberOfTasks; i++)
{
new Task(() => { }).Start();
}
}
[Benchmark]
public void TaskConstructorWithKnownScheduler()
{
TaskScheduler scheduler = TaskScheduler.Current;
for (var i = 0; i < NumberOfTasks; i++)
{
new Task(() => { }).Start(scheduler);
}
}
}
}
+65 -65
View File
@@ -1,65 +1,65 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
using Recyclable.Collections.Benchmarks.POC;
#pragma warning disable RCS1036, RCS1213, IDE0051
namespace Recyclable.Collections.Benchmarks
{
public static class Program
{
static IConfig BenchmarkConfig { get; } = ManualConfig.Create(DefaultConfig.Instance)
.WithOptions(ConfigOptions.DisableOptimizationsValidator | ConfigOptions.JoinSummary);
static void RunAssemblyBenchmarks()
{
_ = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(new[] { "-f*" });
}
private static void RunRecyclableCollectionsBenchmarks()
{
_ = BenchmarkRunner.Run<RecyclableCollectionsBenchmarks>(BenchmarkConfig);
}
static void RunSelectedBenchmarks()
{
// ****************
// *** Template ***
// ****************
var benchmark = new RecyclableCollectionsBenchmarks();
benchmark.Setup();
foreach (var _ in Enumerable.Range(1, 1000))
{
// benchmark.RecyclableList_Create_WithCapacity();
}
benchmark.Cleanup();
}
static void RunPocBenchmarks()
{
_ = BenchmarkRunner.Run<BoolOrComparePocBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<DelegateVsComparerPocBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<ModuloPocBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<TaskRunVsTaskFactoryStartNewBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<RefVsInstanceMemberPocBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<RoundBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<SpanVsArrayBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<LessOperatorVsAndOperatorBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<EqualVsContainsInConsecutiveOrderBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<RoundUpToPowerOf2vsOtherBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<GreaterOperatorVsDoesntEqualOperatorBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<SpanConstructorVsTypecastingPocBenchmarks>(BenchmarkConfig);
// ArraySizeLimitPocBenchmarks>(BenchmarkConfig);
}
static void Main()
{
RunRecyclableCollectionsBenchmarks();
// RunPocBenchmarks();
// RunSelectedBenchmarks();
// RunAssemblyBenchmarks();
}
}
}
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
using Recyclable.Collections.Benchmarks.POC;
#pragma warning disable RCS1036, RCS1213, IDE0051
namespace Recyclable.Collections.Benchmarks
{
public static class Program
{
static IConfig BenchmarkConfig { get; } = ManualConfig.Create(DefaultConfig.Instance)
.WithOptions(ConfigOptions.DisableOptimizationsValidator | ConfigOptions.JoinSummary);
static void RunAssemblyBenchmarks()
{
_ = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(new[] { "-f*" });
}
private static void RunRecyclableCollectionsBenchmarks()
{
_ = BenchmarkRunner.Run<RecyclableCollectionsBenchmarks>(BenchmarkConfig);
}
static void RunSelectedBenchmarks()
{
// ****************
// *** Template ***
// ****************
var benchmark = new RecyclableCollectionsBenchmarks();
benchmark.Setup();
foreach (var _ in Enumerable.Range(1, 1000))
{
// benchmark.RecyclableList_Create_WithCapacity();
}
benchmark.Cleanup();
}
static void RunPocBenchmarks()
{
_ = BenchmarkRunner.Run<BoolOrComparePocBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<DelegateVsComparerPocBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<ModuloPocBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<TaskRunVsTaskFactoryStartNewBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<RefVsInstanceMemberPocBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<RoundBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<SpanVsArrayBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<LessOperatorVsAndOperatorBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<EqualVsContainsInConsecutiveOrderBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<RoundUpToPowerOf2vsOtherBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<GreaterOperatorVsDoesntEqualOperatorBenchmarks>(BenchmarkConfig);
_ = BenchmarkRunner.Run<SpanConstructorVsTypecastingPocBenchmarks>(BenchmarkConfig);
// ArraySizeLimitPocBenchmarks>(BenchmarkConfig);
}
static void Main()
{
RunRecyclableCollectionsBenchmarks();
// RunPocBenchmarks();
// RunSelectedBenchmarks();
// RunAssemblyBenchmarks();
}
}
}
@@ -1,12 +1,12 @@
{
"profiles": {
"WSL": {
"commandName": "WSL2",
"distributionName": ""
},
".Net": {
"commandName": "Project",
"nativeDebugging": true
}
}
{
"profiles": {
"WSL": {
"commandName": "WSL2",
"distributionName": ""
},
".Net": {
"commandName": "Project",
"nativeDebugging": true
}
}
}
@@ -1,22 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x64</Platforms>
<StartupObject>Recyclable.Collections.Benchmarks.Program</StartupObject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
<PackageReference Include="Collections.Pooled" Version="1.0.82" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Recyclable.CollectionsTests\Recyclable.CollectionsTests.csproj" />
<ProjectReference Include="..\Recyclable.Collections\Recyclable.Collections.csproj" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net8.0;net7.0;net6.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x64</Platforms>
<StartupObject>Recyclable.Collections.Benchmarks.Program</StartupObject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
<PackageReference Include="Collections.Pooled" Version="1.0.82" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Recyclable.CollectionsTests\Recyclable.CollectionsTests.csproj" />
<ProjectReference Include="..\Recyclable.Collections\Recyclable.Collections.csproj" />
</ItemGroup>
</Project>
@@ -1,340 +1,340 @@
using BenchmarkDotNet.Attributes;
using Collections.Pooled;
using Recyclable.Collections.Benchmarks.Core;
using Recyclable.Collections.Benchmarks.POC;
namespace Recyclable.Collections.Benchmarks
{
public enum RecyclableCollectionsBenchmarkSource
{
Unknown,
Array,
List,
PooledList,
RecyclableList,
RecyclableLongList,
}
[MemoryDiagnoser]
public class RecyclableBenchmarkBase<TBenchmarkType> : PocBenchmarkBase<TBenchmarkType>
{
protected long[]? _testObjects;
protected long[] TestObjects => _testObjects ?? throw new NullReferenceException("Something is wrong and the field is not initialized");
protected List<long>? _testObjectsAsList;
protected List<long> TestObjectsAsList => _testObjectsAsList ?? throw new NullReferenceException("Something went wrong and the field is not initialized");
protected PooledList<long>? _testObjectsAsPooledList;
public PooledList<long> TestObjectsAsPooledList => _testObjectsAsPooledList ?? throw new NullReferenceException("Something is wrong and the field is not initialized");
protected RecyclableList<long>? _testObjectsAsRecyclableList;
protected RecyclableList<long> TestObjectsAsRecyclableList => _testObjectsAsRecyclableList ?? throw new NullReferenceException("Something is wrong and the field is not initialized");
protected RecyclableLongList<long>? _testObjectsAsRecyclableLongList;
protected RecyclableLongList<long> TestObjectsAsRecyclableLongList => _testObjectsAsRecyclableLongList ?? throw new NullReferenceException("Something is wrong and the field is not initialized");
protected IEnumerable<long> TestObjectsAsIEnumerable
{
get
{
for (long i = 0; i < TestObjectCount; i++)
{
yield return i;
}
}
}
public override void Setup()
{
PrepareData(RecyclableCollectionsBenchmarkSource.Array);
base.Setup();
}
protected override void PrepareData<T>(T benchmarkType)
{
Console.WriteLine($"******* PREPARING DATA FOR {benchmarkType} *******");
switch (benchmarkType)
{
case RecyclableCollectionsBenchmarkSource.Unknown:
break;
case RecyclableCollectionsBenchmarkSource.Array:
_testObjects ??= DataGenerator.EnumerateTestObjects(TestObjectCount);
break;
case RecyclableCollectionsBenchmarkSource.List:
_testObjectsAsList ??= TestObjects.ToList();
break;
case RecyclableCollectionsBenchmarkSource.PooledList:
_testObjectsAsPooledList ??= new PooledList<long>(TestObjects, ClearMode.Auto);
break;
case RecyclableCollectionsBenchmarkSource.RecyclableList:
_testObjectsAsRecyclableList ??= new(TestObjects, initialCapacity: TestObjectCount);
break;
case RecyclableCollectionsBenchmarkSource.RecyclableLongList:
_testObjectsAsRecyclableLongList ??= new(TestObjects, BlockSize, initialCapacity: base.TestObjectCount);
break;
default:
throw CreateUnknownBenchmarkTypeException(benchmarkType);
}
base.PrepareData(benchmarkType);
}
public override void Cleanup()
{
Console.WriteLine("******* GLOBAL CLEANUP *******");
// Enable this if item type is changed to reference type
//if (_testObjects != null)
//{
// Array.Fill(_testObjects, 0);
//}
_testObjects = default;
_testObjectsAsList = default;
_testObjectsAsPooledList?.Dispose();
_testObjectsAsPooledList = default;
_testObjectsAsRecyclableList?.Dispose();
_testObjectsAsRecyclableList = default;
_testObjectsAsRecyclableLongList?.Dispose();
_testObjectsAsRecyclableLongList = default;
base.Cleanup();
}
}
//[MemoryDiagnoser]
//public class ListVsLinkedListBenchmarks
//{
// [Benchmark]
// public void RecyclableList()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// using var list = new RecyclableList<object>(initialCapacity: dataCount);
// for (long i = 0; i < dataCount; i++)
// {
// list.Add(data[i]);
// }
// for (long i = 0; i < list.LongCount; i++)
// {
// DoNothing(list[i]);
// }
// list.Clear();
// }
// [Benchmark(Baseline = true)]
// public void RecyclableLongList()
// {
// var data = TestObjects;
// long dataCount = TestObjectCount;
// using var list = new RecyclableLongList<object>(BlockSize, totalItemsCount: dataCount);
// for (long i = 0; i < dataCount; i++)
// {
// list.Add(data[i]);
// }
// for (long i = 0; i < list.LongCount; i++)
// {
// DoNothing(list[i]);
// }
// list.Clear();
// }
// //[Benchmark]
// public void List()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new List<object>(capacity: dataCount);
// for (var i = 0; i < dataCount; i++)
// {
// list.Add(data[i]);
// }
// for (var i = 0; i < list.Count; i++)
// {
// DoNothing(list[i]);
// }
// list.Clear();
// }
// //[Benchmark]
// public void LinkedList()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new LinkedList<object>();
// for (var i = 0; i < dataCount; i++)
// {
// _ = list.AddLast(data[i]);
// }
// foreach (var node in list)
// {
// DoNothing(node);
// }
// list.Clear();
// }
// //[Benchmark]
// public void RecyclableQueue()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new RecyclableQueue<object>(BlockSize);
// for (var i = 0; i < dataCount; i++)
// {
// list.Enqueue(data[i]);
// }
// while (list.LongCount > 0)
// {
// DoNothing(list.Dequeue());
// }
// list.Clear();
// }
// //[Benchmark]
// public void Queue()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new Queue<object>(capacity: dataCount);
// for (var i = 0; i < dataCount; i++)
// {
// list.Enqueue(data[i]);
// }
// while (list.Count > 0)
// {
// DoNothing(list.Dequeue());
// }
// list.Clear();
// }
// //[Benchmark]
// public void PriorityQueue()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new PriorityQueue<object, long>(initialCapacity: dataCount);
// for (var i = 0; i < dataCount; i++)
// {
// list.Enqueue(data[i], i);
// }
// while (list.Count > 0)
// {
// DoNothing(list.Dequeue());
// }
// list.Clear();
// }
// //[Benchmark]
// public void RecyclableSortedList_InUpdateMode()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new RecyclableSortedList<long, object>(BlockSize);
// list.BeginUpdate();
// for (var i = 0; i < dataCount; i++)
// {
// list.Add(i, data[i]);
// }
// list.EndUpdate();
// for (var i = 0; i < list.LongCount; i++)
// {
// DoNothing(list[i]);
// }
// list.Clear();
// }
// //[Benchmark]
// public void RecyclableSortedList()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new RecyclableSortedList<long, object>(BlockSize);
// for (var i = 0; i < dataCount; i++)
// {
// list.Add(i, data[i]);
// }
// for (var i = 0; i < list.LongCount; i++)
// {
// DoNothing(list[i]);
// }
// list.Clear();
// }
// //[Benchmark]
// public void SortedList()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new SortedList<long, object>();
// for (var i = 0; i < dataCount; i++)
// {
// list.Add(i, data[i]);
// }
// for (var i = 0; i < list.Count; i++)
// {
// DoNothing(list[i]);
// }
// list.Clear();
// }
// //[Benchmark]
// public void RecyclableStack()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new RecyclableStack<object>(BlockSize);
// for (var i = 0; i < dataCount; i++)
// {
// list.Push(data[i]);
// }
// while (list.LongCount > 0)
// {
// DoNothing(list.Pop());
// }
// list.Clear();
// }
// //[Benchmark]
// public void Stack()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new Stack<object>();
// for (var i = 0; i < dataCount; i++)
// {
// list.Push(data[i]);
// }
// while (list.Count > 0)
// {
// DoNothing(list.Pop());
// }
// list.Clear();
// }
//}
}
using BenchmarkDotNet.Attributes;
using Collections.Pooled;
using Recyclable.Collections.Benchmarks.Core;
using Recyclable.Collections.Benchmarks.POC;
namespace Recyclable.Collections.Benchmarks
{
public enum RecyclableCollectionsBenchmarkSource
{
Unknown,
Array,
List,
PooledList,
RecyclableList,
RecyclableLongList,
}
[MemoryDiagnoser]
public class RecyclableBenchmarkBase<TBenchmarkType> : PocBenchmarkBase<TBenchmarkType>
{
protected long[]? _testObjects;
protected long[] TestObjects => _testObjects ?? throw new NullReferenceException("Something is wrong and the field is not initialized");
protected List<long>? _testObjectsAsList;
protected List<long> TestObjectsAsList => _testObjectsAsList ?? throw new NullReferenceException("Something went wrong and the field is not initialized");
protected PooledList<long>? _testObjectsAsPooledList;
public PooledList<long> TestObjectsAsPooledList => _testObjectsAsPooledList ?? throw new NullReferenceException("Something is wrong and the field is not initialized");
protected RecyclableList<long>? _testObjectsAsRecyclableList;
protected RecyclableList<long> TestObjectsAsRecyclableList => _testObjectsAsRecyclableList ?? throw new NullReferenceException("Something is wrong and the field is not initialized");
protected RecyclableLongList<long>? _testObjectsAsRecyclableLongList;
protected RecyclableLongList<long> TestObjectsAsRecyclableLongList => _testObjectsAsRecyclableLongList ?? throw new NullReferenceException("Something is wrong and the field is not initialized");
protected IEnumerable<long> TestObjectsAsIEnumerable
{
get
{
for (long i = 0; i < TestObjectCount; i++)
{
yield return i;
}
}
}
public override void Setup()
{
PrepareData(RecyclableCollectionsBenchmarkSource.Array);
base.Setup();
}
protected override void PrepareData<T>(T benchmarkType)
{
Console.WriteLine($"******* PREPARING DATA FOR {benchmarkType} *******");
switch (benchmarkType)
{
case RecyclableCollectionsBenchmarkSource.Unknown:
break;
case RecyclableCollectionsBenchmarkSource.Array:
_testObjects ??= DataGenerator.EnumerateTestObjects(TestObjectCount);
break;
case RecyclableCollectionsBenchmarkSource.List:
_testObjectsAsList ??= TestObjects.ToList();
break;
case RecyclableCollectionsBenchmarkSource.PooledList:
_testObjectsAsPooledList ??= new PooledList<long>(TestObjects, ClearMode.Auto);
break;
case RecyclableCollectionsBenchmarkSource.RecyclableList:
_testObjectsAsRecyclableList ??= new(TestObjects, initialCapacity: TestObjectCount);
break;
case RecyclableCollectionsBenchmarkSource.RecyclableLongList:
_testObjectsAsRecyclableLongList ??= new(TestObjects, BlockSize, initialCapacity: base.TestObjectCount);
break;
default:
throw CreateUnknownBenchmarkTypeException(benchmarkType);
}
base.PrepareData(benchmarkType);
}
public override void Cleanup()
{
Console.WriteLine("******* GLOBAL CLEANUP *******");
// Enable this if item type is changed to reference type
//if (_testObjects != null)
//{
// Array.Fill(_testObjects, 0);
//}
_testObjects = default;
_testObjectsAsList = default;
_testObjectsAsPooledList?.Dispose();
_testObjectsAsPooledList = default;
_testObjectsAsRecyclableList?.Dispose();
_testObjectsAsRecyclableList = default;
_testObjectsAsRecyclableLongList?.Dispose();
_testObjectsAsRecyclableLongList = default;
base.Cleanup();
}
}
//[MemoryDiagnoser]
//public class ListVsLinkedListBenchmarks
//{
// [Benchmark]
// public void RecyclableList()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// using var list = new RecyclableList<object>(initialCapacity: dataCount);
// for (long i = 0; i < dataCount; i++)
// {
// list.Add(data[i]);
// }
// for (long i = 0; i < list.LongCount; i++)
// {
// DoNothing(list[i]);
// }
// list.Clear();
// }
// [Benchmark(Baseline = true)]
// public void RecyclableLongList()
// {
// var data = TestObjects;
// long dataCount = TestObjectCount;
// using var list = new RecyclableLongList<object>(BlockSize, totalItemsCount: dataCount);
// for (long i = 0; i < dataCount; i++)
// {
// list.Add(data[i]);
// }
// for (long i = 0; i < list.LongCount; i++)
// {
// DoNothing(list[i]);
// }
// list.Clear();
// }
// //[Benchmark]
// public void List()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new List<object>(capacity: dataCount);
// for (var i = 0; i < dataCount; i++)
// {
// list.Add(data[i]);
// }
// for (var i = 0; i < list.Count; i++)
// {
// DoNothing(list[i]);
// }
// list.Clear();
// }
// //[Benchmark]
// public void LinkedList()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new LinkedList<object>();
// for (var i = 0; i < dataCount; i++)
// {
// _ = list.AddLast(data[i]);
// }
// foreach (var node in list)
// {
// DoNothing(node);
// }
// list.Clear();
// }
// //[Benchmark]
// public void RecyclableQueue()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new RecyclableQueue<object>(BlockSize);
// for (var i = 0; i < dataCount; i++)
// {
// list.Enqueue(data[i]);
// }
// while (list.LongCount > 0)
// {
// DoNothing(list.Dequeue());
// }
// list.Clear();
// }
// //[Benchmark]
// public void Queue()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new Queue<object>(capacity: dataCount);
// for (var i = 0; i < dataCount; i++)
// {
// list.Enqueue(data[i]);
// }
// while (list.Count > 0)
// {
// DoNothing(list.Dequeue());
// }
// list.Clear();
// }
// //[Benchmark]
// public void PriorityQueue()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new PriorityQueue<object, long>(initialCapacity: dataCount);
// for (var i = 0; i < dataCount; i++)
// {
// list.Enqueue(data[i], i);
// }
// while (list.Count > 0)
// {
// DoNothing(list.Dequeue());
// }
// list.Clear();
// }
// //[Benchmark]
// public void RecyclableSortedList_InUpdateMode()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new RecyclableSortedList<long, object>(BlockSize);
// list.BeginUpdate();
// for (var i = 0; i < dataCount; i++)
// {
// list.Add(i, data[i]);
// }
// list.EndUpdate();
// for (var i = 0; i < list.LongCount; i++)
// {
// DoNothing(list[i]);
// }
// list.Clear();
// }
// //[Benchmark]
// public void RecyclableSortedList()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new RecyclableSortedList<long, object>(BlockSize);
// for (var i = 0; i < dataCount; i++)
// {
// list.Add(i, data[i]);
// }
// for (var i = 0; i < list.LongCount; i++)
// {
// DoNothing(list[i]);
// }
// list.Clear();
// }
// //[Benchmark]
// public void SortedList()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new SortedList<long, object>();
// for (var i = 0; i < dataCount; i++)
// {
// list.Add(i, data[i]);
// }
// for (var i = 0; i < list.Count; i++)
// {
// DoNothing(list[i]);
// }
// list.Clear();
// }
// //[Benchmark]
// public void RecyclableStack()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new RecyclableStack<object>(BlockSize);
// for (var i = 0; i < dataCount; i++)
// {
// list.Push(data[i]);
// }
// while (list.LongCount > 0)
// {
// DoNothing(list.Pop());
// }
// list.Clear();
// }
// //[Benchmark]
// public void Stack()
// {
// var data = TestObjects;
// var dataCount = TestObjectCount;
// var list = new Stack<object>();
// for (var i = 0; i < dataCount; i++)
// {
// list.Push(data[i]);
// }
// while (list.Count > 0)
// {
// DoNothing(list.Pop());
// }
// list.Clear();
// }
//}
}
File diff suppressed because it is too large. Load diff
+51 -51
View File
@@ -1,51 +1,51 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33205.214
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Recyclable.Collections", "Recyclable.Collections\Recyclable.Collections.csproj", "{7EC0F96D-FB4C-418A-9172-612CC5BCE705}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Recyclable.CollectionsTests", "Recyclable.CollectionsTests\Recyclable.CollectionsTests.csproj", "{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Recyclable.Collections.Benchmarks", "Recyclable.Collections.Benchmarks\Recyclable.Collections.Benchmarks.csproj", "{CDC0CF78-FC11-43E3-9059-3261DA3639D1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7EC0F96D-FB4C-418A-9172-612CC5BCE705}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7EC0F96D-FB4C-418A-9172-612CC5BCE705}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EC0F96D-FB4C-418A-9172-612CC5BCE705}.Debug|x64.ActiveCfg = Debug|x64
{7EC0F96D-FB4C-418A-9172-612CC5BCE705}.Debug|x64.Build.0 = Debug|x64
{7EC0F96D-FB4C-418A-9172-612CC5BCE705}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7EC0F96D-FB4C-418A-9172-612CC5BCE705}.Release|Any CPU.Build.0 = Release|Any CPU
{7EC0F96D-FB4C-418A-9172-612CC5BCE705}.Release|x64.ActiveCfg = Release|x64
{7EC0F96D-FB4C-418A-9172-612CC5BCE705}.Release|x64.Build.0 = Release|x64
{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}.Debug|x64.ActiveCfg = Debug|x64
{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}.Debug|x64.Build.0 = Debug|x64
{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}.Release|Any CPU.Build.0 = Release|Any CPU
{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}.Release|x64.ActiveCfg = Release|x64
{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}.Release|x64.Build.0 = Release|x64
{CDC0CF78-FC11-43E3-9059-3261DA3639D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDC0CF78-FC11-43E3-9059-3261DA3639D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDC0CF78-FC11-43E3-9059-3261DA3639D1}.Debug|x64.ActiveCfg = Debug|x64
{CDC0CF78-FC11-43E3-9059-3261DA3639D1}.Debug|x64.Build.0 = Debug|x64
{CDC0CF78-FC11-43E3-9059-3261DA3639D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDC0CF78-FC11-43E3-9059-3261DA3639D1}.Release|Any CPU.Build.0 = Release|Any CPU
{CDC0CF78-FC11-43E3-9059-3261DA3639D1}.Release|x64.ActiveCfg = Release|x64
{CDC0CF78-FC11-43E3-9059-3261DA3639D1}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2C612762-D1AF-4E88-9427-B4750FBECB1A}
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33205.214
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Recyclable.Collections", "Recyclable.Collections\Recyclable.Collections.csproj", "{7EC0F96D-FB4C-418A-9172-612CC5BCE705}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Recyclable.CollectionsTests", "Recyclable.CollectionsTests\Recyclable.CollectionsTests.csproj", "{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Recyclable.Collections.Benchmarks", "Recyclable.Collections.Benchmarks\Recyclable.Collections.Benchmarks.csproj", "{CDC0CF78-FC11-43E3-9059-3261DA3639D1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7EC0F96D-FB4C-418A-9172-612CC5BCE705}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7EC0F96D-FB4C-418A-9172-612CC5BCE705}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EC0F96D-FB4C-418A-9172-612CC5BCE705}.Debug|x64.ActiveCfg = Debug|x64
{7EC0F96D-FB4C-418A-9172-612CC5BCE705}.Debug|x64.Build.0 = Debug|x64
{7EC0F96D-FB4C-418A-9172-612CC5BCE705}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7EC0F96D-FB4C-418A-9172-612CC5BCE705}.Release|Any CPU.Build.0 = Release|Any CPU
{7EC0F96D-FB4C-418A-9172-612CC5BCE705}.Release|x64.ActiveCfg = Release|x64
{7EC0F96D-FB4C-418A-9172-612CC5BCE705}.Release|x64.Build.0 = Release|x64
{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}.Debug|x64.ActiveCfg = Debug|x64
{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}.Debug|x64.Build.0 = Debug|x64
{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}.Release|Any CPU.Build.0 = Release|Any CPU
{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}.Release|x64.ActiveCfg = Release|x64
{2B7223FA-2D5B-40BF-9BD9-9D6B05DC18E2}.Release|x64.Build.0 = Release|x64
{CDC0CF78-FC11-43E3-9059-3261DA3639D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDC0CF78-FC11-43E3-9059-3261DA3639D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDC0CF78-FC11-43E3-9059-3261DA3639D1}.Debug|x64.ActiveCfg = Debug|x64
{CDC0CF78-FC11-43E3-9059-3261DA3639D1}.Debug|x64.Build.0 = Debug|x64
{CDC0CF78-FC11-43E3-9059-3261DA3639D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDC0CF78-FC11-43E3-9059-3261DA3639D1}.Release|Any CPU.Build.0 = Release|Any CPU
{CDC0CF78-FC11-43E3-9059-3261DA3639D1}.Release|x64.ActiveCfg = Release|x64
{CDC0CF78-FC11-43E3-9059-3261DA3639D1}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2C612762-D1AF-4E88-9427-B4750FBECB1A}
EndGlobalSection
EndGlobal
+9 -9
View File
@@ -1,9 +1,9 @@
namespace Recyclable.Collections
{
public static class CompareResult
{
public const int XLessThanY = -1;
public const int XEqualY = 0;
public const int XGreaterThenY = 1;
}
}
namespace Recyclable.Collections
{
public static class CompareResult
{
public const int XLessThanY = -1;
public const int XEqualY = 0;
public const int XGreaterThenY = 1;
}
}
@@ -1,32 +1,32 @@
namespace Recyclable.Collections
{
public readonly struct ComparisonToComparerAdapter<T> : IComparer<T>
{
private readonly Comparison<T> _comparison;
public ComparisonToComparerAdapter(Comparison<T> comparison)
{
_comparison = comparison;
}
public int Compare(T? x, T? y)
{
if (x == null && y == null)
{
return CompareResult.XEqualY;
}
else if (x != null && y == null)
{
return CompareResult.XGreaterThenY;
}
else if (y != null && x == null)
{
return CompareResult.XLessThanY;
}
else
{
return _comparison(x!, y!);
}
}
}
}
namespace Recyclable.Collections
{
public readonly struct ComparisonToComparerAdapter<T> : IComparer<T>
{
private readonly Comparison<T> _comparison;
public ComparisonToComparerAdapter(Comparison<T> comparison)
{
_comparison = comparison;
}
public int Compare(T? x, T? y)
{
if (x == null && y == null)
{
return CompareResult.XEqualY;
}
else if (x != null && y == null)
{
return CompareResult.XGreaterThenY;
}
else if (y != null && x == null)
{
return CompareResult.XLessThanY;
}
else
{
return _comparison(x!, y!);
}
}
}
}
+12 -12
View File
@@ -1,12 +1,12 @@
using System.Runtime.ExceptionServices;
namespace Recyclable.Collections
{
internal static class ExceptionExtensions
{
public static void CaptureAndRethrow(this Exception ex)
{
ExceptionDispatchInfo.Capture(ex).Throw();
}
}
}
using System.Runtime.ExceptionServices;
namespace Recyclable.Collections
{
internal static class ExceptionExtensions
{
public static void CaptureAndRethrow(this Exception ex)
{
ExceptionDispatchInfo.Capture(ex).Throw();
}
}
}
@@ -1,10 +1,10 @@
namespace Recyclable.Collections
{
internal class FrequencyTupleComparer<TKey, TValue> : IComparer<(TKey Key, TValue Value)>
{
private static readonly IComparer<TKey> _baseComparer = Comparer<TKey>.Default;
public int Compare((TKey Key, TValue Value) x, (TKey Key, TValue Value) y)
=> _baseComparer.Compare(x.Key, y.Key);
}
}
namespace Recyclable.Collections
{
internal class FrequencyTupleComparer<TKey, TValue> : IComparer<(TKey Key, TValue Value)>
{
private static readonly IComparer<TKey> _baseComparer = Comparer<TKey>.Default;
public int Compare((TKey Key, TValue Value) x, (TKey Key, TValue Value) y)
=> _baseComparer.Compare(x.Key, y.Key);
}
}
+8 -8
View File
@@ -1,8 +1,8 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.
using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("Style", "IDE0032:Use auto property", Justification = "<Pending>", Scope = "member", Target = "~F:Recyclable.Collections.RecyclableList`1._count")]
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.
using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("Style", "IDE0032:Use auto property", Justification = "<Pending>", Scope = "member", Target = "~F:Recyclable.Collections.RecyclableList`1._count")]
@@ -1,7 +1,7 @@
namespace Recyclable.Collections
{
internal interface IRandomNumberGenerator
{
int NextInt32(int startIndex, int endIndex);
}
namespace Recyclable.Collections
{
internal interface IRandomNumberGenerator
{
int NextInt32(int startIndex, int endIndex);
}
}
+10 -10
View File
@@ -1,10 +1,10 @@
namespace Recyclable.Collections
{
internal interface IRecyclableOwner<T> : IList<T>
{
T this[long index] { get; set; }
int BlockSize { get; }
long Capacity { get; set; }
long LongCount { get; set; }
}
}
namespace Recyclable.Collections
{
internal interface IRecyclableOwner<T> : IList<T>
{
T this[long index] { get; set; }
int BlockSize { get; }
long Capacity { get; set; }
long LongCount { get; set; }
}
}
@@ -1,7 +1,7 @@
namespace Recyclable.Collections
{
public interface IRecyclableVersionedList<T> : IEnumerable<T>
{
new RecyclableList<T>.VersionedEnumerator GetEnumerator();
}
}
namespace Recyclable.Collections
{
public interface IRecyclableVersionedList<T> : IEnumerable<T>
{
new RecyclableList<T>.VersionedEnumerator GetEnumerator();
}
}
@@ -1,7 +1,7 @@
namespace Recyclable.Collections
{
public interface IRecyclableVersionedLongList<T> : IEnumerable<T>
{
new RecyclableLongList<T>.VersionedEnumerator GetEnumerator();
}
}
namespace Recyclable.Collections
{
public interface IRecyclableVersionedLongList<T> : IEnumerable<T>
{
new RecyclableLongList<T>.VersionedEnumerator GetEnumerator();
}
}
+132 -132
View File
@@ -1,132 +1,132 @@
// Ignore Spelling: Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
// The source code was taken from
// https://codereview.stackexchange.com/questions/205407/quicksort-without-recursion.
//
// It was only beautified for .Net 6.0+.
namespace Recyclable.Collections.Linq
{
internal static class QuickSortExtensions<T>
{
public static void InsertionSort(IList<T> values, int startIndex, int endIndex, IComparer<T> comparer)
{
var left = startIndex;
while (left < endIndex)
{
int right = left;
T? temp = values[++left];
while (right >= startIndex && 0 < comparer.Compare(values[right], temp))
{
values[right + 1] = values[right--];
}
values[right + 1] = temp;
}
}
public static void InsertionSort(IList<T> values, IComparer<T> comparer)
{
InsertionSort(values, 0, values.Count - 1, comparer);
}
public static void InsertionSort(IList<T> values)
{
InsertionSort(values, Comparer<T>.Default);
}
public static void QuickSort(IList<T> values, int startIndex, int endIndex, IComparer<T> comparer, IRandomNumberGenerator randomNumberGenerator)
{
var range = (startIndex, endIndex);
var stack = new Stack<(int, int)>();
do
{
startIndex = range.startIndex;
endIndex = range.endIndex;
if (endIndex - startIndex + 1 < 31)
{
InsertionSort(values, startIndex, endIndex, comparer);
continue;
}
var pivot = SampleMedian(values, startIndex, endIndex, comparer, randomNumberGenerator);
var left = startIndex;
var right = endIndex;
while (left <= right)
{
while (0 > comparer.Compare(values[left], pivot))
{
left++;
}
while (0 > comparer.Compare(pivot, values[right]))
{
right--;
}
if (left <= right)
{
Swap(values, left++, right--);
}
}
if (startIndex < right)
{
stack.Push((startIndex, right));
}
if (left < endIndex)
{
stack.Push((left, endIndex));
}
}
while (stack.TryPop(out range));
}
public static void QuickSort(IList<T> values, IComparer<T> comparer, IRandomNumberGenerator randomNumberGenerator)
=> QuickSort(values, 0, values.Count - 1, comparer, randomNumberGenerator);
public static void QuickSort(IList<T> values)
=> QuickSort(values, Comparer<T>.Default, new SystemRandomNumberGenerator());
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static T SampleMedian(IList<T> values, int startIndex, int endIndex, IComparer<T> comparer, IRandomNumberGenerator randomNumberGenerator)
{
var left = randomNumberGenerator.NextInt32(startIndex, endIndex);
var middle = randomNumberGenerator.NextInt32(startIndex, endIndex);
var right = randomNumberGenerator.NextInt32(startIndex, endIndex);
if (0 > comparer.Compare(values[right], values[left]))
{
Swap(values, right, left);
}
if (0 > comparer.Compare(values[middle], values[left]))
{
Swap(values, middle, left);
}
if (0 > comparer.Compare(values[right], values[middle]))
{
Swap(values, right, middle);
}
return values[middle];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Swap(IList<T> values, int xIndex, int yIndex)
=> (values[yIndex], values[xIndex]) = (values[xIndex], values[yIndex]);
}
}
// Ignore Spelling: Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
// The source code was taken from
// https://codereview.stackexchange.com/questions/205407/quicksort-without-recursion.
//
// It was only beautified for .Net 6.0+.
namespace Recyclable.Collections.Linq
{
internal static class QuickSortExtensions<T>
{
public static void InsertionSort(IList<T> values, int startIndex, int endIndex, IComparer<T> comparer)
{
var left = startIndex;
while (left < endIndex)
{
int right = left;
T? temp = values[++left];
while (right >= startIndex && 0 < comparer.Compare(values[right], temp))
{
values[right + 1] = values[right--];
}
values[right + 1] = temp;
}
}
public static void InsertionSort(IList<T> values, IComparer<T> comparer)
{
InsertionSort(values, 0, values.Count - 1, comparer);
}
public static void InsertionSort(IList<T> values)
{
InsertionSort(values, Comparer<T>.Default);
}
public static void QuickSort(IList<T> values, int startIndex, int endIndex, IComparer<T> comparer, IRandomNumberGenerator randomNumberGenerator)
{
var range = (startIndex, endIndex);
var stack = new Stack<(int, int)>();
do
{
startIndex = range.startIndex;
endIndex = range.endIndex;
if (endIndex - startIndex + 1 < 31)
{
InsertionSort(values, startIndex, endIndex, comparer);
continue;
}
var pivot = SampleMedian(values, startIndex, endIndex, comparer, randomNumberGenerator);
var left = startIndex;
var right = endIndex;
while (left <= right)
{
while (0 > comparer.Compare(values[left], pivot))
{
left++;
}
while (0 > comparer.Compare(pivot, values[right]))
{
right--;
}
if (left <= right)
{
Swap(values, left++, right--);
}
}
if (startIndex < right)
{
stack.Push((startIndex, right));
}
if (left < endIndex)
{
stack.Push((left, endIndex));
}
}
while (stack.TryPop(out range));
}
public static void QuickSort(IList<T> values, IComparer<T> comparer, IRandomNumberGenerator randomNumberGenerator)
=> QuickSort(values, 0, values.Count - 1, comparer, randomNumberGenerator);
public static void QuickSort(IList<T> values)
=> QuickSort(values, Comparer<T>.Default, new SystemRandomNumberGenerator());
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static T SampleMedian(IList<T> values, int startIndex, int endIndex, IComparer<T> comparer, IRandomNumberGenerator randomNumberGenerator)
{
var left = randomNumberGenerator.NextInt32(startIndex, endIndex);
var middle = randomNumberGenerator.NextInt32(startIndex, endIndex);
var right = randomNumberGenerator.NextInt32(startIndex, endIndex);
if (0 > comparer.Compare(values[right], values[left]))
{
Swap(values, right, left);
}
if (0 > comparer.Compare(values[middle], values[left]))
{
Swap(values, middle, left);
}
if (0 > comparer.Compare(values[right], values[middle]))
{
Swap(values, right, middle);
}
return values[middle];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Swap(IList<T> values, int xIndex, int yIndex)
=> (values[yIndex], values[xIndex]) = (values[xIndex], values[yIndex]);
}
}
+35 -35
View File
@@ -1,35 +1,35 @@
using System.Numerics;
using System.Runtime.CompilerServices;
namespace Recyclable.Collections
{
internal static class MathUtils
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsPow2(long value) => (value != 0) && ((value & (value - 1)) == 0);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetPow2Shift(long value) => BitOperations.IsPow2(value) ? 63 - BitOperations.LeadingZeroCount((ulong)value) : -1;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long DivMod(long number, int divider, out long remainder)
{
if (number >= checked((long)divider << 4))
{
return Math.DivRem(number, divider, out remainder);
}
else
{
int result = 0;
remainder = number;
while (remainder >= divider)
{
remainder -= divider;
result++;
}
return result;
}
}
}
}
using System.Numerics;
using System.Runtime.CompilerServices;
namespace Recyclable.Collections
{
internal static class MathUtils
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsPow2(long value) => (value != 0) && ((value & (value - 1)) == 0);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetPow2Shift(long value) => BitOperations.IsPow2(value) ? 63 - BitOperations.LeadingZeroCount((ulong)value) : -1;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long DivMod(long number, int divider, out long remainder)
{
if (number >= checked((long)divider << 4))
{
return Math.DivRem(number, divider, out remainder);
}
else
{
int result = 0;
remainder = number;
while (remainder >= divider)
{
remainder -= divider;
result++;
}
return result;
}
}
}
}
+24 -24
View File
@@ -1,24 +1,24 @@
namespace Recyclable.Collections
{
internal sealed class MemoryBucket<T>
{
public MemoryBucket(T[] memory, MemoryBucket<T> nextBucket)
{
_memory = memory;
_nextBucket = nextBucket;
}
public MemoryBucket(T[] memory)
{
_memory = memory;
}
#pragma warning disable RCS1085
private T[] _memory;
public T[] Memory { get => _memory; set => _memory = value; }
private MemoryBucket<T>? _nextBucket;
public MemoryBucket<T>? NextBucket { get => _nextBucket; set => _nextBucket = value; }
#pragma warning restore RCS1085
}
}
namespace Recyclable.Collections
{
internal sealed class MemoryBucket<T>
{
public MemoryBucket(T[] memory, MemoryBucket<T> nextBucket)
{
_memory = memory;
_nextBucket = nextBucket;
}
public MemoryBucket(T[] memory)
{
_memory = memory;
}
#pragma warning disable RCS1085
private T[] _memory;
public T[] Memory { get => _memory; set => _memory = value; }
private MemoryBucket<T>? _nextBucket;
public MemoryBucket<T>? NextBucket { get => _nextBucket; set => _nextBucket = value; }
#pragma warning restore RCS1085
}
}
+117 -117
View File
@@ -1,117 +1,117 @@
using Microsoft.Extensions.ObjectPool;
using System.Runtime.CompilerServices;
namespace Recyclable.Collections.Parallel
{
public sealed class BarrierSlimmer : IDisposable
{
private static readonly ObjectPool<BarrierSlimmer> _defaultPool = new DefaultObjectPool<BarrierSlimmer>(new DefaultPooledObjectPolicy<BarrierSlimmer>());
private ObjectPool<BarrierSlimmer>? _assignedPool;
private readonly SpinLock _lock = new(false);
public bool HasParticipants;
public int Participants;
[MethodImpl(MethodImplOptions.NoInlining)]
public void AddParticipant()
{
bool isLocked = false;
_lock.Enter(ref isLocked);
Participants++;
if (Participants == 1)
{
HasParticipants = true;
}
if (isLocked)
{
_lock.Exit(false);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public void RemoveParticipant()
{
if (!HasParticipants)
{
ThrowNoMoreParticipants();
}
bool isLocked = false;
_lock.Enter(ref isLocked);
Participants--;
if (Participants == 0)
{
HasParticipants = false;
}
if (isLocked)
{
_lock.Exit(false);
}
}
private static void ThrowNoMoreParticipants()
{
throw new InvalidOperationException("There are no more participants. The operation is not allowed");
}
public BarrierSlimmer()
{
_lock = new(false);
}
public BarrierSlimmer(int participants)
{
_lock = new(false);
Participants = participants;
HasParticipants = participants > 0;
}
public static BarrierSlimmer Create(int participants)
{
var result = _defaultPool.Get();
result.Participants = participants;
result.HasParticipants = participants > 0;
return result;
}
public void Dispose()
{
var assignedPool = _assignedPool;
_assignedPool = null;
assignedPool?.Return(this);
GC.SuppressFinalize(this);
}
public void SignalAndWait()
{
RemoveParticipant();
//if (!HasParticipants)
//{
// return;
//}
//if (!HasParticipants)
//{
// return;
//}
//if (!HasParticipants)
//{
// return;
//}
// SpinWait.SpinUntil(() => HasParticipants);
var waitSpinner = new SpinWait();
while (HasParticipants)
{
waitSpinner.SpinOnce(0);
}
}
}
}
using Microsoft.Extensions.ObjectPool;
using System.Runtime.CompilerServices;
namespace Recyclable.Collections.Parallel
{
public sealed class BarrierSlimmer : IDisposable
{
private static readonly ObjectPool<BarrierSlimmer> _defaultPool = new DefaultObjectPool<BarrierSlimmer>(new DefaultPooledObjectPolicy<BarrierSlimmer>());
private ObjectPool<BarrierSlimmer>? _assignedPool;
private readonly SpinLock _lock = new(false);
public bool HasParticipants;
public int Participants;
[MethodImpl(MethodImplOptions.NoInlining)]
public void AddParticipant()
{
bool isLocked = false;
_lock.Enter(ref isLocked);
Participants++;
if (Participants == 1)
{
HasParticipants = true;
}
if (isLocked)
{
_lock.Exit(false);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public void RemoveParticipant()
{
if (!HasParticipants)
{
ThrowNoMoreParticipants();
}
bool isLocked = false;
_lock.Enter(ref isLocked);
Participants--;
if (Participants == 0)
{
HasParticipants = false;
}
if (isLocked)
{
_lock.Exit(false);
}
}
private static void ThrowNoMoreParticipants()
{
throw new InvalidOperationException("There are no more participants. The operation is not allowed");
}
public BarrierSlimmer()
{
_lock = new(false);
}
public BarrierSlimmer(int participants)
{
_lock = new(false);
Participants = participants;
HasParticipants = participants > 0;
}
public static BarrierSlimmer Create(int participants)
{
var result = _defaultPool.Get();
result.Participants = participants;
result.HasParticipants = participants > 0;
return result;
}
public void Dispose()
{
var assignedPool = _assignedPool;
_assignedPool = null;
assignedPool?.Return(this);
GC.SuppressFinalize(this);
}
public void SignalAndWait()
{
RemoveParticipant();
//if (!HasParticipants)
//{
// return;
//}
//if (!HasParticipants)
//{
// return;
//}
//if (!HasParticipants)
//{
// return;
//}
// SpinWait.SpinUntil(() => HasParticipants);
var waitSpinner = new SpinWait();
while (HasParticipants)
{
waitSpinner.SpinOnce(0);
}
}
}
}
@@ -1,111 +1,111 @@
namespace Recyclable.Collections.Parallel
{
public sealed class IndexOfSynchronizationContext
{
private SpinWait _participantsSpinWait = new();
private SpinLockSlimmer _participantsSpinLock = new();
private SpinLockSlimmer _indexOfSpinLock = new();
internal bool _isItemFound;
internal int _participants;
internal bool _returned;
public void AddParticipant()
{
_participantsSpinLock.Enter();
_participants++;
_participantsSpinLock.Exit();
}
public void AddParticipants(int newParticipants)
{
_participantsSpinLock.Enter();
_participants += newParticipants;
_participantsSpinLock.Exit();
}
public void Lock()
{
_indexOfSpinLock.Enter();
}
public void RemoveParticipants(int participantsToRemove)
{
if (_participants < participantsToRemove)
{
ThrowNotEnoughParticipants();
}
_participantsSpinLock.Enter();
_participants -= participantsToRemove;
_participantsSpinLock.Exit();
}
public void RemoveParticipant()
{
if (_participants == 0)
{
ThrowNoMoreParticipants();
}
_participantsSpinLock.Enter();
_participants--;
_participantsSpinLock.Exit();
}
public void SignalAndWait()
{
if (_participants == 0)
{
ThrowNoMoreParticipants();
}
_participantsSpinLock.Enter();
_participants--;
_participantsSpinLock.Exit();
while (_participants > 0)
{
_participantsSpinWait.SpinOnce();
}
}
public void Unlock()
{
_indexOfSpinLock.Exit();
}
private static void ThrowNotEnoughParticipants() => throw new InvalidOperationException("Participant cannot be removed, because there are not enough participants.");
private static void ThrowNoMoreParticipants() => throw new InvalidOperationException("Participant cannot be removed, because there are no more participants.");
public long FoundBlockIndex;
public long FoundItemIndex;
public Exception? Exception;
public int Participants => _participants;
public IndexOfSynchronizationContext()
{
}
public IndexOfSynchronizationContext(int participantCount)
{
_participants = participantCount;
}
public void SetItemFound()
{
_isItemFound = true;
}
public bool IsItemFound => _isItemFound;
public void Dispose()
{
if (!_returned)
{
_returned = true;
IndexOfSynchronizationContextPool.Shared.Return(this);
}
}
}
namespace Recyclable.Collections.Parallel
{
public sealed class IndexOfSynchronizationContext
{
private SpinWait _participantsSpinWait = new();
private SpinLockSlimmer _participantsSpinLock = new();
private SpinLockSlimmer _indexOfSpinLock = new();
internal bool _isItemFound;
internal int _participants;
internal bool _returned;
public void AddParticipant()
{
_participantsSpinLock.Enter();
_participants++;
_participantsSpinLock.Exit();
}
public void AddParticipants(int newParticipants)
{
_participantsSpinLock.Enter();
_participants += newParticipants;
_participantsSpinLock.Exit();
}
public void Lock()
{
_indexOfSpinLock.Enter();
}
public void RemoveParticipants(int participantsToRemove)
{
if (_participants < participantsToRemove)
{
ThrowNotEnoughParticipants();
}
_participantsSpinLock.Enter();
_participants -= participantsToRemove;
_participantsSpinLock.Exit();
}
public void RemoveParticipant()
{
if (_participants == 0)
{
ThrowNoMoreParticipants();
}
_participantsSpinLock.Enter();
_participants--;
_participantsSpinLock.Exit();
}
public void SignalAndWait()
{
if (_participants == 0)
{
ThrowNoMoreParticipants();
}
_participantsSpinLock.Enter();
_participants--;
_participantsSpinLock.Exit();
while (_participants > 0)
{
_participantsSpinWait.SpinOnce();
}
}
public void Unlock()
{
_indexOfSpinLock.Exit();
}
private static void ThrowNotEnoughParticipants() => throw new InvalidOperationException("Participant cannot be removed, because there are not enough participants.");
private static void ThrowNoMoreParticipants() => throw new InvalidOperationException("Participant cannot be removed, because there are no more participants.");
public long FoundBlockIndex;
public long FoundItemIndex;
public Exception? Exception;
public int Participants => _participants;
public IndexOfSynchronizationContext()
{
}
public IndexOfSynchronizationContext(int participantCount)
{
_participants = participantCount;
}
public void SetItemFound()
{
_isItemFound = true;
}
public bool IsItemFound => _isItemFound;
public void Dispose()
{
if (!_returned)
{
_returned = true;
IndexOfSynchronizationContextPool.Shared.Return(this);
}
}
}
}
@@ -1,50 +1,50 @@
using Microsoft.Extensions.ObjectPool;
namespace Recyclable.Collections.Parallel
{
public static class IndexOfSynchronizationContextPool
{
private static readonly SpinLock _updateLock = new(false);
public static readonly ObjectPool<IndexOfSynchronizationContext> Shared = new DefaultObjectPool<IndexOfSynchronizationContext>(new DefaultPooledObjectPolicy<IndexOfSynchronizationContext>());
public static IndexOfSynchronizationContext GetWithOneParticipant()
{
bool lockTaken = false;
_updateLock.Enter(ref lockTaken);
var context = Shared.Get();
if (lockTaken)
{
_updateLock.Exit(false);
}
if (context._isItemFound)
{
context._isItemFound = false;
}
switch (context._participants)
{
case > 1:
context.RemoveParticipants(context._participants - 1);
break;
case 0:
context.AddParticipant();
break;
}
if (context.Exception != null)
{
context.Exception = null;
}
if (context.FoundItemIndex >= 0)
{
context.FoundItemIndex = RecyclableDefaults.ItemNotFoundIndexLong;
}
context._returned = false;
return context;
}
}
using Microsoft.Extensions.ObjectPool;
namespace Recyclable.Collections.Parallel
{
public static class IndexOfSynchronizationContextPool
{
private static readonly SpinLock _updateLock = new(false);
public static readonly ObjectPool<IndexOfSynchronizationContext> Shared = new DefaultObjectPool<IndexOfSynchronizationContext>(new DefaultPooledObjectPolicy<IndexOfSynchronizationContext>());
public static IndexOfSynchronizationContext GetWithOneParticipant()
{
bool lockTaken = false;
_updateLock.Enter(ref lockTaken);
var context = Shared.Get();
if (lockTaken)
{
_updateLock.Exit(false);
}
if (context._isItemFound)
{
context._isItemFound = false;
}
switch (context._participants)
{
case > 1:
context.RemoveParticipants(context._participants - 1);
break;
case 0:
context.AddParticipant();
break;
}
if (context.Exception != null)
{
context.Exception = null;
}
if (context.FoundItemIndex >= 0)
{
context.FoundItemIndex = RecyclableDefaults.ItemNotFoundIndexLong;
}
context._returned = false;
return context;
}
}
}
@@ -1,38 +1,38 @@
namespace Recyclable.Collections.Parallel
{
public sealed class ManualResetEventSlimmer
{
internal bool _isSet;
public bool IsSet => _isSet;
internal bool _returned;
public ManualResetEventSlimmer()
{
}
public ManualResetEventSlimmer(bool initialState)
{
_isSet = initialState;
}
public void Set()
{
_isSet = true;
}
public void Reset()
{
_isSet = false;
}
public void Dispose()
{
if (!_returned)
{
_returned = true;
ManualResetEventSlimmerPool.Shared.Return(this);
}
}
}
}
namespace Recyclable.Collections.Parallel
{
public sealed class ManualResetEventSlimmer
{
internal bool _isSet;
public bool IsSet => _isSet;
internal bool _returned;
public ManualResetEventSlimmer()
{
}
public ManualResetEventSlimmer(bool initialState)
{
_isSet = initialState;
}
public void Set()
{
_isSet = true;
}
public void Reset()
{
_isSet = false;
}
public void Dispose()
{
if (!_returned)
{
_returned = true;
ManualResetEventSlimmerPool.Shared.Return(this);
}
}
}
}
@@ -1,29 +1,29 @@
using Microsoft.Extensions.ObjectPool;
namespace Recyclable.Collections.Parallel
{
public static class ManualResetEventSlimmerPool
{
public static readonly SpinLock _updateLock = new(false);
public static readonly ObjectPool<ManualResetEventSlimmer> Shared = new DefaultObjectPool<ManualResetEventSlimmer>(new DefaultPooledObjectPolicy<ManualResetEventSlimmer>());
public static ManualResetEventSlimmer Create(bool initialState)
{
bool lockTaken = false;
_updateLock.Enter(ref lockTaken);
ManualResetEventSlimmer result = Shared.Get();
if (lockTaken)
{
_updateLock.Exit(false);
}
result._returned = false;
if (result._isSet != initialState)
{
result._isSet = initialState;
}
return result;
}
}
}
using Microsoft.Extensions.ObjectPool;
namespace Recyclable.Collections.Parallel
{
public static class ManualResetEventSlimmerPool
{
public static readonly SpinLock _updateLock = new(false);
public static readonly ObjectPool<ManualResetEventSlimmer> Shared = new DefaultObjectPool<ManualResetEventSlimmer>(new DefaultPooledObjectPolicy<ManualResetEventSlimmer>());
public static ManualResetEventSlimmer Create(bool initialState)
{
bool lockTaken = false;
_updateLock.Enter(ref lockTaken);
ManualResetEventSlimmer result = Shared.Get();
if (lockTaken)
{
_updateLock.Exit(false);
}
result._returned = false;
if (result._isSet != initialState)
{
result._isSet = initialState;
}
return result;
}
}
}
@@ -1,26 +1,26 @@
namespace Recyclable.Collections.Parallel
{
public struct SpinLockSlimmer
{
private volatile int _locked;
public void Enter()
{
if (Interlocked.CompareExchange(ref _locked, 1, 0) == 0)
{
return;
}
SpinWait waiter = new();
while (Interlocked.CompareExchange(ref _locked, 1, 0) == 1)
{
waiter.SpinOnce();
}
}
public void Exit()
{
_locked = 0;
}
}
}
namespace Recyclable.Collections.Parallel
{
public struct SpinLockSlimmer
{
private volatile int _locked;
public void Enter()
{
if (Interlocked.CompareExchange(ref _locked, 1, 0) == 0)
{
return;
}
SpinWait waiter = new();
while (Interlocked.CompareExchange(ref _locked, 1, 0) == 1)
{
waiter.SpinOnce();
}
}
public void Exit()
{
_locked = 0;
}
}
}
@@ -0,0 +1,20 @@
namespace Recyclable.Collections.Pools
{
public sealed class BiDirectionalRecyclableArrayPoolChunk<T>
{
private static readonly T[] _empty = Array.Empty<T>();
internal static T[] Empty => _empty;
internal T[] Value;
internal int Top;
internal int Bottom;
internal BiDirectionalRecyclableArrayPoolChunk<T>? Previous;
internal BiDirectionalRecyclableArrayPoolChunk<T>? Next;
public BiDirectionalRecyclableArrayPoolChunk()
{
Value = _empty;
}
}
}
@@ -0,0 +1,37 @@
using Microsoft.Extensions.ObjectPool;
namespace Recyclable.Collections.Pools
{
public static class BiDirectionalRecyclableArrayPoolChunkPool<T>
{
private static readonly ObjectPool<BiDirectionalRecyclableArrayPoolChunk<T>> _pool =
new DefaultObjectPool<BiDirectionalRecyclableArrayPoolChunk<T>>(new DefaultPooledObjectPolicy<BiDirectionalRecyclableArrayPoolChunk<T>>());
public static BiDirectionalRecyclableArrayPoolChunk<T> Rent() => _pool.Get();
public static void Return(BiDirectionalRecyclableArrayPoolChunk<T> chunk)
{
chunk.Top = 0;
chunk.Bottom = 0;
chunk.Previous = null;
chunk.Next = null;
_pool.Return(chunk);
}
public static void Dispose(BiDirectionalRecyclableArrayPoolChunk<T> chunk, bool needsClearing)
{
var buffer = chunk.Value;
if (buffer.Length >= RecyclableDefaults.MinPooledArrayLength)
{
RecyclableArrayPool<T>.ReturnShared(buffer, needsClearing);
}
chunk.Value = BiDirectionalRecyclableArrayPoolChunk<T>.Empty;
chunk.Top = 0;
chunk.Bottom = 0;
chunk.Previous = null;
chunk.Next = null;
_pool.Return(chunk);
}
}
}
+31 -31
View File
@@ -1,32 +1,32 @@
namespace Recyclable.Collections.Pools
{
internal enum MemoryPressure
{
High,
Medium,
Low
}
internal static class MemoryUtils
{
public static MemoryPressure GetMemoryPressure()
{
const double HighPressureThreshold = .90; // Percent of GC memory pressure threshold we consider "high"
const double MediumPressureThreshold = .70; // Percent of GC memory pressure threshold we consider "medium"
GCMemoryInfo memoryInfo = GC.GetGCMemoryInfo();
if (memoryInfo.MemoryLoadBytes >= memoryInfo.HighMemoryLoadThresholdBytes * HighPressureThreshold)
{
return MemoryPressure.High;
}
if (memoryInfo.MemoryLoadBytes >= memoryInfo.HighMemoryLoadThresholdBytes * MediumPressureThreshold)
{
return MemoryPressure.Medium;
}
return MemoryPressure.Low;
}
}
namespace Recyclable.Collections.Pools
{
internal enum MemoryPressure
{
High,
Medium,
Low
}
internal static class MemoryUtils
{
public static MemoryPressure GetMemoryPressure()
{
const double HighPressureThreshold = .90; // Percent of GC memory pressure threshold we consider "high"
const double MediumPressureThreshold = .70; // Percent of GC memory pressure threshold we consider "medium"
GCMemoryInfo memoryInfo = GC.GetGCMemoryInfo();
if (memoryInfo.MemoryLoadBytes >= memoryInfo.HighMemoryLoadThresholdBytes * HighPressureThreshold)
{
return MemoryPressure.High;
}
if (memoryInfo.MemoryLoadBytes >= memoryInfo.HighMemoryLoadThresholdBytes * MediumPressureThreshold)
{
return MemoryPressure.Medium;
}
return MemoryPressure.Low;
}
}
}
+144 -144
View File
@@ -1,144 +1,144 @@
namespace Recyclable.Collections.Pools
{
public struct OneSizeArrayPool<T>
{
private volatile MemoryBucket<T>? _currentBucket;
private volatile int _blockCount;
private volatile int _locked;
private readonly int _blockSize;
private readonly int _minBlockCount;
internal volatile bool _memoryExceeded;
private void Enter()
{
if (Interlocked.CompareExchange(ref _locked, 1, 0) == 0)
{
return;
}
SpinWait waiter = new();
while (Interlocked.CompareExchange(ref _locked, 1, 0) == 1)
{
waiter.SpinOnce();
}
}
private void Exit()
{
_locked = 0;
}
public OneSizeArrayPool(int blockSize, int minBlockCount)
{
_blockCount = 0;
_currentBucket = null;
_locked = 0;
_memoryExceeded = false;
_blockSize = blockSize;
_minBlockCount = minBlockCount;
_ = Task.Run(ReleaseMemoryOnExcess);
}
public T[] Rent()
{
MemoryBucket<T>? bucket;
Enter();
if (_currentBucket != null)
{
_blockCount--;
bucket = _currentBucket;
_currentBucket = _currentBucket.NextBucket;
}
else
{
bucket = null;
}
Exit();
return bucket != null ? bucket.Memory : new T[_blockSize];
}
public void ReturnUnchecked(T[] array, bool clearArray)
{
MemoryBucket<T> bucket = new(array);
if (clearArray)
{
// TODO: Measure performance
// new Span<T>(array).Clear();
Array.Clear(array, 0, array.Length);
}
Enter();
bucket.NextBucket = _currentBucket;
_currentBucket = bucket;
_blockCount++;
Exit();
}
public void Return(T[] array, bool clearArray = false)
{
if (array.Length != _blockSize)
{
ThrowHelper.ThrowWrongReturnedArraySizeException(array.Length, _blockSize);
}
if (clearArray)
{
// TODO: Measure performance
// new Span<T>(array).Clear();
Array.Clear(array, 0, array.Length);
}
MemoryBucket<T> bucket = new(array);
Enter();
if (_blockCount < _minBlockCount)
{
bucket.NextBucket = _currentBucket;
_currentBucket = bucket;
_blockCount++;
}
Exit();
}
private const int MemoryReleaseTaskSleepMilliseconds = 500;
private async Task ReleaseMemoryOnExcess()
{
while (true)
{
if (_memoryExceeded)
{
Enter();
if (_currentBucket != null)
{
_blockCount--;
_currentBucket = _currentBucket.NextBucket;
}
if (_currentBucket != null)
{
_blockCount--;
_currentBucket = _currentBucket.NextBucket;
}
if (_currentBucket != null)
{
_blockCount--;
_currentBucket = _currentBucket.NextBucket;
}
if (_currentBucket != null)
{
_blockCount--;
_currentBucket = _currentBucket.NextBucket;
}
Exit();
}
await Task.Delay(MemoryReleaseTaskSleepMilliseconds);
}
}
}
}
namespace Recyclable.Collections.Pools
{
public struct OneSizeArrayPool<T>
{
private volatile MemoryBucket<T>? _currentBucket;
private volatile int _blockCount;
private volatile int _locked;
private readonly int _blockSize;
private readonly int _minBlockCount;
internal volatile bool _memoryExceeded;
private void Enter()
{
if (Interlocked.CompareExchange(ref _locked, 1, 0) == 0)
{
return;
}
SpinWait waiter = new();
while (Interlocked.CompareExchange(ref _locked, 1, 0) == 1)
{
waiter.SpinOnce();
}
}
private void Exit()
{
_locked = 0;
}
public OneSizeArrayPool(int blockSize, int minBlockCount)
{
_blockCount = 0;
_currentBucket = null;
_locked = 0;
_memoryExceeded = false;
_blockSize = blockSize;
_minBlockCount = minBlockCount;
_ = Task.Run(ReleaseMemoryOnExcess);
}
public T[] Rent()
{
MemoryBucket<T>? bucket;
Enter();
if (_currentBucket != null)
{
_blockCount--;
bucket = _currentBucket;
_currentBucket = _currentBucket.NextBucket;
}
else
{
bucket = null;
}
Exit();
return bucket != null ? bucket.Memory : new T[_blockSize];
}
public void ReturnUnchecked(T[] array, bool clearArray)
{
MemoryBucket<T> bucket = new(array);
if (clearArray)
{
// TODO: Measure performance
// new Span<T>(array).Clear();
Array.Clear(array, 0, array.Length);
}
Enter();
bucket.NextBucket = _currentBucket;
_currentBucket = bucket;
_blockCount++;
Exit();
}
public void Return(T[] array, bool clearArray = false)
{
if (array.Length != _blockSize)
{
ThrowHelper.ThrowWrongReturnedArraySizeException(array.Length, _blockSize);
}
if (clearArray)
{
// TODO: Measure performance
// new Span<T>(array).Clear();
Array.Clear(array, 0, array.Length);
}
MemoryBucket<T> bucket = new(array);
Enter();
if (_blockCount < _minBlockCount)
{
bucket.NextBucket = _currentBucket;
_currentBucket = bucket;
_blockCount++;
}
Exit();
}
private const int MemoryReleaseTaskSleepMilliseconds = 500;
private async Task ReleaseMemoryOnExcess()
{
while (true)
{
if (_memoryExceeded)
{
Enter();
if (_currentBucket != null)
{
_blockCount--;
_currentBucket = _currentBucket.NextBucket;
}
if (_currentBucket != null)
{
_blockCount--;
_currentBucket = _currentBucket.NextBucket;
}
if (_currentBucket != null)
{
_blockCount--;
_currentBucket = _currentBucket.NextBucket;
}
if (_currentBucket != null)
{
_blockCount--;
_currentBucket = _currentBucket.NextBucket;
}
Exit();
}
await Task.Delay(MemoryReleaseTaskSleepMilliseconds);
}
}
}
}
@@ -1,53 +1,53 @@
using System.Numerics;
namespace Recyclable.Collections.Pools
{
public readonly struct RecyclableArrayPool<T>
{
internal static readonly OneSizeArrayPool<T>[] _pools = new OneSizeArrayPool<T>[32];
public static T[] RentShared(int blockSize) => _pools[31 - BitOperations.LeadingZeroCount((uint)blockSize)].Rent();
public static void ReturnShared(in T[] memoryBlock, bool needsClearing)
{
if (BitOperations.IsPow2(memoryBlock.Length))
{
_pools[31 - BitOperations.LeadingZeroCount((uint)memoryBlock.Length)].ReturnUnchecked(memoryBlock, needsClearing);
}
}
public static ref OneSizeArrayPool<T> Shared(int blockSize)
=> ref _pools[31 - BitOperations.LeadingZeroCount((uint)blockSize)];
static RecyclableArrayPool()
{
var pools = _pools;
for (var arraySize = 0; arraySize < 32; arraySize++)
{
int blockSize = 1 << arraySize;
pools[arraySize] = new OneSizeArrayPool<T>(blockSize, 1000);
}
_ = Task.Run(CheckAndReportMemoryPressure);
}
public static readonly OneSizeArrayPool<T> Null = new(0, 0);
private static async Task CheckAndReportMemoryPressure()
{
var previousValue = false;
while (true)
{
bool memoryExceeded = MemoryUtils.GetMemoryPressure() == MemoryPressure.High;
if (memoryExceeded != previousValue)
{
previousValue = memoryExceeded;
for (var i = 0; i < _pools!.Length; i++)
{
_pools[i]._memoryExceeded = memoryExceeded;
}
}
await Task.Delay(500);
}
}
}
}
using System.Numerics;
namespace Recyclable.Collections.Pools
{
public readonly struct RecyclableArrayPool<T>
{
internal static readonly OneSizeArrayPool<T>[] _pools = new OneSizeArrayPool<T>[32];
public static T[] RentShared(int blockSize) => _pools[31 - BitOperations.LeadingZeroCount((uint)blockSize)].Rent();
public static void ReturnShared(in T[] memoryBlock, bool needsClearing)
{
if (BitOperations.IsPow2(memoryBlock.Length))
{
_pools[31 - BitOperations.LeadingZeroCount((uint)memoryBlock.Length)].ReturnUnchecked(memoryBlock, needsClearing);
}
}
public static ref OneSizeArrayPool<T> Shared(int blockSize)
=> ref _pools[31 - BitOperations.LeadingZeroCount((uint)blockSize)];
static RecyclableArrayPool()
{
var pools = _pools;
for (var arraySize = 0; arraySize < 32; arraySize++)
{
int blockSize = 1 << arraySize;
pools[arraySize] = new OneSizeArrayPool<T>(blockSize, 1000);
}
_ = Task.Run(CheckAndReportMemoryPressure);
}
public static readonly OneSizeArrayPool<T> Null = new(0, 0);
private static async Task CheckAndReportMemoryPressure()
{
var previousValue = false;
while (true)
{
bool memoryExceeded = MemoryUtils.GetMemoryPressure() == MemoryPressure.High;
if (memoryExceeded != previousValue)
{
previousValue = memoryExceeded;
for (var i = 0; i < _pools!.Length; i++)
{
_pools[i]._memoryExceeded = memoryExceeded;
}
}
await Task.Delay(500);
}
}
}
}
@@ -0,0 +1,19 @@
namespace Recyclable.Collections.Pools
{
public sealed class RecyclableArrayPoolChunk<T>
{
private static readonly T[] _empty = Array.Empty<T>();
internal static T[] Empty => _empty;
internal T[] Value;
internal int Index;
internal RecyclableArrayPoolChunk<T>? Previous;
internal RecyclableArrayPoolChunk<T>? Next;
public RecyclableArrayPoolChunk()
{
Value = _empty;
}
}
}
@@ -0,0 +1,34 @@
using Microsoft.Extensions.ObjectPool;
namespace Recyclable.Collections.Pools
{
public static class RecyclableArrayPoolChunkPool<T>
{
private static readonly ObjectPool<RecyclableArrayPoolChunk<T>> _pool =
new DefaultObjectPool<RecyclableArrayPoolChunk<T>>(new DefaultPooledObjectPolicy<RecyclableArrayPoolChunk<T>>());
public static RecyclableArrayPoolChunk<T> Rent() => _pool.Get();
public static void Return(RecyclableArrayPoolChunk<T> chunk)
{
chunk.Index = 0;
chunk.Previous = null;
chunk.Next = null;
_pool.Return(chunk);
}
public static void Dispose(RecyclableArrayPoolChunk<T> chunk, bool needsClearing)
{
var buffer = chunk.Value;
if (buffer.Length >= RecyclableDefaults.MinPooledArrayLength)
{
RecyclableArrayPool<T>.ReturnShared(buffer, needsClearing);
}
chunk.Value = RecyclableArrayPoolChunk<T>.Empty;
chunk.Index = 0;
chunk.Previous = null;
chunk.Next = null;
_pool.Return(chunk);
}
}
}
@@ -1,37 +1,42 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>Recyclable.Collections</PackageId>
<PackageVersion>0.0.6</PackageVersion>
<Authors>Marek Lemańczyk</Authors>
<Company>N/A</Company>
<Product>Recyclable.Collections</Product>
<PackageTags>Recyclable ArrayPool Array Pool List Sortable Sort</PackageTags>
<Description>Recyclable.Collections project is an open source framework for operating dynamic lists at performance close to raw arrays. It aims at providing minimal memory footprint. It implements IList&lt;T&gt;'s interface and is targeted as direct replacements of List&lt;T&gt;, SortableList&lt;T&gt;, PriorityQueue&lt;T&gt; and similar.</Description>
<PackageProjectUrl>https://github.com/mlemanczyk/Recyclable.Collections</PackageProjectUrl>
<RepositoryUrl>https://github.com/mlemanczyk/Recyclable.Collections</RepositoryUrl>
<Copyright>Marek Lemańczyk 2023</Copyright>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>List&lt;T&gt; compatibility pack added for RecyclableList&lt;T&gt;. Various performance improvements and fixes.</PackageReleaseNotes>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>
<ItemGroup>
<None Include="..\LICENSE.md" Pack="true" PackagePath="" Link="LICENSE.md">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="..\README.md" Pack="true" PackagePath="" Link="README.md">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="7.0.5" />
<PackageReference Include="morelinq" Version="3.4.2" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0;net7.0;net6.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DefineConstants>$(DefineConstants);WITH_VERSIONING</DefineConstants>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>Recyclable.Collections</PackageId>
<PackageVersion>0.0.6</PackageVersion>
<Authors>Marek Lemańczyk</Authors>
<Company>N/A</Company>
<Product>Recyclable.Collections</Product>
<PackageTags>Recyclable ArrayPool Array Pool List Sortable Sort</PackageTags>
<Description>Recyclable.Collections project is an open source framework for operating dynamic lists at performance close to raw arrays. It aims at providing minimal memory footprint. It implements IList&lt;T&gt;'s interface and is targeted as direct replacements of List&lt;T&gt;, SortableList&lt;T&gt;, PriorityQueue&lt;T&gt; and similar.</Description>
<PackageProjectUrl>https://github.com/mlemanczyk/Recyclable.Collections</PackageProjectUrl>
<RepositoryUrl>https://github.com/mlemanczyk/Recyclable.Collections</RepositoryUrl>
<Copyright>Marek Lemańczyk 2023</Copyright>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>List&lt;T&gt; compatibility pack added for RecyclableList&lt;T&gt;. Various performance improvements and fixes.</PackageReleaseNotes>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>
<ItemGroup>
<None Include="..\LICENSE.md" Pack="true" PackagePath="" Link="LICENSE.md">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="..\README.md" Pack="true" PackagePath="" Link="README.md">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="7.0.5" />
<PackageReference Include="morelinq" Version="3.4.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\VersionedCollectionsGenerator\VersionedCollectionsGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
+14 -14
View File
@@ -1,14 +1,14 @@
namespace Recyclable.Collections
{
public static class RecyclableDefaults
{
public const int BlockIndexLimit = int.MaxValue;
public const int BlockSize = 16_384;
public const int InitialCapacity = 4;
public const long MinItemsCountForParallelization = 850_000;
public const int MinPooledArrayLength = 37;
public const int MaxPooledBlockSize = 2_147_483_591;
public const long ItemNotFoundIndexLong = -1L;
public const int ItemNotFoundIndex = -1;
}
}
namespace Recyclable.Collections
{
public static class RecyclableDefaults
{
public const int BlockIndexLimit = int.MaxValue;
public const int BlockSize = 16_384;
public const int InitialCapacity = 4;
public const long MinItemsCountForParallelization = 850_000;
public const int MinPooledArrayLength = 37;
public const int MaxPooledBlockSize = 2_147_483_591;
public const long ItemNotFoundIndexLong = -1L;
public const int ItemNotFoundIndex = -1;
}
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,406 @@
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using Recyclable.Collections.Pools;
namespace Recyclable.Collections
{
internal sealed class RecyclableDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDisposable
where TKey : notnull
{
internal static readonly bool _needsClearing = !typeof(TKey).IsValueType || !typeof(TValue).IsValueType;
internal readonly int _blockSize;
internal readonly int _blockSizeMinus1;
internal readonly byte _blockShift;
#nullable disable
internal Entry[][] _entries;
#nullable restore
internal int[] _buckets;
internal int _count;
internal bool _disposed;
internal struct Entry
{
public int HashCode;
public int Next;
public TKey Key;
public TValue Value;
}
public RecyclableDictionary(int blockSize = RecyclableDefaults.BlockSize)
{
if (!BitOperations.IsPow2((uint)blockSize))
{
blockSize = (int)BitOperations.RoundUpToPowerOf2((uint)blockSize);
}
_blockSize = blockSize;
_blockSizeMinus1 = blockSize - 1;
_blockShift = (byte)(31 - BitOperations.LeadingZeroCount((uint)blockSize));
_entries = 4 >= RecyclableDefaults.MinPooledArrayLength
? RecyclableArrayPool<Entry[]>.RentShared(4)
: new Entry[4][];
_entries[0] = blockSize >= RecyclableDefaults.MinPooledArrayLength
? RecyclableArrayPool<Entry>.RentShared(blockSize)
: new Entry[blockSize];
_buckets = new int[4];
Array.Fill(_buckets, -1);
}
public int Count => _count;
public TValue this[TKey key]
{
get
{
var index = FindIndex(key, out _);
if (index >= 0)
{
return GetEntry(index).Value;
}
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(key), "Key not found");
return default!;
}
set
{
var index = FindIndex(key, out _);
if (index >= 0)
{
ref var entry = ref GetEntry(index);
entry.Value = value;
return;
}
Add(key, value);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(TKey key, TValue value)
{
var hash = key.GetHashCode() & int.MaxValue;
if (_count >= (_buckets.Length * 3) / 4)
{
ResizeBuckets(_buckets.Length * 2);
}
EnsureCapacity(_count + 1);
int bucket = hash & (_buckets.Length - 1);
int index = _buckets[bucket];
while (index >= 0)
{
ref var check = ref GetEntry(index);
if (check.HashCode == hash && EqualityComparer<TKey>.Default.Equals(check.Key, key))
{
throw new ArgumentException("An element with the same key already exists.", nameof(key));
}
index = check.Next;
}
int newIndex = _count++;
ref var entry = ref GetEntry(newIndex);
entry.HashCode = hash;
entry.Key = key;
entry.Value = value;
entry.Next = _buckets[bucket];
_buckets[bucket] = newIndex;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ContainsKey(TKey key) => FindIndex(key, out _) >= 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetValue(TKey key, out TValue value)
{
var index = FindIndex(key, out _);
if (index >= 0)
{
value = GetEntry(index).Value;
return true;
}
value = default!;
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Remove(TKey key)
{
if (_count == 0)
{
return false;
}
var hash = key.GetHashCode() & int.MaxValue;
int bucket = hash & (_buckets.Length - 1);
int prev = -1;
int index = _buckets[bucket];
while (index >= 0)
{
ref var entry = ref GetEntry(index);
if (entry.HashCode == hash && EqualityComparer<TKey>.Default.Equals(entry.Key, key))
{
if (prev < 0)
{
_buckets[bucket] = entry.Next;
}
else
{
GetEntry(prev).Next = entry.Next;
}
int lastIndex = _count - 1;
if (index != lastIndex)
{
MoveEntry(lastIndex, index);
}
ClearEntry(lastIndex);
_count--;
return true;
}
prev = index;
index = entry.Next;
}
return false;
}
public void Clear()
{
if (_count == 0)
{
Array.Fill(_buckets, -1);
return;
}
int blockIndex = 0;
if (_blockSize >= RecyclableDefaults.MinPooledArrayLength)
{
while (blockIndex < _entries.Length && _entries[blockIndex] != null)
{
RecyclableArrayPool<Entry>.ReturnShared(_entries[blockIndex]!, _needsClearing);
_entries[blockIndex++] = null!;
}
}
else
{
if (_needsClearing)
{
while (blockIndex < _entries.Length && _entries[blockIndex] != null)
{
Array.Clear(_entries[blockIndex]!, 0, _entries[blockIndex]!.Length);
_entries[blockIndex++] = null!;
}
}
else
{
while (blockIndex < _entries.Length && _entries[blockIndex] != null)
{
_entries[blockIndex++] = null!;
}
}
}
_count = 0;
Array.Fill(_buckets, -1);
}
public TKey GetKey(int index) => GetEntry(index).Key;
public TValue GetValue(int index) => GetEntry(index).Value;
public Enumerator GetEnumerator() => new(this);
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() => new Enumerator(this);
IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this);
public void Dispose()
{
if (_disposed)
{
return;
}
Clear();
if (_entries.Length >= RecyclableDefaults.MinPooledArrayLength)
{
RecyclableArrayPool<Entry[]>.ReturnShared(_entries, false);
}
if (_buckets.Length >= RecyclableDefaults.MinPooledArrayLength)
{
RecyclableArrayPool<int>.ReturnShared(_buckets, false);
}
_entries = Array.Empty<Entry[]>();
_buckets = Array.Empty<int>();
_disposed = true;
GC.SuppressFinalize(this);
}
private ref Entry GetEntry(int index)
{
return ref _entries[index >> _blockShift][index & _blockSizeMinus1];
}
private void ClearEntry(int index)
{
ref var entry = ref GetEntry(index);
entry.HashCode = 0;
entry.Next = -1;
if (_needsClearing)
{
entry.Key = default!;
entry.Value = default!;
}
}
private void MoveEntry(int sourceIndex, int targetIndex)
{
ref var source = ref GetEntry(sourceIndex);
ref var target = ref GetEntry(targetIndex);
target = source;
int bucket = source.HashCode & (_buckets.Length - 1);
int cur = _buckets[bucket];
int prev = -1;
while (cur != sourceIndex)
{
prev = cur;
cur = GetEntry(cur).Next;
}
if (prev < 0)
{
_buckets[bucket] = targetIndex;
}
else
{
GetEntry(prev).Next = targetIndex;
}
}
private void EnsureCapacity(int min)
{
int requiredBlocks = (min + _blockSizeMinus1) >> _blockShift;
if (_entries.Length < requiredBlocks)
{
var newBlocks = requiredBlocks >= RecyclableDefaults.MinPooledArrayLength
? RecyclableArrayPool<Entry[]>.RentShared(requiredBlocks)
: new Entry[requiredBlocks][];
Array.Copy(_entries, newBlocks, _entries.Length);
if (_entries.Length >= RecyclableDefaults.MinPooledArrayLength)
{
RecyclableArrayPool<Entry[]>.ReturnShared(_entries, false);
}
_entries = newBlocks;
}
for (int i = 0; i < requiredBlocks; i++)
{
if (_entries[i] == null)
{
_entries[i] = _blockSize >= RecyclableDefaults.MinPooledArrayLength
? RecyclableArrayPool<Entry>.RentShared(_blockSize)
: new Entry[_blockSize];
}
}
}
private void ResizeBuckets(int newSize)
{
var newBuckets = new int[newSize];
Array.Fill(newBuckets, -1);
for (int i = 0; i < _count; i++)
{
ref var entry = ref GetEntry(i);
int bucket = entry.HashCode & (newSize - 1);
entry.Next = newBuckets[bucket];
newBuckets[bucket] = i;
}
_buckets = newBuckets;
}
private int FindIndex(TKey key, out int hash)
{
if (_count == 0)
{
hash = 0;
return -1;
}
hash = key.GetHashCode() & int.MaxValue;
int bucket = hash & (_buckets.Length - 1);
int index = _buckets[bucket];
while (index >= 0)
{
ref var entry = ref GetEntry(index);
if (entry.HashCode == hash && EqualityComparer<TKey>.Default.Equals(entry.Key, key))
{
return index;
}
index = entry.Next;
}
return -1;
}
public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>
{
private readonly RecyclableDictionary<TKey, TValue> _dictionary;
private int _index;
private KeyValuePair<TKey, TValue> _current;
internal Enumerator(RecyclableDictionary<TKey, TValue> dictionary)
{
_dictionary = dictionary;
_index = 0;
_current = default;
}
public KeyValuePair<TKey, TValue> Current => _current;
object IEnumerator.Current => _current;
public bool MoveNext()
{
if (_index >= _dictionary._count)
{
return false;
}
var entry = _dictionary.GetEntry(_index++);
_current = new KeyValuePair<TKey, TValue>(entry.Key, entry.Value);
return true;
}
public void Reset()
{
_index = 0;
_current = default;
}
public void Dispose()
{
}
}
}
}
File diff suppressed because it is too large. Load diff
+362
View File
@@ -0,0 +1,362 @@
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using Recyclable.Collections.Pools;
namespace Recyclable.Collections
{
internal sealed class RecyclableHashSet<T> : IEnumerable<T>, IDisposable
where T : notnull
{
internal static readonly bool _needsClearing = !typeof(T).IsValueType;
internal readonly int _blockSize;
internal readonly int _blockSizeMinus1;
internal readonly byte _blockShift;
#nullable disable
internal Entry[][] _entries;
#nullable restore
internal int[] _buckets;
internal int _count;
internal bool _disposed;
internal struct Entry
{
public int HashCode;
public int Next;
public T Value;
}
public RecyclableHashSet(int blockSize = RecyclableDefaults.BlockSize)
{
if (!BitOperations.IsPow2((uint)blockSize))
{
blockSize = (int)BitOperations.RoundUpToPowerOf2((uint)blockSize);
}
_blockSize = blockSize;
_blockSizeMinus1 = blockSize - 1;
_blockShift = (byte)(31 - BitOperations.LeadingZeroCount((uint)blockSize));
_entries = 4 >= RecyclableDefaults.MinPooledArrayLength
? RecyclableArrayPool<Entry[]>.RentShared(4)
: new Entry[4][];
_entries[0] = blockSize >= RecyclableDefaults.MinPooledArrayLength
? RecyclableArrayPool<Entry>.RentShared(blockSize)
: new Entry[blockSize];
_buckets = new int[4];
Array.Fill(_buckets, -1);
}
public int Count => _count;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Add(T item)
{
var hash = item.GetHashCode() & int.MaxValue;
if (_count >= (_buckets.Length * 3) / 4)
{
ResizeBuckets(_buckets.Length * 2);
}
EnsureCapacity(_count + 1);
int bucket = hash & (_buckets.Length - 1);
int index = _buckets[bucket];
while (index >= 0)
{
ref var check = ref GetEntry(index);
if (check.HashCode == hash && EqualityComparer<T>.Default.Equals(check.Value, item))
{
return false;
}
index = check.Next;
}
int newIndex = _count++;
ref var entry = ref GetEntry(newIndex);
entry.HashCode = hash;
entry.Value = item;
entry.Next = _buckets[bucket];
_buckets[bucket] = newIndex;
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(T item) => FindIndex(item, out _) >= 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Remove(T item)
{
if (_count == 0)
{
return false;
}
var hash = item.GetHashCode() & int.MaxValue;
int bucket = hash & (_buckets.Length - 1);
int prev = -1;
int index = _buckets[bucket];
while (index >= 0)
{
ref var entry = ref GetEntry(index);
if (entry.HashCode == hash && EqualityComparer<T>.Default.Equals(entry.Value, item))
{
if (prev < 0)
{
_buckets[bucket] = entry.Next;
}
else
{
GetEntry(prev).Next = entry.Next;
}
int lastIndex = _count - 1;
if (index != lastIndex)
{
MoveEntry(lastIndex, index);
}
ClearEntry(lastIndex);
_count--;
return true;
}
prev = index;
index = entry.Next;
}
return false;
}
public void Clear()
{
if (_count == 0)
{
Array.Fill(_buckets, -1);
return;
}
int blockIndex = 0;
if (_blockSize >= RecyclableDefaults.MinPooledArrayLength)
{
while (blockIndex < _entries.Length && _entries[blockIndex] != null)
{
RecyclableArrayPool<Entry>.ReturnShared(_entries[blockIndex]!, _needsClearing);
_entries[blockIndex++] = null!;
}
}
else
{
if (_needsClearing)
{
while (blockIndex < _entries.Length && _entries[blockIndex] != null)
{
Array.Clear(_entries[blockIndex]!, 0, _entries[blockIndex]!.Length);
_entries[blockIndex++] = null!;
}
}
else
{
while (blockIndex < _entries.Length && _entries[blockIndex] != null)
{
_entries[blockIndex++] = null!;
}
}
}
_count = 0;
Array.Fill(_buckets, -1);
}
public Enumerator GetEnumerator() => new(this);
IEnumerator<T> IEnumerable<T>.GetEnumerator() => new Enumerator(this);
IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this);
public void Dispose()
{
if (_disposed)
{
return;
}
Clear();
if (_entries.Length >= RecyclableDefaults.MinPooledArrayLength)
{
RecyclableArrayPool<Entry[]>.ReturnShared(_entries, false);
}
if (_buckets.Length >= RecyclableDefaults.MinPooledArrayLength)
{
RecyclableArrayPool<int>.ReturnShared(_buckets, false);
}
_entries = Array.Empty<Entry[]>();
_buckets = Array.Empty<int>();
_disposed = true;
GC.SuppressFinalize(this);
}
private ref Entry GetEntry(int index)
{
return ref _entries[index >> _blockShift][index & _blockSizeMinus1];
}
private void ClearEntry(int index)
{
ref var entry = ref GetEntry(index);
entry.HashCode = 0;
entry.Next = -1;
if (_needsClearing)
{
entry.Value = default!;
}
}
private void MoveEntry(int sourceIndex, int targetIndex)
{
ref var source = ref GetEntry(sourceIndex);
ref var target = ref GetEntry(targetIndex);
target = source;
int bucket = source.HashCode & (_buckets.Length - 1);
int cur = _buckets[bucket];
int prev = -1;
while (cur != sourceIndex)
{
prev = cur;
cur = GetEntry(cur).Next;
}
if (prev < 0)
{
_buckets[bucket] = targetIndex;
}
else
{
GetEntry(prev).Next = targetIndex;
}
}
private void EnsureCapacity(int min)
{
int requiredBlocks = (min + _blockSizeMinus1) >> _blockShift;
if (_entries.Length < requiredBlocks)
{
var newBlocks = requiredBlocks >= RecyclableDefaults.MinPooledArrayLength
? RecyclableArrayPool<Entry[]>.RentShared(requiredBlocks)
: new Entry[requiredBlocks][];
Array.Copy(_entries, newBlocks, _entries.Length);
if (_entries.Length >= RecyclableDefaults.MinPooledArrayLength)
{
RecyclableArrayPool<Entry[]>.ReturnShared(_entries, false);
}
_entries = newBlocks;
}
for (int i = 0; i < requiredBlocks; i++)
{
if (_entries[i] == null)
{
_entries[i] = _blockSize >= RecyclableDefaults.MinPooledArrayLength
? RecyclableArrayPool<Entry>.RentShared(_blockSize)
: new Entry[_blockSize];
}
}
}
private void ResizeBuckets(int newSize)
{
int[] newBuckets = newSize >= RecyclableDefaults.MinPooledArrayLength
? RecyclableArrayPool<int>.RentShared(newSize)
: new int[newSize];
Array.Fill(newBuckets, -1);
for (int i = 0; i < _count; i++)
{
ref var entry = ref GetEntry(i);
int bucket = entry.HashCode & (newSize - 1);
entry.Next = newBuckets[bucket];
newBuckets[bucket] = i;
}
if (_buckets.Length >= RecyclableDefaults.MinPooledArrayLength)
{
RecyclableArrayPool<int>.ReturnShared(_buckets, false);
}
_buckets = newBuckets;
}
private int FindIndex(T item, out int hash)
{
if (_count == 0)
{
hash = 0;
return -1;
}
hash = item.GetHashCode() & int.MaxValue;
int bucket = hash & (_buckets.Length - 1);
int index = _buckets[bucket];
while (index >= 0)
{
ref var entry = ref GetEntry(index);
if (entry.HashCode == hash && EqualityComparer<T>.Default.Equals(entry.Value, item))
{
return index;
}
index = entry.Next;
}
return -1;
}
public struct Enumerator : IEnumerator<T>
{
private readonly RecyclableHashSet<T> _set;
private int _index;
private T? _current;
internal Enumerator(RecyclableHashSet<T> set)
{
_set = set;
_index = 0;
_current = default;
}
public T Current => _current!;
object IEnumerator.Current => _current!;
public bool MoveNext()
{
if (_index >= _set._count)
{
return false;
}
_current = _set.GetEntry(_index++).Value;
return true;
}
public void Reset()
{
_index = 0;
_current = default;
}
public void Dispose()
{
}
}
}
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,323 @@
using System.Collections;
using System.Numerics;
using System.Runtime.CompilerServices;
using Recyclable.Collections.Pools;
namespace Recyclable.Collections
{
internal sealed class RecyclableLinkedList<T> : IEnumerable<T>, IDisposable
{
internal static readonly bool _needsClearing = !typeof(T).IsValueType;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static BiDirectionalRecyclableArrayPoolChunk<T> RentChunk(int size, BiDirectionalRecyclableArrayPoolChunk<T>? previous, bool forHead)
{
var chunk = BiDirectionalRecyclableArrayPoolChunkPool<T>.Rent();
if (chunk.Value.Length < size)
{
if (chunk.Value.Length >= RecyclableDefaults.MinPooledArrayLength)
{
RecyclableArrayPool<T>.ReturnShared(chunk.Value, _needsClearing);
}
chunk.Value = size >= RecyclableDefaults.MinPooledArrayLength
? RecyclableArrayPool<T>.RentShared(size)
: new T[size];
}
if (forHead)
{
chunk.Top = size;
chunk.Bottom = size;
}
else
{
chunk.Top = 0;
chunk.Bottom = 0;
}
chunk.Previous = previous;
chunk.Next = null;
return chunk;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ReturnChunk(BiDirectionalRecyclableArrayPoolChunk<T> chunk)
{
BiDirectionalRecyclableArrayPoolChunkPool<T>.Return(chunk);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void DisposeChunk(BiDirectionalRecyclableArrayPoolChunk<T> chunk)
{
BiDirectionalRecyclableArrayPoolChunkPool<T>.Dispose(chunk, _needsClearing);
}
internal BiDirectionalRecyclableArrayPoolChunk<T> _head;
internal BiDirectionalRecyclableArrayPoolChunk<T> _tail;
internal bool _disposed;
internal long _capacity;
internal long _count;
public RecyclableLinkedList(int initialCapacity = RecyclableDefaults.InitialCapacity)
{
if (initialCapacity < 1)
{
initialCapacity = 1;
}
if (!BitOperations.IsPow2((uint)initialCapacity))
{
initialCapacity = (int)BitOperations.RoundUpToPowerOf2((uint)initialCapacity);
}
_head = RentChunk(initialCapacity, null, false);
_tail = _head;
_capacity = initialCapacity;
_count = 0;
}
public RecyclableLinkedList(IEnumerable<T> source, int initialCapacity = RecyclableDefaults.InitialCapacity) : this(initialCapacity)
{
foreach (var item in source)
{
AddLast(item);
}
}
public int Count => checked((int)_count);
public long LongCount => _count;
public void AddLast(T item)
{
if (_tail.Top == _tail.Value.Length)
{
GrowTail();
}
_tail.Value[_tail.Top++] = item;
_count++;
}
public void AddFirst(T item)
{
if (_head.Bottom == 0)
{
GrowHead();
}
_head.Value[--_head.Bottom] = item;
_count++;
}
public T RemoveFirst()
{
if (_count == 0)
{
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(_count), "List is empty");
}
_count--;
var value = _head.Value[_head.Bottom];
if (_needsClearing)
{
_head.Value[_head.Bottom] = default!;
}
_head.Bottom++;
if (_head.Bottom == _head.Top && _head.Next != null)
{
ReleaseHeadChunk();
}
else if (_head.Bottom == _head.Top)
{
_head.Bottom = _head.Top;
}
return value;
}
public T RemoveLast()
{
if (_count == 0)
{
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(_count), "List is empty");
}
_count--;
_tail.Top--;
var value = _tail.Value[_tail.Top];
if (_needsClearing)
{
_tail.Value[_tail.Top] = default!;
}
if (_tail.Top == _tail.Bottom && _tail.Previous != null)
{
ReleaseTailChunk();
}
else if (_tail.Top == _tail.Bottom)
{
_tail.Top = _tail.Bottom;
}
return value;
}
public void Clear()
{
while (_head.Next != null)
{
ReleaseHeadChunk();
}
while (_tail.Previous != null)
{
ReleaseTailChunk();
}
if (_needsClearing && _head.Top > _head.Bottom)
{
Array.Clear(_head.Value, _head.Bottom, _head.Top - _head.Bottom);
}
_capacity = _head.Value.Length;
_head.Bottom = _head.Top;
_tail = _head;
_count = 0;
}
public Enumerator GetEnumerator() => new(this);
IEnumerator<T> IEnumerable<T>.GetEnumerator() => new Enumerator(this);
IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this);
private void GrowTail()
{
int newSize;
if (_capacity >= RecyclableDefaults.MaxPooledBlockSize)
{
newSize = RecyclableDefaults.MaxPooledBlockSize;
}
else
{
var doubled = _capacity << 1;
newSize = (int)Math.Min(doubled - _capacity, RecyclableDefaults.MaxPooledBlockSize);
}
var newChunk = RentChunk(newSize, _tail, false);
_tail.Next = newChunk;
_tail = newChunk;
_capacity += newSize;
}
private void GrowHead()
{
int newSize;
if (_capacity >= RecyclableDefaults.MaxPooledBlockSize)
{
newSize = RecyclableDefaults.MaxPooledBlockSize;
}
else
{
var doubled = _capacity << 1;
newSize = (int)Math.Min(doubled - _capacity, RecyclableDefaults.MaxPooledBlockSize);
}
var newChunk = RentChunk(newSize, null, true);
newChunk.Next = _head;
_head.Previous = newChunk;
_head = newChunk;
_capacity += newSize;
}
private void ReleaseHeadChunk()
{
var toReturn = _head;
_head = toReturn.Next!;
_head.Previous = null;
_capacity -= toReturn.Value.Length;
ReturnChunk(toReturn);
}
private void ReleaseTailChunk()
{
var toReturn = _tail;
_tail = toReturn.Previous!;
_tail.Next = null;
_capacity -= toReturn.Value.Length;
ReturnChunk(toReturn);
}
public void Dispose()
{
if (_disposed)
{
return;
}
var chunk = _head;
while (chunk != null)
{
var next = chunk.Next;
DisposeChunk(chunk);
chunk = next;
}
_disposed = true;
GC.SuppressFinalize(this);
}
public struct Enumerator : IEnumerator<T>
{
private readonly BiDirectionalRecyclableArrayPoolChunk<T>? _start;
private BiDirectionalRecyclableArrayPoolChunk<T>? _chunk;
private int _index;
private T? _current;
internal Enumerator(RecyclableLinkedList<T> list)
{
_start = list._head;
_chunk = list._head;
_index = _chunk != null ? _chunk.Bottom : 0;
_current = default;
}
public T Current => _current!;
object IEnumerator.Current => _current!;
public bool MoveNext()
{
if (_chunk == null)
{
return false;
}
while (_index >= _chunk.Top)
{
_chunk = _chunk.Next;
if (_chunk == null)
{
return false;
}
_index = _chunk.Bottom;
}
_current = _chunk.Value[_index++];
return true;
}
public void Reset()
{
_chunk = _start;
_index = _chunk != null ? _chunk.Bottom : 0;
}
public void Dispose()
{
_chunk = null;
_current = default;
}
}
}
}
File diff suppressed because it is too large. Load diff
@@ -1,272 +1,272 @@
using System.Numerics;
namespace Recyclable.Collections
{
[Obsolete("It'll be moved to Recyclable.Collections.Compatibility.List NuGet package in the next release.")]
public static class zRecyclableListCompatibilityListFind
{
public static bool Exists<T>(this RecyclableList<T> list, Predicate<T> match)
{
int sourceItemsCount = list._count;
if (sourceItemsCount == 0)
{
return false;
}
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = 0; itemIndex < sourceItemsCount; itemIndex++)
{
if (match(sourceSpan[itemIndex]))
{
return true;
}
}
return false;
}
public static T? Find<T>(this RecyclableList<T> list, Predicate<T> match)
{
int sourceItemsCount = list._count;
if (sourceItemsCount == 0)
{
return default;
}
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = 0; itemIndex < sourceItemsCount; itemIndex++)
{
if (match(sourceSpan[itemIndex]))
{
return sourceSpan[itemIndex];
}
}
return default;
}
public static RecyclableList<T> FindAll<T>(this RecyclableList<T> list, Predicate<T> match)
{
int sourceItemsCount = list._count;
if (sourceItemsCount == 0)
{
return new();
}
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
RecyclableList<T> result = new(sourceItemsCount >> 3);
Span<T> resultSpan = result._memoryBlock;
int capacity = result._capacity,
resultItemsCount = 0;
for (var itemIndex = 0; itemIndex < sourceItemsCount; itemIndex++)
{
if (match(sourceSpan[itemIndex]))
{
if (resultItemsCount + 1 > capacity)
{
capacity = checked((int)BitOperations.RoundUpToPowerOf2((uint)resultItemsCount + 1));
_ = RecyclableListHelpers<T>.EnsureCapacity(result, resultItemsCount, capacity);
resultSpan = result._memoryBlock;
}
resultSpan[resultItemsCount++] = sourceSpan[itemIndex];
}
}
result._capacity = capacity;
result._count = resultItemsCount;
return result;
}
public static RecyclableList<int> FindAllIndexes<T>(this RecyclableList<T> list, Predicate<T> match)
{
int sourceItemsCount = list._count;
if (sourceItemsCount == 0)
{
return new();
}
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
RecyclableList<int> result = new(sourceItemsCount >> 3);
Span<int> resultSpan = result._memoryBlock;
int capacity = result._capacity,
resultItemsCount = 0;
for (var itemIndex = 0; itemIndex < sourceItemsCount; itemIndex++)
{
if (match(sourceSpan[itemIndex]))
{
if (resultItemsCount + 1 > capacity)
{
capacity = checked((int)BitOperations.RoundUpToPowerOf2((uint)resultItemsCount + 1));
_ = RecyclableListHelpers<int>.EnsureCapacity(result, resultItemsCount, capacity);
resultSpan = result._memoryBlock;
}
resultSpan[resultItemsCount++] = itemIndex;
}
}
result._capacity = capacity;
result._count = resultItemsCount;
return result;
}
public static int FindIndex<T>(this RecyclableList<T> list, int startIndex, int count, Predicate<T> match)
{
int sourceItemsCount = list._count;
if (sourceItemsCount == 0 || startIndex >= sourceItemsCount)
{
return RecyclableDefaults.ItemNotFoundIndex;
}
sourceItemsCount = Math.Min(sourceItemsCount, startIndex + sourceItemsCount);
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = startIndex; itemIndex < sourceItemsCount; itemIndex++)
{
if (match(sourceSpan[itemIndex]))
{
return itemIndex;
}
}
return RecyclableDefaults.ItemNotFoundIndex;
}
public static int FindIndex<T>(this RecyclableList<T> list, int startIndex, Predicate<T> match)
{
int sourceItemsCount = list._count;
if (sourceItemsCount == 0 || startIndex >= sourceItemsCount)
{
return RecyclableDefaults.ItemNotFoundIndex;
}
sourceItemsCount = Math.Min(sourceItemsCount, startIndex + sourceItemsCount);
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = startIndex; itemIndex < sourceItemsCount; itemIndex++)
{
if (match(sourceSpan[itemIndex]))
{
return itemIndex;
}
}
return RecyclableDefaults.ItemNotFoundIndex;
}
public static int FindIndex<T>(this RecyclableList<T> list, Predicate<T> match)
{
int sourceItemsCount = list._count;
if (sourceItemsCount == 0)
{
return RecyclableDefaults.ItemNotFoundIndex;
}
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = 0; itemIndex < sourceItemsCount; itemIndex++)
{
if (match(sourceSpan[itemIndex]))
{
return itemIndex;
}
}
return RecyclableDefaults.ItemNotFoundIndex;
}
public static T? FindLast<T>(this RecyclableList<T> list, Predicate<T> match)
{
if (list._count == 0)
{
return default;
}
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = list._count - 1; itemIndex >= 0; itemIndex--)
{
if (match(sourceSpan[itemIndex]))
{
return sourceSpan[itemIndex];
}
}
return default;
}
public static int FindLastIndex<T>(this RecyclableList<T> list, int startIndex, int count, Predicate<T> match)
{
if (count == 0)
{
if (startIndex != -1)
{
ThrowHelper.ThrowArgumentOutOfRangeException_Index();
}
}
else if (count < 0 || startIndex - count + 1 < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeException_Count();
}
if (startIndex < 0)
{
return RecyclableDefaults.ItemNotFoundIndex;
}
int sourceItemsCount = count; // startIndex - count + 1 >= 0 ? count : startIndex + 1;
if (sourceItemsCount == 0)
{
return RecyclableDefaults.ItemNotFoundIndex;
}
int lastItemIndex = startIndex - sourceItemsCount;
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = startIndex; itemIndex > lastItemIndex; itemIndex--)
{
if (match(sourceSpan[itemIndex]))
{
return itemIndex;
}
}
return RecyclableDefaults.ItemNotFoundIndex;
}
public static int FindLastIndex<T>(this RecyclableList<T> list, int startIndex, Predicate<T> match)
{
if (list._count == 0 || startIndex < 0)
{
return RecyclableDefaults.ItemNotFoundIndex;
}
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = startIndex; itemIndex >= 0; itemIndex--)
{
if (match(sourceSpan[itemIndex]))
{
return itemIndex;
}
}
return RecyclableDefaults.ItemNotFoundIndex;
}
public static int FindLastIndex<T>(this RecyclableList<T> list, Predicate<T> match)
{
if (list._count == 0)
{
return RecyclableDefaults.ItemNotFoundIndex;
}
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = list._count - 1; itemIndex >= 0; itemIndex--)
{
if (match(sourceSpan[itemIndex]))
{
return itemIndex;
}
}
return RecyclableDefaults.ItemNotFoundIndex;
}
}
}
using System.Numerics;
namespace Recyclable.Collections
{
[Obsolete("It'll be moved to Recyclable.Collections.Compatibility.List NuGet package in the next release.")]
public static class zRecyclableListCompatibilityListFind
{
public static bool Exists<T>(this RecyclableList<T> list, Predicate<T> match)
{
int sourceItemsCount = list._count;
if (sourceItemsCount == 0)
{
return false;
}
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = 0; itemIndex < sourceItemsCount; itemIndex++)
{
if (match(sourceSpan[itemIndex]))
{
return true;
}
}
return false;
}
public static T? Find<T>(this RecyclableList<T> list, Predicate<T> match)
{
int sourceItemsCount = list._count;
if (sourceItemsCount == 0)
{
return default;
}
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = 0; itemIndex < sourceItemsCount; itemIndex++)
{
if (match(sourceSpan[itemIndex]))
{
return sourceSpan[itemIndex];
}
}
return default;
}
public static RecyclableList<T> FindAll<T>(this RecyclableList<T> list, Predicate<T> match)
{
int sourceItemsCount = list._count;
if (sourceItemsCount == 0)
{
return new();
}
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
RecyclableList<T> result = new(sourceItemsCount >> 3);
Span<T> resultSpan = result._memoryBlock;
int capacity = result._capacity,
resultItemsCount = 0;
for (var itemIndex = 0; itemIndex < sourceItemsCount; itemIndex++)
{
if (match(sourceSpan[itemIndex]))
{
if (resultItemsCount + 1 > capacity)
{
capacity = checked((int)BitOperations.RoundUpToPowerOf2((uint)resultItemsCount + 1));
_ = RecyclableListHelpers<T>.EnsureCapacity(result, resultItemsCount, capacity);
resultSpan = result._memoryBlock;
}
resultSpan[resultItemsCount++] = sourceSpan[itemIndex];
}
}
result._capacity = capacity;
result._count = resultItemsCount;
return result;
}
public static RecyclableList<int> FindAllIndexes<T>(this RecyclableList<T> list, Predicate<T> match)
{
int sourceItemsCount = list._count;
if (sourceItemsCount == 0)
{
return new();
}
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
RecyclableList<int> result = new(sourceItemsCount >> 3);
Span<int> resultSpan = result._memoryBlock;
int capacity = result._capacity,
resultItemsCount = 0;
for (var itemIndex = 0; itemIndex < sourceItemsCount; itemIndex++)
{
if (match(sourceSpan[itemIndex]))
{
if (resultItemsCount + 1 > capacity)
{
capacity = checked((int)BitOperations.RoundUpToPowerOf2((uint)resultItemsCount + 1));
_ = RecyclableListHelpers<int>.EnsureCapacity(result, resultItemsCount, capacity);
resultSpan = result._memoryBlock;
}
resultSpan[resultItemsCount++] = itemIndex;
}
}
result._capacity = capacity;
result._count = resultItemsCount;
return result;
}
public static int FindIndex<T>(this RecyclableList<T> list, int startIndex, int count, Predicate<T> match)
{
int sourceItemsCount = list._count;
if (sourceItemsCount == 0 || startIndex >= sourceItemsCount)
{
return RecyclableDefaults.ItemNotFoundIndex;
}
sourceItemsCount = Math.Min(sourceItemsCount, startIndex + sourceItemsCount);
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = startIndex; itemIndex < sourceItemsCount; itemIndex++)
{
if (match(sourceSpan[itemIndex]))
{
return itemIndex;
}
}
return RecyclableDefaults.ItemNotFoundIndex;
}
public static int FindIndex<T>(this RecyclableList<T> list, int startIndex, Predicate<T> match)
{
int sourceItemsCount = list._count;
if (sourceItemsCount == 0 || startIndex >= sourceItemsCount)
{
return RecyclableDefaults.ItemNotFoundIndex;
}
sourceItemsCount = Math.Min(sourceItemsCount, startIndex + sourceItemsCount);
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = startIndex; itemIndex < sourceItemsCount; itemIndex++)
{
if (match(sourceSpan[itemIndex]))
{
return itemIndex;
}
}
return RecyclableDefaults.ItemNotFoundIndex;
}
public static int FindIndex<T>(this RecyclableList<T> list, Predicate<T> match)
{
int sourceItemsCount = list._count;
if (sourceItemsCount == 0)
{
return RecyclableDefaults.ItemNotFoundIndex;
}
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = 0; itemIndex < sourceItemsCount; itemIndex++)
{
if (match(sourceSpan[itemIndex]))
{
return itemIndex;
}
}
return RecyclableDefaults.ItemNotFoundIndex;
}
public static T? FindLast<T>(this RecyclableList<T> list, Predicate<T> match)
{
if (list._count == 0)
{
return default;
}
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = list._count - 1; itemIndex >= 0; itemIndex--)
{
if (match(sourceSpan[itemIndex]))
{
return sourceSpan[itemIndex];
}
}
return default;
}
public static int FindLastIndex<T>(this RecyclableList<T> list, int startIndex, int count, Predicate<T> match)
{
if (count == 0)
{
if (startIndex != -1)
{
ThrowHelper.ThrowArgumentOutOfRangeException_Index();
}
}
else if (count < 0 || startIndex - count + 1 < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeException_Count();
}
if (startIndex < 0)
{
return RecyclableDefaults.ItemNotFoundIndex;
}
int sourceItemsCount = count; // startIndex - count + 1 >= 0 ? count : startIndex + 1;
if (sourceItemsCount == 0)
{
return RecyclableDefaults.ItemNotFoundIndex;
}
int lastItemIndex = startIndex - sourceItemsCount;
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = startIndex; itemIndex > lastItemIndex; itemIndex--)
{
if (match(sourceSpan[itemIndex]))
{
return itemIndex;
}
}
return RecyclableDefaults.ItemNotFoundIndex;
}
public static int FindLastIndex<T>(this RecyclableList<T> list, int startIndex, Predicate<T> match)
{
if (list._count == 0 || startIndex < 0)
{
return RecyclableDefaults.ItemNotFoundIndex;
}
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = startIndex; itemIndex >= 0; itemIndex--)
{
if (match(sourceSpan[itemIndex]))
{
return itemIndex;
}
}
return RecyclableDefaults.ItemNotFoundIndex;
}
public static int FindLastIndex<T>(this RecyclableList<T> list, Predicate<T> match)
{
if (list._count == 0)
{
return RecyclableDefaults.ItemNotFoundIndex;
}
ReadOnlySpan<T> sourceSpan = list._memoryBlock;
for (var itemIndex = list._count - 1; itemIndex >= 0; itemIndex--)
{
if (match(sourceSpan[itemIndex]))
{
return itemIndex;
}
}
return RecyclableDefaults.ItemNotFoundIndex;
}
}
}
@@ -1,26 +1,26 @@
namespace Recyclable.Collections
{
[Obsolete("It'll be moved to Recyclable.Collections.Compatibility.List NuGet package in the next release.")]
public static class zRecyclableListCompatibilityListIndexOf
{
public static int IndexOf<T>(this RecyclableList<T> list, T item, int index) => list._count != 0
? Array.IndexOf(list._memoryBlock, item, index, list._count - index)
: RecyclableDefaults.ItemNotFoundIndex;
public static int IndexOf<T>(this RecyclableList<T> list, T item, int index, int count) => list._count != 0
? Array.IndexOf(list._memoryBlock, item, index, count)
: RecyclableDefaults.ItemNotFoundIndex;
public static int LastIndexOf<T>(this RecyclableList<T> list, T item) => list._count != 0
? Array.LastIndexOf(list._memoryBlock, item, list._count - 1)
: RecyclableDefaults.ItemNotFoundIndex;
public static int LastIndexOf<T>(this RecyclableList<T> list, T item, int index) => list._count != 0
? Array.LastIndexOf(list._memoryBlock, item, index)
: RecyclableDefaults.ItemNotFoundIndex;
public static int LastIndexOf<T>(this RecyclableList<T> list, T item, int index, int count) => list._count != 0
? Array.LastIndexOf(list._memoryBlock, item, index, count)
: RecyclableDefaults.ItemNotFoundIndex;
}
}
namespace Recyclable.Collections
{
[Obsolete("It'll be moved to Recyclable.Collections.Compatibility.List NuGet package in the next release.")]
public static class zRecyclableListCompatibilityListIndexOf
{
public static int IndexOf<T>(this RecyclableList<T> list, T item, int index) => list._count != 0
? Array.IndexOf(list._memoryBlock, item, index, list._count - index)
: RecyclableDefaults.ItemNotFoundIndex;
public static int IndexOf<T>(this RecyclableList<T> list, T item, int index, int count) => list._count != 0
? Array.IndexOf(list._memoryBlock, item, index, count)
: RecyclableDefaults.ItemNotFoundIndex;
public static int LastIndexOf<T>(this RecyclableList<T> list, T item) => list._count != 0
? Array.LastIndexOf(list._memoryBlock, item, list._count - 1)
: RecyclableDefaults.ItemNotFoundIndex;
public static int LastIndexOf<T>(this RecyclableList<T> list, T item, int index) => list._count != 0
? Array.LastIndexOf(list._memoryBlock, item, index)
: RecyclableDefaults.ItemNotFoundIndex;
public static int LastIndexOf<T>(this RecyclableList<T> list, T item, int index, int count) => list._count != 0
? Array.LastIndexOf(list._memoryBlock, item, index, count)
: RecyclableDefaults.ItemNotFoundIndex;
}
}
Loaded 100 of 151 files, more files were not shown because too many files have changed in this diff. Show more