Memory leak in erlang.

September 15, 2009 by iron9light

Let’s talk about memory leak in java or .net first.

Maybe you think with auto Garbage Collection, there will no memory leak at all.

Suggest you write your own ArrayList. You pre-allocate a space when you create one. When you add more elements than your space, you allocate a new space twice as the old space, copy your data to the new space, and add new elements. It’s OK so far.

Now, we delete most of the elements, and, unlucky, you forgot free your space…

This is what we called memory leak in java or .NET. when you implement a ThreadPool, ObjectPool, HashTable, etc. be careful memory leak.

In  erlang, it’s nearly no cost to spawn a new process. And erlang encourage you create more processes. It’s erlang-style! We all like it!

All right.

Now I just port a java project to erlang. I make most of abject a gen_server. It’s natural. And It’s dangerous.

GC won’t free your gen_server, you need terminate it BY YOURSELF!! Every actors are everlasting, you need KILL them with your hands!

Any best practice for this kinda memory leak? Do not tell me “Supervisor + start_link”.

Object-Oriented on Erlang VM

September 6, 2009 by iron9light

Erlang is a *super* concurrent-oriented functional language.

Here, I listed some way to write object-oriented code for erlang. (Not just some shapes trick)

1. mudole as value.

ModuleName = math,
X = ModuleName:sqrt(16.0).

Ok, it’s not OO enough.

2. build-in parameter module and extends module.

-module(super).
-export([f1/0]).
f1() -> super_f1.
%%%%%%%%%%%%%%%%%%%%%%
-module(sub).
-extends(super).

Try to call sub:f1().

-module(pmodule, [X]).
-export([x/0]).
x() -> X.

And you can do this:

Obj = pmodule:new(cool),
Obj:x().

It’s true. You can do it right now! Although these features are experimented. see Inheritance in Erlang.

3. other object-oriented languages run on erlang vm.

Reia

ruby-style, every object is a gen_server.

Now, it’s compiler just compile the reia code into the erlang code.

See slide.

ECT(Erlang Class Transformation)

add object-oriented programming to Erlang.

see Object-oriented extension to Erlang.

WOOPER(Wrapper for Object-Oriented Programming in Erlang)

eXAT(erlang eXperimental Agent Tool)

Too old.

Enumeration class for string parameter

September 4, 2009 by iron9light

When I write the Google API for .NET, there are a lot of string parameters, like imageColor and safeLevel etc.

So I create an enum for every kind of parameter.

public enum SafeLevel
{
    off,
    moderate = 0,
    active,
}

I even set the moderate of SafeLevel as default value. And I can use the ToString method to get the string value most of the time.

Not every string parameter are so readable. They use “r” for relevance and “d” for date for instance.

At this time, I use extension method + switch.

public string GetString(this SortType value)
{
    switch (value)
    {
        case SortType.relevance:
            return "r";
        case SortType.date:
            return "d";
        default:
            return null;
    }
}

Or I use some helper class like LanguageUtility.

Everything is OK until Google add more choices of parameters.

Someone complained Google now can translate XXX language. Then I have to release a new version to follow the pace of Google!

The bad day has gone. Now I use string parameter directly. One can translate any language Google supported without complain and waiting.

But without enum and the helper class, API user need to check every parameter code from Google (like me).

For keeping life easy for all of you, I create the simple Enumeration class

