Skip to content

Instantly share code, notes, and snippets.

View mgravell's full-sized avatar
🏠
Working from home

Marc Gravell mgravell

🏠
Working from home
  • Redis
  • UK
View GitHub Profile
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using Grpc.Core;
using ProtoBuf.Grpc.Client;
using ProtoBuf.Grpc.Configuration;
using System.Collections.Concurrent;
BenchmarkRunner.Run<MyBenchmark>();
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Linq;
[MemoryDiagnoser]
public class ByteArrayChunker
{
private readonly byte[] data;
[ProtoContract]
class Foo
{
public JToken? Bar { get; set; }
[ProtoMember(1)]
[SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "Used for serialization")]
private string? BarSerialized
{
get => Bar?.ToString();
protected override bool TryGetArray(out ArraySegment<T> segment)
{
if (typeof(T) == typeof(byte))
{
return MemoryMarshal.TryGetArray(Unsafe.As<Memory<byte>, Memory<T>>(ref Unsafe.AsRef(in this.memory)), out segment);
}
else
{
segment = default;
return false;
using System.Linq.Expressions;
using System.Reflection;
using System;
static class P
{
class Foo
{
public Guid Value => _value;
private readonly Guid _value;
using ProtoBuf;
using ProtoBuf.Serializers;
using System;
using System.IO;
static class P
{
static void Main()
{
const int BLOCK_COUNT = 100000;
HashSet<int> uniques = new(BLOCK_COUNT);
for (int i = 0; i < 100000; i++)
{
if (!uniques.Add(new object().GetHashCode()))
Console.WriteLine($"Collision after {uniques.Count} items");
}
// I was *expecting* to need the following; turns out that the above *by itself*
// usually produces collisions
using System;
Console.Write("Please give your age: ");
int input = Convert.ToInt32(Console.ReadLine());
if (input < 18)
{
Console.WriteLine("Not eligible for voting.");
}
else
using ProtoBuf;
using System;
static class Program
{
static void Main()
{
ReadOnlyMemory<byte> bytes = Convert.FromBase64String("CgYI3KzdiQYQidgLGgAgZiokNzk4M2Y0YjAtYjQ3My00NTY1LTkyMWUtMzI5NDk3YjAwMzQy");
var finalValue = Serializer.Deserialize<ileco.chimp.proto.FinalValue>(bytes);
Console.WriteLine(finalValue.inputGuid); // 7983f4b0-b473-4565-921e-329497b00342
using System;
using System.Reflection;
public class SomeType
{
static void Main() => new SomeType().DoTheThing();
public void DoTheThing()
{
var prop = GetType().GetProperty(nameof(AgreedInstallationDate));
var value = new DateTime(2021, 08, 17);