Li Chen's Blog

  • Is recursion really bad?

    After my previous post about the stack space, it appears that there is perception from the feedback that recursion is bad and we should avoid deep recursion. After writing a compiler, I know that the modern computer and compiler are complex enough and one cannot automatically assume that a hand crafted code would out-perform the compiler optimization. The only way is to do some prototype to find out.

    So why recursive code may not perform as well? Compilers place frames on a stack. In additional to arguments and local variables, compiles also need to place frame and program pointers on the frame, resulting in overheads.

    So why hand-crafted code may not performance as well? The stack used by a compiler is a simpler data structure and can grow and shrink cleanly. To replace recursion with out own stack, our stack is allocated in the heap that is far more complicated to manage. There could be overhead as well if the compiler needs to mark objects for garbage collection. Compiler also needs to worry about the memory fragmentation.

    Then there is additional complexity: CPUs have registers and multiple levels of cache. Register access is a few times faster than in-CPU cache access and is a few 10s times than on-board memory access. So it is up to the OS and compiler to maximize the use of register and in-CPU cache.

    For my particular problem, I did an experiment to rewrite my c# version of recursive code with a loop and stack approach. So here are the outcomes of the two approaches:

      Recursive call Loop and Stack
    Lines of code for the algorithm 17 46
    Speed Baseline 3% faster
    Readability Clean Far more complex

    So at the end, I was able to achieve 3% better performance with other drawbacks. My message is never assuming your sophisticated approach would automatically work out better than a simpler approach with a modern computer and compiler. Gage carefully before committing to a more complex approach.

  • What if we run out of stack space in C# or Python?

    Supposing we are running a recursive algorithm on a very large data set that requires, say, 1 million recursive calls. Normally, one would solve such a large problem by converting recursion to a loop and a stack, but what if we do not want to or cannot rewrite the algorithm?

    Python has the sys.setrecursionlimit(int) method to set the number of recursions. However, this is only part of the story; the program can still run our of stack space. C# does not have a equivalent method.

    Fortunately, both C# and Python have option to set the stack space when creating a thread. In C#, there is an overloaded constructor for the Thread class that accepts a parameter for the stack size:

    Thread t = new Thread(work, stackSize);

    In Python, we can set the stack size by calling:

    threading.stack_size(67108864)

    We can then run our work under a new thread with increased stack size.

  • Fun with Python

    I am taking a class on Coursera recently. My formal education is in physics. Although I have been working as a developer for over 18 years and have learnt a lot of programming on the job, I still would like to gain some systematic knowledge in computer science. Coursera courses taught by Standard professors provided me a wonderful chance.

    The three languages recommended for assignments are Java, C and Python. I am fluent in Java and have done some projects using C++/MFC/ATL in the past, but I would like to try something different this time. I first started with pure C. Soon I discover that I have to write a lot of code outside the question that I try to solve because the very limited C standard library. For example, to read a list of values from a file, I have to read characters by characters until I hit a delimiter. If I need a list that can grow, I have to create a data structure myself, something that I have taking for granted in .Net or Java. Out of frustration, I switched to Python. I was pleasantly surprised to find that Python is very easy to learn. The tutorial on the official Python site has the exactly the right pace for me, someone with experience in another programming. After a couple of hours on the tutorial and a few more minutes of toying with IDEL, I was in business. I like the “battery supplied” philosophy that gives everything that I need out of box. For someone from C# or Java background, curly braces are replaced by colon(:) and tab spaces. Although I tend to miss colon from time to time, I found that the idea of tab space is actually very nice once I get use to them. I also like to feature of multiple assignment and multiple return parameters. When I need to return a by-product, I just add it to the list of returns.

    When would use Python? I would use Python if I need to computer anything quick. The language is very easy to use. Python has a good collection of libraries (packages). The REPL of the interpreter allows me test ideas quickly before committing them into script. Lots of computer science work have been ported from Lisp to Python. Some universities are even teaching SICP in Python.

    When wouldn’t I use Python? I mostly would not use it in a managed environment, such as Ironpython or Jython. Both .Net and Java already have a rich library so one has to make a choice which library to use. If we use the managed runtime library, the code will tie to the particular runtime and thus not portable. If we use the Python library, then we will face the relatively long start-up time. For this reason, I would not recommend to use Ironpython for WP7 development. The only situation that I see merit with managed Python is in a server application where I can preload Python so that the start-up time is not a concern. Using Python as a managed glue language is an over-kill most of the time. A managed Scheme could be a better glue language as it is small enough to start-up very fast.

  • Understanding asynchronous programming in ASP.NET

    I recently posted about asynchronous programing patterns in .NET. Like .net framework, ASP.NET has supported asynchronous programming since 1.x. C# and VB.NET introduced the new async and await keywords in the upcoming Visual Studio 11. This is also supported in the upcoming ASP.NET 4.5. On the other hand, there is currently an open source project from the ASP.NET team called SignalR. It uses synchronous programming model to support long running comet-like request in ASP.NET. SigalR can be used in ASP.NET 4.0 today. The purpose of this post is to talk about a little bit of history so that we can put new projects/features in perspective.

    In asynchronous programming, the callee will create another thread to handle the execution and return immediately the the caller. The caller can go on with another task, or wait, join, or be called back from the callee. So one may ask what does asynchronous programming have to do with ASP.NET. Which is the caller and which is the callee?

    It turns out that the caller is ASP.NET and the callee is the asynchronous HttpHandler. When IIS calls into ASP.NET, ASP.NET immediately posts the request to the CLR ThreadPool and returns a pending status to IIS. This frees the IIS thread so that it can be used to handle static contents. In the request is handled by an IHttpHandler in ASP.NET, a thread will handle the entire life cycle of the request. If the request is handled by an IHttpAsyncHandler, ASP.NET will call the BeginProcessRequest method of the handler and return the thread to the thread pool as soon as the method exists. If the handler simply creates another thread in the BeginProcessRequest method and immediately returns, there is really no benefits since we are simply replace one thread with another. If the handler make an asynchronous call to another class that is IO blocking and immediately returns, there is real benefit because the ASP.NET thread is no longer blocked by the IO and can be returned to the thread-pool to server other requests. Even though there is not a thread associated with the request, the connection remains open until the call-back delegate passed into the BeginProcessRequest method is called. This powerful method allows an asynchronous ASP.NET application to handle far more requests than synchronous application if IO blocking is a concern. On the other hand, it is possible for an application to keep a connection open for a long time with allocating a thread, and allocates thread only on events. That is why SignalR can work today. What is the cost to keep those connections open? The TCP/IP stack needs to keep connection info. IIS and ASP.NET also need to keep the context info. That’s say we need in average 5kB to keep a connection open. Then 1GB of memory would allow us to keep 200,000 connections open. This is unusually large number for synchronous asp.net applications. That is why SignalR implementations often configure much larger number of connections than the 5000 default in IIS7.

    References:

    [1] Performing Asynchronous Work, or Tasks, in ASP.NET Applications.

    [2] ASP.NET Thread Usage on IIS 7.5, IIS 7.0, and IIS 6.0

  • A review of asynchronous programming patterns in .net

    In Visual Studio 11, C# and VB.NET will gain new keywords like async and await for asynchronous programming. Before we dive into it, I will first give a brief preview on what we have so far.

    IAsyncResult Pattern

    IAsyncResult pattern has existed since .net 1.x. The class that implements pattern would implement Beginxxx and Endxxx methods.For example, the WebRequest class has BeginGetResponse and EndGetResponse in additional to synchronous GetResponse method.  Here is the signature of the BeginGetResponse method:

    public virtual IAsyncResult BeginGetResponse( AsyncCallback callback, Object state )

    The caller of the method needs to supply a callback function which will be call when the asynchronous operation is over. The callee will usually create another thread to do the actual operation and immediate an IAsyncResult to the caller. Here are the members of IAsyncResult:

    Member Description
    AsyncState An optional application-specific object that contains information about the asynchronous operation.
    AsyncWaitHandle A WaitHandle that can be used to block application execution until the asynchronous operation completes.
    CompletedSynchronously A value that indicates whether the asynchronous operation completed on the thread used to call BeginOperationName instead of completing on a separate ThreadPool thread.
    IsCompleted A value that indicates whether the asynchronous operation has completed.

    The caller of Beginxxx method can do one of the following:

    1. Wait on the AsyncWaitHandle if the caller needs to do processing after the async operation.
    2. Poll the IsCompleted and AsyncState for completion and possibly the progress.
    3. Do nothing. Wait for the callback to be called.
    4. Call the Endxxx method. If it is called before the completion of the async operation, the calling thread will block until the asynchronous operation is complete.

    Event-based Asynchronous Pattern

    Since .net 2.0, we gain another asynchronous pattern – the Event-based Asynchronous Pattern (EAP). EAP was created to allow a richer event model. The class that implement EAP can raised multiple events during a single asynchronous operation so that the callee does not have to poll the class.

    The class that implements EAP uses xxxAsync naming conversion for its asynchronous methods. The class can optionally implements xxxCancelAsync methods or simply a CancelAsync method to allow caller to cancel the asynchronous operation. When a caller calls a xxxAsync method, the method will immediately return. The caller needs to subscribe one of the events exposed by the class for notifications of completion and progress. WebClient and BackgroundWorker are two typical components that use this pattern. For library designers, MSDN has a nice article descripting when to use which pattern.

    Task-based Asynchronous Pattern

    .Net framework 4.0 introduced yet another way to run asynchronous operation – the Task class in the Task Parallel Library.  Although the task parallel library is about parallel execution and is much more than asynchronous programming, the .net framework 4.5 uses it as the base for yet another asynchronous pattern – the Task-based Asynchronous Patterns (TAP). The class that implements TAP would expose xxxAsync methods, just like EAP. However, the xxxAsync methods return Task<TResult>. The callee can then use the members of Task<TResult> to check completion or progress, wait or continue with another operation. This pattern is the basis for the async and await keywords in Visual Studio 11; the methods that support TAP can be marked using the async keywoard and then consumed asynchronously with the await keyword. That is why only methods return Task, Task<TResult> or void (that is, no results) can be marked with async.

    [Edited 5/23/2012]

    Found this MSDN Magazine article that discusses these patterns in more detail: http://msdn.microsoft.com/en-us/magazine/ff959203.aspx.

  • Created my first Windows Phone app

    After getting a Lumia 900 last week, I decided to write an app for myself. One of the complaint that I had is that I need a minimum 4 taps to dial a number from a contact list, something I would like to avoid when I am driving. I can pin a few my most frequently used numbers to a tile; from there I can dial them with 2 taps. I set out to write an app and hope I can dial 30-40 number in 2 taps. The hear is the idea:

    1. I group my phone number in groups, such as family, friends, colleagues, etc.
    2. I display the groups using the panorama control and phone numbers within each list in a list control.
    3. I need a tap from the tile to start the application. I may or may not need a swipe to get to a contact. I do another tap to dial the phone number.

    It turned out that I need another tap after I tap the phone number. The reason is that the PhoneCallTask does not trust me; it prompted me to confirm if I really wanted to dial the number. I hope Microsoft can make this a policy that can be granted when the application is installed.

    I am an experienced WPF and Silverlight developer. This is my first Windows Phone app. I hoped I can write this app is a couple of hours, but it turned out that I spent almost a whole day to learn unique features in Windows Phone. The WindowsPhoneGeek.com site is immensely helpful. So here are a list of things that I learnt through this simple app:

    1. Panorama control and data binding to panorama control.
    2. Windows Phone Toolkit ContextMenu control and reference the tapped item from the context menu click handler.
    3. Application bar icon button and menu.
    4. WP7 navigation framework and pass an object from a page to another page.
    5. WP7 built-in theme resources.
    6. PhoneNumberChooserTask and PhoneCallTask.
    7. WP7 application and page events.
    8. WP7 icons, application icon, background icon, marketplace icon.
    9. WP7 Isolated Storage.

    These appear to be minimum to get started with an app. I was able to deploy my app to my phone. I cannot deploy to my app to marketplace yet. That is because I unlocked my phone with my employer’s account but I could not possibly release my app to the marketplace using my my employers account. I either have to pay $99 to get my own account, or release my source code to make arrangement to have someone else release the app for me.

    [Edited 5/3/2012 to add some pictures]

    First page

    Categories

  • Experimenting with Visual Studio 11 Beta

    Windows 8 is coming soon. You need Visual Studio 11 beta to develop Windows 8 applications. That said, Visual Studio 11 also offers many new features even if you do not specifically program for Windows 8:

    • .NET Framework 4.5
    • ASP.NET 4.5
    • ASP.NET MVC 4

    What is the best way to start experimenting with Visual Studio 11 Beta? .Net Framework 4.5 is a highly compatible in-place update of .Net Framework 4.0. If you uninstall .Net Framework 4.5 beta, you will have to reinstall .Net Framework 4.0. For this reason, I would suggest you not to experiment with Visual Studio 11 beta on your main work machine. Fortunately, there is a way that allows you to experiment with Visual Studio 11 with satisfactory performance without a spare machine.

    If your operating system is Windows 7, you can dual-boot your machine off a VHD. You secondary OS will resides in a VHD so it will not mess up your primary OS, yet your machine will boot from the VHD natively so the performance is much better than running a virtual machine. In my case, I installed a copy of Windows 8 consumer preview on VHD by following this blog. I then install Visual Studio 11 Beta on top of it. I then get a Windows 8 Preview/Visual Studio 11 Beta combo that I can experiment with. The performance is only slightly (let’s say 10%) worse than a dedicated machine. I can still access the files in my native file system. Is there inconvenience in dual boot? The inconvenience can be managed if you rearrange your data to live in the cloud.

  • 5 days after getting a Nokia Lumia 900

    My experience has been very pleasant so far. This Windows Phone is always very responsive and stable. I also like the idea of charging through mini USB connector rather than a flimsy proprietary connector. My primary focus this week is to be able to dial a contact quickly. The other focus is to find a list of apps both for usage and fun.

    Clean up the contact list

    Windows phone requires me to use a Windows Live account (a Hotmail account, in my case) as a cloud account. The phone maintains bi-directional automatic sync with my Hotmail. When I add other accounts such as GMail and Facebook to my phone, and import the contacts from my old phone, Windows Phone synced them all to my Hotmail contact. The result is that my contact list is flooded with over 400 contacts, many of them are duplicates. So my first task is to clean up the list. Fortunately, it is fairly easy to combine contacts in Hotmail. I was able to get it down to about 100.

    Windows phone does not automatically sync Hotmail categories as group. I had to create several groups on the phone and add contacts to groups. Windows phone also allow me to link contacts for phone/email to my Facebook and Linked-in contacts. The result is that I have a much more organized list now and is easier to navigate through groups. However, it still requires a minimum of 4 clicks to dial a number through the contact list.

    Apps, Apps and Apps

    Many journal articles claim that Windows Phone has far less apps than iPhone and Android. This is not a big concern to me as long as I can find enough good apps. Currently, there are a bout 80,000 apps for Windows Phone. I need only 20-30 good apps. So here is a list of apps that I downloaded this week:

     
    • Adobe Reader.
    • Facebook and Twitter: these are actually written by Microsoft, nothing fancy but do the job.
    • Nokia Drive, Nokia Maps and Nokia Transit: these are from Nokia collection.
    • Translator: This from Microsoft, actually quite powerful and fun to use.
    • Unit Convert: A simple utility from Microsoft.
    • SkyDrive
    • CNN and ESPN: these are actually written by Nokia.
    • Level and the Unite game: these are from Microsoft demonstrating gyro sensors. Pretty fun.
    • Nokia Creative Studio: An Instagram-like app created by Nokia that makes me wonder how possibly Instagram worthed $1 billion.
    • Endomondo Sports Track: Use the GSP to track my walking statistics.
    • Mobile Magnifier: I could not find one as good as the iPhone equivalent that I saw. I hope someone will create a better one.

    Lastly, I have a problem that cannot be solved by an app. I would like to block some spamming phone numbers. Windows Phone does not allow me to do that. I cannot find an app because Windows Phone does not have an API to do that. Samsung has implemented in hardware in some Windows Phones. For now, I just have to hit the volume key to reject a call.

  • Bought a Nokia Lumia 900

    I am sick of my Samsung Galaxy. I considered it as a half-done project with beta software. I saw a fantastic deal for Lumia 900 on Amazon Wireless for $49. In addition, Nokia is offering a $100 instant rebate to make up for a post-launch bug by April 21. The phone is currently ranked #1 on Amazon wireless and has a incredible customer rating. So I decided to give it a try.

    nokia_lumia_900_matteblack_l

    The phone arrived 2 days later. The construction and screen of the phone looks really nice. When I set the date/time of the clock, I really like the very large number and how it scrolls. The phone feels more responsive than Galaxy. However, I have also found a few places that could be polished:

    1. Outlook setup. The small company that I work with uses a self-sign certificate on the Exchange server. The Outlook in WP7 does not have an option for me to ignore the certificate error warning. So it took me a couple of hours to figure out how to access our Exchange server. Basically, I use browser to visit the Outlook Web Access. I will see the certificate warning in my Internet Explorer. From there, I bring up the property page, click “Certificates”, then Details and click “Copy to File…”. I export the certificate as a .p7b file and then email to my hotmail account. I open the email in my phone, and then open the .p7b attachment and installed the certificate. I was able to access our Exchange server from then on. I think an option to skip certificate error could be nice.
    2. Marketplace: In the search results, there isn’t any indication whether an application is already installed.
    3. Contacts: The phone imported by Hotmail contacts. I also imported the contact list from my old phone using the Contact Transfer application under Nokia collection. When I tried to dial the phone, both email and phone contacts show up. I think the phone can certainly filter out the contacts that do not have phone number. Right now it seems there are too many contacts. I am going to try the grouping feature try to group the contacts.

    So much for my first day of experience with Lumia 900. Lastly, I would like to share this very useful article that I found:

    10 things you never knew your Lumia could do.