public enum SafeLevel
    /// <summary>
    /// The enumeration. For parameters of Google APIs.
    /// </summary>
    public abstract class Enumeration
    {
        private readonly string name;

        private readonly string value;

        private readonly bool isDefault;

        /// <summary>
        /// Initializes a new instance of the <see cref="Enumeration"/> class.
        /// </summary>
        /// <param name="value">The value.</param>
        protected Enumeration(string value)
            : this(value, value)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Enumeration"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        protected Enumeration(string name, string value)
            : this(name, value, false)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Enumeration"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        /// <param name="isDefault">if set to <c>true</c> it is default value.</param>
        protected Enumeration(string name, string value, bool isDefault)
        {
            this.name = name;
            this.isDefault = isDefault;
            this.value = value;
        }

        /// <summary>
        /// Gets a value indicating whether this instance is default.
        /// </summary>
        /// <value>
        ///     <c>true</c> if this instance is default; otherwise, <c>false</c>.
        /// </value>
        public bool IsDefault
        {
            get
            {
                return this.isDefault;
            }
        }

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <value>The value.</value>
        public string Value
        {
            get
            {
                return this.value;
            }
        }

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get
            {
                return this.name;
            }
        }

        /// <summary>
        /// Performs an implicit conversion from <see cref="Google.API.Enumeration"/> to <see cref="System.String"/>.
        /// </summary>
        /// <param name="enumeration">The enumeration.</param>
        /// <returns>The result of the conversion.</returns>
        public static implicit operator string(Enumeration enumeration)
        {
            if (enumeration.IsDefault)
            {
                return null;
            }

            return enumeration.Value;
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return this.Name;
        }

        /// <summary>
        /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
        /// <returns>
        ///     <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">
        /// The <paramref name="obj"/> parameter is null.
        /// </exception>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(this, obj))
            {
                return true;
            }

            if (obj == null)
            {
                return false;
            }

            if (!this.GetType().IsInstanceOfType(obj))
            {
                return false;
            }

            return this.Value == ((Enumeration)obj).Value;
        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
        /// </returns>
        public override int GetHashCode()
        {
            Debug.Assert(this.Value != null, "Value cannot be null.");

            return this.Value.GetHashCode();
        }
    }
}

It has three readonly properties: Name, Value and IsDefault.

It can also convert to string implicitly. Then you can use it as string. Or you can make it looks like the type of parameter is Enumeration but not string.

TranslateClient client = new TranslateClient();
string translated = client.Translate(text, Language.ChineseSimplified, Language.English);

It looks the same of the old enum parameter version. But the Language is not a enum but a subclass of Enumeration and the English is a public static readonly field.

public static readonly Language English = new Language("English", "en");

Here I want add two more static method and a implicit convert method.

public static Language GetDefault();

public static ICollection<Language> GetEnums();

public static implicit operator Language(string value)

It’s not hard using reflection. But I need repeat them in every subclasses of Enumeration.

Although code template tool can help me a lot, but I do not think it’s a good I idea. Don’t repeat yourself!

So I create a generic Enumeration class:

public abstract class Enumeration<T> : Enumeration, IEquatable<T> where T : Enumeration<T>

This class has a very interesting constrain. We can only create a subclass like this:

public sealed class SortType : Enumeration<SortType>

The subclass must pass itself as the generic parameter. (Tell me if I’m wrong.) In other words, the T in Enumeration is must be the type of subclass.

Here I marked SortType as sealed, because the Enumeration can only know it subclass but not sub-subclass.

