Skip to content

Instantly share code, notes, and snippets.

@jkotas
Last active June 12, 2019 17:05
Show Gist options
  • Save jkotas/91160457be48f8b75eec7c87c003f4bc to your computer and use it in GitHub Desktop.
Save jkotas/91160457be48f8b75eec7c87c003f4bc to your computer and use it in GitHub Desktop.
CompareInfoResurrection
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
class Program
{
readonly static string s_shortString = new string((char)257, 1);
readonly static string s_longString = new string((char)257, 1000000);
static volatile CompareInfo s_resurectedCompareInfo;
CompareInfo _compareInfo;
Program()
{
_compareInfo = new CultureInfo("cz-cz", useUserOverride: false).CompareInfo;
_compareInfo.GetHashCode(s_shortString, CompareOptions.IgnoreCase);
}
~Program()
{
s_resurectedCompareInfo = _compareInfo;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void Allocate()
{
new Program();
}
static void Pooler()
{
for (;;)
{
var compareInfo = s_resurectedCompareInfo;
if (compareInfo != null)
{
s_resurectedCompareInfo = null;
try
{
compareInfo.GetHashCode(s_longString, CompareOptions.IgnoreCase);
compareInfo.GetHashCode(s_longString, CompareOptions.IgnoreCase);
}
catch
{
}
}
}
}
static void Main(string[] args)
{
new Thread(Pooler).Start();
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(i.ToString());
Allocate();
GC.Collect();
GC.WaitForPendingFinalizers();
while (s_resurectedCompareInfo != null) { Thread.Sleep(1); }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment