Skip to content

Instantly share code, notes, and snippets.

@zhangz
zhangz / ObjectPool.cs
Created May 8, 2014 03:31
ObjectPool in Roslyn
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// define TRACE_LEAKS to get additional diagnostics that can lead to the leak sources. note: it will
// make everything about 2-3x slower
//
// #define TRACE_LEAKS
// define DETECT_LEAKS to detect possible leaks
// #if DEBUG
// #define DETECT_LEAKS //for now always enable DETECT_LEAKS in debug.
@zhangz
zhangz / benchmarks-scaffolding.cs
Created May 5, 2014 08:15
yet-another-benchmarks-scaffolding
static readonly double TicksToNanos = 1000 * 1000 * 1000 / (double)Stopwatch.Frequency;
const int reps = 10 * 1000 * 1000;
var gcCount = GC.CollectionCount(0);
var sw = Stopwatch.StartNew();
for (int i = 0; i < reps; i++)
{
//do stuff
}
public static class StringBuilderExtensions
{
/// <summary>
/// Sizes StringBuilder to given size
/// </summary>
/// <param name="sb">StringBuilder</param>
/// <param name="bytes">Maximum Size of resultant string. Mostly the size of string will be less than max size</param>
/// <param name="removeLinesFromBeginning">Pass true, if clipping has to happen in the beginning</param>
public static void SizeIt(this StringBuilder sb, int bytes, bool removeLinesFromBeginning)
{
@zhangz
zhangz / NotNull
Created April 26, 2014 13:53 — forked from bleroy/NotNull
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace Bleroy.Helpers {
public static class NotNull {
public static TProp Get<TSource, TProp>(this TSource source, Expression<Func<TSource, TProp>> property) where TSource : class {
if (source == null) return default(TProp);
var current = property.Body;
#!/usr/bin/env ruby
# 1. export your RIL bookmarks
# 2. save this file to the same directory where your ril_export.html is
# 3. change username and password in the script bellow
# 4. run 'ruby ril_to_instapaper.rb' in terminal
require "cgi"
require "net/http"
require "net/https"
@zhangz
zhangz / vs shortcuts
Created March 5, 2014 03:37
vs shortcuts
Full-screen mode - Alt + Shift + Enter
Quick Lauch - Ctrl + Q
Search Solution Explorer - Ctrl + ;
Opens the menu on a class that isn't recognized and gives you import options - Alt + Shift + F10
Comment lines - Ctrl + K, Ctrl + C
Uncomment lines - Ctrl + K, Ctrl + U
@zhangz
zhangz / WeakEventHandler.cs
Created February 12, 2014 12:05
WeakEventHandler
public static class WeakEventHandler {
private class WeakEventHandlerImpl {
protected readonly WeakReference m_wrTarget; // WeakReference to original delegate's target object
protected Delegate m_openEventHandler; // "Open" delegate to invoke original target's delegate method
public WeakEventHandlerImpl(Delegate d) { m_wrTarget = new WeakReference(d.Target); }
// Match is used to compare a WeakEventHandlerImpl object with an actual delegate.
// Typically used to remove a WeakEventHandlerImpl from an event collection.
public Boolean Match(Delegate strongEventHandler) {
@zhangz
zhangz / cpu-usage-sin
Last active December 30, 2015 23:59
cpu正弦曲线
class Program
{
static void Main(string[] args)
{
Sin1 sin11 = new Sin1();
//4 core-cpu
Thread t1 = new Thread(sin11.DoWork);
Sin1ThreadParam p1 = new Sin1ThreadParam(0);
Thread t2 = new Thread(sin11.DoWork);
Sin1ThreadParam p2 = new Sin1ThreadParam(1);
class Program
{
private readonly Dictionary<Foo, int> dict = new Dictionary<Foo, int>();
private readonly List<Data> data = new List<Data>();
static void Main(string[] args)
{
Program p = new Program();
var foo = new Foo(1);
//foo.GetHashCode(); //this will cache hashcode in foo, but data.Foo is another object...
/// <summary>
/// Determines the size of the object or struct created and returned by the
/// specified method. </summary>
/// <remarks>Should not be used in production! This is meant for use during
/// development, not as a general purpose sizeof function.</remarks>
/// <param name="maker">The method that creates and returns the object or
/// struct whose size will be determined.</param>
/// <returns>The size in bytes of the object created by the method.</returns>
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public long ByteSize<T>(Func<T> maker)