Here is the whole code of Enumeration:

    /// <summary>
    /// The enumeration. Provide more static methods and properties for every concrete enumeration.
    /// </summary>
    /// <typeparam name="T">The type of concrete enumeration.</typeparam>
    public abstract class Enumeration<T> : Enumeration, IEquatable<T>
        where T : Enumeration<T>
    {
        private static T @default;

        private static IDictionary<string, T> dictionary;

        /// <summary>
        /// Initializes a new instance of the <see cref="Enumeration&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="value">The value.</param>
        protected Enumeration(string value)
            : base(value)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Enumeration&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        protected Enumeration(string name, string value)
            : base(name, value)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Enumeration&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        /// <param name="isDefault">if set to <c>true</c> it is default value.</param>
        protected Enumeration(string name, string value, bool isDefault)
            : base(name, value, isDefault)
        {
        }

        /// <summary>
        /// Gets the dictionary of value and enumeration.
        /// </summary>
        /// <value>The dictionary.</value>
        protected static IDictionary<string, T> Dictionary
        {
            get
            {
                Initialize();
                return dictionary;
            }
        }

        /// <summary>
        /// Gets the default enumeration.
        /// </summary>
        /// <returns>The default enumeration</returns>
        public static T GetDefault()
        {
            Initialize();
            return @default;
        }

        /// <summary>
        /// Gets all enumerations.
        /// </summary>
        /// <returns>All enumerations</returns>
        public static ICollection<T> GetEnums()
        {
            return Dictionary.Values;
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public bool Equals(T other)
        {
            if (other == null)
            {
                return false;
            }

            return this.Value == other.Value;
        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        protected static void Initialize()
        {
            if (dictionary == null)
            {
                dictionary = new Dictionary<string, T>();

                var type = typeof(T);

                ////var enums =
                ////    from propertyInfo in
                ////        type.GetProperties(
                ////        BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.GetProperty)
                ////    where
                ////        propertyInfo.PropertyType.IsAssignableFrom(typeof(T)) &&
                ////        propertyInfo.GetIndexParameters().Length == 0
                ////    select propertyInfo.GetValue(null, null) as T;

                var enums =
                    from fieldInfo in
                        type.GetFields(
                        BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.GetField)
                    where fieldInfo.FieldType.IsAssignableFrom(type)
                    select fieldInfo.GetValue(null) as T;

                foreach (var @enum in enums)
                {
                    Debug.Assert(@enum != null, "enum cannot be null.");

                    dictionary[@enum.Value] = @enum;

                    if (@default == null && @enum.IsDefault)
                    {
                        @default = @enum;
                    }
                }
            }
        }

        /// <summary>
        /// Converts the specified value to this enumeration.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="construct">The constructor function.</param>
        /// <returns>The enumeration.</returns>
        protected static T Convert(string value, Func<string, T> construct)
        {
            if (value == null)
            {
                return GetDefault();
            }

            T @enum;

            if (!Dictionary.TryGetValue(value, out @enum))
            {
                @enum = construct(value);
            }

            return @enum;
        }
    }

Here I use the lazy load trick but not initialize in the static constructor, because the static constructor will run before than the static fields in subclass have been assigned.

And the implement convert operation can only defined in the subclass. So I put most of the logic into the Convert method.

Here is what a subclass looks like:

    /// <summary>
    /// The search safety level.
    /// </summary>
    public sealed class SafeLevel : Enumeration<SafeLevel>
    {
        /// <summary>
        /// Disables safe search filtering.
        /// </summary>
        public static readonly SafeLevel Off = new SafeLevel("Off", "off");

        /// <summary>
        /// Enables moderate safe search filtering. Default value.
        /// </summary>
        public static readonly SafeLevel Moderate = new SafeLevel("Moderate", "moderate", true);

        /// <summary>
        /// Enables the highest level of safe search filtering.
        /// </summary>
        public static readonly SafeLevel Active = new SafeLevel("Active", "active");

        private SafeLevel(string value)
            : base(value)
        {
        }

        private SafeLevel(string name, string value)
            : base(name, value)
        {
        }

        private SafeLevel(string name, string value, bool isDefault)
            : base(name, value, isDefault)
        {
        }

        /// <summary>
        /// Performs an implicit conversion from <see cref="System.String"/> to <see cref="SafeLevel"/>.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The result of the conversion.</returns>
        public static implicit operator SafeLevel(string value)
        {
            return Convert(value, s => new SafeLevel(s));
        }
    }

Although the solution has some smell, like visit static method in superclass and static protected method.

I hope it can solve my problem well.

Thanks for reading.

Good! F# (May 2009) support tail recursion very well!

July 23, 2009 by iron9light

What’s the tail recursion?

In computer science, tail recursion (or tail-end recursion) is a special case of recursion in which the last operation of the function, the tail call, is a recursive call. Such recursions can be easily transformed to iterations. Replacing recursion with iteration, manually or automatically, can drastically decrease the amount of stack space used and improve efficiency. This technique is commonly used with functional programming languages, where the declarative approach and explicit handling of state promote the use of recursive functions that would otherwise rapidly fill the call stack. (from Wikipedia)

Still don’t understand? OK, let me show you the code.

int f(int n) {
    return f(n + 1);    // it's the last operation.
}

If you run f(0), you will get a StackOverFlowException.

How about F#?

let rec f n = f (n + 1)

The keyword “rec” tell compiler it’s a recursive function.

We use Reflector to see how it looks like as a C# code:

public static a f&lt;a&gt;(int n)
{
    while (true)
    {
        n++;
    }
}

The magic the compiler done is called Tail Call Optimization. (see Adventures in F#–Tail Recursion in Three Languages)

Another case of tail recursion:

type t() =
member x.f1 n = x.f2 (n + 1)
member x.f2 n = x.f1 (n + 1)

This time, when you run t().f1 0, you will get the StackOverFlowException too.

Don’t worry. Just open the F# project properties dialog and the Build tab. Check the “Generate tail calls” on. Then the exception disappeared.

See what’s different of the IL code:

“Generate tail calls” unchecked

.method public instance !!a f1(int32 n) cil managed
{
.maxstack 5
L_0000: nop
L_0001: ldc.i4.s 0×2e
L_0003: call void [mscorlib]System.Console::Write(char)
L_0008: ldarg.0
L_0009: ldarg.1
L_000a: ldc.i4.1
L_000b: add
L_000c: call instance !!0 Program/t::f2(int32)
L_0011: ret
}

“Generate tail calls” checked


.method public instance !!a f1
(int32 n) cil managed
{
.maxstack 5
L_0000: nop
L_0001: ldc.i4.s 0×2e
L_0003: call void [mscorlib]System.Console::Write(char)
L_0008: ldarg.0
L_0009: ldarg.1
L_000a: ldc.i4.1
L_000b: add
L_000c: tail
L_000e: call instance !!0 Program/t::f2(int32)
L_0013: ret
}

One more case:

type t() =
member x.f3 n = x.f3 (n + 1)

No matter you check the “Generate tail calls” or not, the compiler will do the Tail Call Optimization.

The reflector will render it like this:

public a f3&amp;amp;lt;a&amp;amp;gt;(int n)
{
    while (true)
    {
        n++;
        this = this;
    }
}

Scala compile will do the Tail Call Optimization too, but there’s no “tail” in JVM until now. So we need to wait for the Java 7.

.NET’s CultureInfo do not support all languages in ISO 639-1

July 4, 2009 by iron9light

When I wanna replace the Language enum in Google translate API for .NET, I found that, there are at two three language that Google supported but .NET Framework not.

The Filipino (tl) or Tagalog;

The Maltese (mt).

And the Hebrew, Google use “iw” as the code for its language api, but .NET and ISO 639 use “he”.

Well, where is the Babel?!

Reset your firefox location bar search if it changed by AVG

July 4, 2009 by iron9light

When you install the latest AVG free, it will install two firefox addons, AVG safe search and AVG security toolbar.

Disable them by Tools->Addons, click the Disable on them.

But AVG also change your location bar. When you input some keyword, it used to go to that website directly or search it as keywords by google. Now it becomes yahoo search!!!

If you don’t like yahoo or you just wanna another search engine, you can change by the steps below:

  1. Type “about:config” in the location bar and click “enter”. Now you can see the configuration page for your firefox.
  2. Find out the “keyword.URL” and reset or modify the value.
  3. Close the configuration page. Then your old location bar comes back.

Xml Serializable Session (Dictionary)

January 8, 2009 by iron9light

In my recent (c#) project, I need save something in a session (or dictionary) and I need save the session into a standard xml document.

XmlSession session = new XmlSession();

session["string"] = "I'm a string";

session["number"] = 42;

// Any object. Make sure it can be serialize to a standard xml document.
session["object"] = new MyObject("obj", 100, DateTime.Now);

using (Stream stream = File.Create("session.xml"))
{
session.Serialize(stream);
}

using (Stream stream = File.OpenRead("session.xml"))
{
session = XmlSession.Deserialize(stream);
}

// "I'm a string"
string s = (string)session["string"];

// 42
int num = (int)session["number"];

MyObject o = (MyObject)session["object"];

If you have encountered the same requirement, you would know that is not a easy work.

You can use the XmlSerializer to do this object-xml mapping, but you need to know all the types of value in your session, especially when you deserialize the xml document.

OK, OK. I did all these for you.

Just download and session it.

XmlSession.cs

Entry.cs

FileSystemRightsHelper

January 8, 2009 by iron9light

The FileSystemRightsHelper class is a utility class to get the IO permission of a directory or a file.

DirectorySecurity security = System.IO.Directory.GetAccessControl(path);

AuthorizationRuleCollection rules = security.GetAccessRules(true, true, typeof(SecurityIdentifier));

FileSystemRightsHelper rights = new FileSystemRightsHelper(rules);

if (rights.CanWrite && rights.CanRead)
{
    Console.WriteLine("R/W access");
}
else
{
    if (rights.CanWrite)
	{
        Console.WriteLine("Only Write access");
    }
	else if (rights.CanRead)
	{
        Console.WriteLine("Only Read access");
    }
	else
	{
        Console.WriteLine("No Read and Write accesses");
    }
}

When I want to know “Do I have permission to Read or Write this file?”, I found this article by Bruce Hatt, “Testing File Access Rights in .NET 2.0“.

Thanks Bruce Hatt, I can get the answer of this common question.

Everything is OK, exception the code itself. I DO NOT LIKE THE CODE AT ALL!

So I rewrited my FileSystemRightsHelper (not just simple refactor).

Now it’s more effective and elegent.

Check it out!

FileSystemRightsHelper.cs

Google Search API for .NET 0.2 beta and Google Translate API for .NET 0.2 beta released

October 8, 2008 by iron9light

Update4

No more default http referrer. User must set it as a parameter of constructor.

Delete old Translator and Searcher classes.

Updated comments for better document.

Update3

Change all API Using string parameter instead of enum parameter.

Add new Enumeration class and enumerations for API parameters.

The API changed a lot this time. Please let me know whether you like these changes or not.

Update2

All codes are refacted (StyleCop format).

Update the APIs to support latest Google APIs.

Now can customize referrer, accept language, API key and timeout times.

Update1

Update the supported language list for Google translate api.

Set “http://code.google.com/p/google-api-for-dotnet/” as the default http referrer. (Now you cannot set your own http referrer)

No more v0.2, new features will come with v0.3.

Google APIs for .NET

Provides simple, unofficial, .NET Framework APIs for using Google Ajax RestFULL APIs (Search API, Language API etc.)

How LAZY I am. I should release nearly a month ago.

Indeed, 99+% of source have been finished when I release the Google Search API for .NET 0.1 and Google Translate API for .NET 0.1.1.

I just wait some bugs from the last versioin, so I can do some change.

I do fixed one bug, only one.

Ok, see what’s new.

The new versions are nearly no different for using, but the foundation is changed.

Replace Json.NET 2.0 by Microsoft official WCF.

And implement the HttpUtility, so not need the System.Web any more.

It now support .NET 3.5 sp1 Client Profile.

You can download here:

GoogleSearchAPI_0.3.zip

GoogleTranslateAPI_0.3.zip

More in formations, please visit the project’s site:

http://code.google.com/p/google-api-for-dotnet/

How to excute a LINQ Expression

October 8, 2008 by iron9light

Linq for .NET 3.5 is a very interesting new feature. It provide you some abilities that compiler has.

Thanks for Expression (under System.Linq.Expressions namespace), we can keep the expression tree but not compiled MSIL code.

When you use Linq like this,


var count = from dataItem in data where dataItem.Value &gt; 100 select data.Value2;

You do not need to know the expression tree (You even do not need to add the using of System.Linq.Expressions), .NET compiler and runtime to all the things for you.

That good most of the times.

Sometimes, you need to do something by yourself. Just like my current project, I need get the result directly from a expression tree.


object GetResult(Expression m);

T GetResult<T>(Expression m);

The only one I can find to do this directly is Excute method in IQueryProvider interface.


public interface IQueryProvider
{
Object Excute(Expression expression);

TResult Excute
(Expression expression);
}

Looks great, but useless. It use an interface, and I cannot find a impliment in the framework.

Ok, it’s time to show you the truth. No more words. Just the code below.


public static object GetResult(this Expression m)
{
    if (m == null)
        throw new ArgumentException("m");
    Type type = typeof (Func<>).MakeGenericType(m.Type);
    LambdaExpression lambda = Expression.Lambda(type, m);
    Delegate callback = lambda.Compile();
    object result = callback.DynamicInvoke();
    return result;
}

public static T GetResult<T>(this Expression m)
{
    if (m == null)
        throw new ArgumentNullException("m");
    Expression<Func<T>> lambda = Expression.Lambda<Func<T>>(m);
    Func<T> callback = lambda.Compile();
    T result = callback();
    return result;
}

Compile to MSIL at runtime, isn’t it cool?!