Mono Performance Team

For many years a major focus of Mono has been to be compatible-enough with .NET and to support the popular features that developers use.

We have always believed that it is better to be slow and correct than to be fast and wrong.

That said, over the years we have embarked on some multi-year projects to address some of the major performance bottlenecks: from implementing a precise GC and fine tuning it for a number of different workloads to having implemented now four versions of the code generator as well as the LLVM backend for additional speed and things like Mono.SIMD.

But these optimizations have been mostly reactive: we wait for someone to identify or spot a problem, and then we start working on a solution.

We are now taking a proactive approach.

A few months ago, Mark Probst started the new Mono performance team. The goal of the team is to improve the performance of the Mono runtime and treat performance improvements as a feature that is continously being developed, fine-tuned and monitored.

The team is working both on ways to track performance of Mono over time, implemented support for getting better insights into what happens inside the runtime and has implemented several optimizations that have been landing into Mono for the last few months.

We are actively hiring for developers to join the Mono performance team (ideally in San Francisco, where Mark is based).

Most recently, the team added a new and sophisticated new stack for performance counters which allows us to monitor what is happening on the runtime, and we are now able to export to our profiler (a joint effort between our performance team and our feature team and implemented by Ludovic). We also unified both the runtime and user-defined performance counters and will soon be sharing a new profiler UI.

Posted on 23 Jul 2014 by Miguel de Icaza

Better Crypto: Did Snowden Succeed?

Snowden is quoted on Greenwald's new book "No Place to Hide" as wanting to both spark a debate over the use of surveillance and to get software developers to adopt and create better encryption:

While I pray that public awareness and debate will lead to reform, bear in mind that the policies of men change in time, and even the Constitution is subverted when the appetites of power demand it. In words from history: Let us speak no more of faith in man, but bind him down from mischief by the chains of cryptography.

[...]

The shock of this initial period [after the first revelations] will provide the support needed to build a more equal internet, but this will not work to the advantage of the average person unless science outpaces law. By understanding the mechanisms through which our privacy is violated, we can win here. We can guarantee for all people equal protection against unreasonable search through universal laws, but only if the technical community is willing to face the threat and commit to implementing over-engineered solutions.

Last week Matthew Green asked:

Only time will be able to answer whether as a community the tech world can devise better and simpler tools for normal users to have their privacy protected by default.

Snowden has succeeded in starting an important discussion and having software developers and their patrons react to the news.

At Xamarin we build developer tools for Android and iOS developers. It is our job to provide tools that developers use on a day to day basis to build their applications, and we help them build these mobile applications.

In the last year, we have noticed several changes in our developer userbase. Our customers are requesting both features and guidance on a number of areas.

Developers are reaching to us both because there is a new understanding about what is happening to our electronic communications and also response to rapidly changing requirements from the public and private sectors.

Among the things we have noticed:

  • Using Trusted Roots Respectfully: For years, we tried to educate our users on what they should do when dealing with X509 certificates. Two years ago, most users would just pick the first option "Ignore the problem".
    Today this is no longer what developers do by default.
  • Certificate Pinning: more than ever, developers are using certificate pinning to ensure that their mobile applications are only talking to the exact party they want to.
  • Ciphersuite Selection: We recently had to extend the .NET API to allow developers to force our networking stack to control which cipher suites the underlying SSL/TLS stack uses. This is used to prevent weak or suspected ciphersuites to be used for the communications.
  • Request for more CipherSuites: Our Mono runtime implements its own SSL/TLS and crypto stacks entirely in C#. Our customers have asked us to support new cipher suites on newer versions of the standards.

Sometimes developers can use the native APIs we provide to achieve the above goals, but sometimes the native APIs on Android and iOS make this very hard or do not expose the new desired functionality, so we need to step in.

Posted on 21 May 2014 by Miguel de Icaza

News from the .NET World

Some great announcements today from the Microsoft world.

#1 Open Source ASP.NET stack

The first one is that Microsoft's next generation web stack (ASP.NET vNext) is open source from the ground up, and runs on Mono on both Linux and Mac.

There are plenty of other design principles in this new version of ASP.NET. I provide a translation from Microsoft speak into Unix speak in parenthesis:

  • Cloud-ready out of the box (this is code name for "can run with different versions of .NET side by side").
  • A single programming model for Web sites and services
  • Low-latency developer experience (Refresh on browser recompiles)
  • Make high-performance and high-productivity APIs and patterns available - enable them both to be used and compose together within a single app.
  • Fine-grained control available via command-line tools and standard file formats.
  • Delivered via NuGet (package manager, similar to Node's NPM or Ruby Gems).
  • Release as open source via the .NET Foundation.
  • Can run on Mono, on Mac and Linux.

Update: And the software is live at http://github.com/aspnet

Client Libraries to Microsoft Services

They are shipping a number of new components to talk to their online services, and they all have a license suitable for being used from platforms other than Windows.

Posted on 12 May 2014 by Miguel de Icaza

Roslyn Update

As promised, we are now tracking the Unix-friendly Roslyn port on Mono's GitHub Organization.

We implemented a few C# 6.0 features in Mono's C# compiler to simplify the set of patches required to compile Roslyn.

So you will need a fresh Mono (from git).

Posted on 28 Apr 2014 by Miguel de Icaza

Documentation for our new iOS Designer

The team has put together some beautiful getting started documentation for our iOS User Interface Designer.

In particular, check a couple of hot features on it:

Posted on 14 Apr 2014 by Miguel de Icaza

Mono on PS4

We have been working with a few PlayStation 4 C# lovers for the last few months. The first PS4 powered by Mono and MonoGame was TowerFall:

We are very excited about the upcoming Transistor, by the makers of Bastion, coming out on May 20th:

Mono on the PS4 is based on a special branch of Mono that was originally designed to support static compilation for Windows's WinStore applications [1].

[1] Kind of not very useful anymore, since Microsoft just shipped static compilation of .NET at BUILD. Still, there is no wasted effort in Mono land!

Posted on 14 Apr 2014 by Miguel de Icaza

Mono and Roslyn

Last week, Microsoft open sourced Roslyn, the .NET Compiler Platform for C# and VB.

Roslyn is an effort to create a new generation of compilers written in managed code. In addition to the standard batch compiler, it contains a compiler API that can be used by all kinds of tools that want to understand and manipulate C# source code.

Roslyn is the foundation that powers the new smarts in Visual Studio and can also be used for static analysis, code refactoring or even to smartly navigate your source code. It is a great foundation that tool developers will be able to build on.

I had the honor of sharing the stage with Anders Hejlsberg when he published the source code, and showed both Roslyn working on a Mac with Mono, as well as showing the very same patch that he demoed on stage running on Mono.

Roslyn on Mono

At BUILD, we showed Roslyn running on Mono. If you want to run your own copy of Roslyn today, you need to use both a fresh version of Mono, and apply a handful of patches to Roslyn [2].

The source code as released contains some C# 6.0 features so the patches add a bootstrapping phase, allowing Roslyn to be built with a C# 5.0 compiler from sources. There are also a couple of patches to deal with paths (Windows vs Unix paths) as well as a Unix Makefile to build the result.

Sadly, Roslyn's build script depends on a number of features of MSBuild that neither Mono or MonoDevelop/XamarinStudio support currently [3], but we hope we can address in the future. For now, we will have to maintain a Makefile-based system to use Roslyn.

Our patches no longer apply to the tip of Roslyn master, as Roslyn is under very active development. We will be updating the patches and track Roslyn master on our fork moving forward.

Currently Roslyn generates debug information using a Visual Studio native library. So the /debug switch does not work. We will be providing an alternative implementation that uses Mono's symbol writer.

Adopting Roslyn: Mono SDK

Our goal is to keep track of Roslyn as it is being developed, and when it is officially released, to bundle Roslyn's compilers with Mono [6].

But in addition, this will provide an up-to-date and compliant Visual Basic.NET compiler to Unix platforms.

Our plans currently are to keep both compilers around, and we will implement the various C# 6.0 features into Mono's C# compiler.

There are a couple of reasons for this. Our batch compiler has been fine tuned over the years, and for day-to-day compilation it is currently faster than the Roslyn compiler.

The second one is that our compiler powers our Interactive C# Shell and we are about to launch something very interesting with it. This functionality is not currently available on the open sourced Roslyn stack.

In addition, we plan on distributing the various Roslyn assemblies to Mono users, so they can build their own tools on top of Roslyn out of the box.

Adopting Roslyn: MonoDevelop/Xamarin Studio

Roslyn really shines for use in IDEs.

We have started an effort to adopt Roslyn in MonoDevelop/Xamarin Studio. This means that the underlying NRefactory engine will also adopt Roslyn.

This is going to be a gradual process, and during the migration the goal is to keep using both Mono's C# compiler as a service engine and bit by bit, replace with the Roslyn components.

We are evaluating various areas where Roslyn will have a positive impact. The plan is to start with code completion [4] and later on, support the full spectrum of features that NRefactory provides (from refactoring to code generation).

C# Standard

While not related to Roslyn, I figured it was time to share this.

For the last couple of months, the ECMA C# committee has been working on updating the spec to reflect C# 5. And this time around, the spec benefits from having two independent compiler implementations.

Mono Project and Roslyn

Our goal is to contribute fixes to the Roslyn team to make sure that Roslyn works great on Unix systems, and hopefully to provide bug reports and bug fixes as time goes by.

We are very excited about the release of Roslyn, it is an amazing piece of technology and one of the most sophisticated compiler designs available. A great place to learn great C# idioms and best practices [5], and a great foundation for great tooling for C# and VB.

Thanks to everyone at Microsoft that made this possible, and thanks to everyone on the Roslyn team for starting, contributing and delivering such an ambitious project.

Notes

[1] Roslyn uses a few tracing APIs that were not available on Mono, so you must use a newer version of Mono to build Roslyn.

[2] We even include the patch to add french quotes that Anders demoed. Make sure to skip that patch if you don't want it :-)

[3] From Michael Hutchinson:

  • There are references of the form: <Reference Include="Microsoft.Build, Version=$(VisualStudioReferenceAssemblyVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    This is a problem because our project system tries to load project references verbatim from the project file, instead of evaluating them from the MSBuild engine. This would be fixed by one of the MSBuild integration improvements I've proposed.
  • There's an InvalidProjectFileException error from the xbuild engine when loading one of the targets files that's imported by several of the code analysis projects, VSL.Settings.targets. I'm pretty sure this is because it uses MSBuild property functions, an MSBuild 4.0 feature that xbuild does not support.
  • They use the AllowNonModulatedReference metadata on some references and it's completely undocumented, so I have no idea what it does and what problems might be caused by not handling it in xbuild.
  • One project can't be opened because it's a VS Extension project. I've added the GUID and name to our list of known project types so we show a more useful error message.
  • A few of the projects depend on Microsoft.Build.dll, and Mono does not have a working implementation of it yet. They also reference other MSBuild assemblies which I know we have not finished.

[4] Since Roslyn is much better at error recovery and has a much more comprehensive support for code completion than Mono's C# compiler does. It also has much better support for dealing with incremental changes than we do.

[5] Modulo private. They use private everywhere, and that is just plain ugly.

[6] We will find out a way of selecting which compiler to use, either mcs (Mono's C# Compiler) or Roslyn.

Posted on 09 Apr 2014 by Miguel de Icaza

ISO C++ 2D API

Herb Sutter from the ISO C++ group, reached out to the Cairo folks:

We are actively looking at the potential standardization of a basic 2D drawing library for ISO C++, and would like to base it on (or outright adopt, possibly as a binding) solid prior art in the form of an existing library.

And also:

we are focused on current Cairo as a starting point, even though it's not C++ -- we believe Cairo itself it is very well written C (already in an OO style, already const-correct, etc.).

Congratulations to the Cairo guys for designing such a pleasant to use 2D API.

But this would not be a Saturday blog post without pointing out that Cairo's C-based API is easier and simpler to use than many of those C++ libraries out there. The more sophisticated the use of the C++ language to get some performance benefit, the more unpleasant the API is to use.

The incredibly powerful Antigrain sports an insanely fast software renderer and also a quite hostile template-based API.

We got to compare Antigrain and Cairo back when we worked on Moonlight. Cairo was the clear winner.

We built Moonlight in C++ for all the wrong reasons ("better performance", "memory usage") and was a decision we came to regret. Not only were the reasons wrong, it is not clear we got any performance benefit and it is clear that we did worse with memory usage.

But that is a story for another time.

Posted on 04 Jan 2014 by Miguel de Icaza

Debugging Remote Mono Targets

Few guys have approached us recently about doing remote debugging of a Mono process. Typically this involves an underpowered system, or some kind of embedded system running Mono, and a fancy Mac or PC on the other end.

These are the instructions that Michael Hutchinson kindly provided on how to remotely debug your process using either Xamarin Studio or MonoDevelop:

Remote debugging is actually really easy with the Mono soft debugger. The IDE sends commands over TCP/IP to the Mono Soft Debugger agent inside the runtime. Depending how you launch the debuggee, you can either have it connect to the IDE over TCP, or have it open a port and wait for the IDE to connect to it.

For simple prototyping purposes, you can just set the MONODEVELOP_SDB_TEST env var, and a new "Run->Run With->Custom Soft Debugger" command will show up in Xamarin Studio / MonoDevelop, and you can specify an arbitrary IP and port or connect or or listen on, and optionally a command to run. Then you just have to start the debuggee with the correct --debugger-agent arguments (see the Mono manpage for details), start the connection, and start debugging.

For a production workflow, you'd typically create a MonoDevelop addin with a debugger engine and session subclassing the soft debugger classes, and overriding how to launch the app and set up the connection parameters. You'd typically have a custom project type too subclassing the DotNetProject, so you could override how the project was built and executed, and so that the new debugger engine could be the primary debugger for projects of that type. You'd get all the default .NET/Mono project and debugger functionality "for free".

You can get some inspiration on how to build your own add-in from the old MeeGo add-in. It has bitrotted, since MeeGo is no more, but it is good enough as a starting point.

Posted on 29 Oct 2013 by Miguel de Icaza

Hiring Developers

I am hiring software developers.

We are growing our Xamarin Studio/MonoDevelop, Visual Studio, iOS and Android teams.

Ideally, you are a C# programmer and ideally, you relocate to Boston, MA. But we can work with remote employees

Posted on 22 Aug 2013 by Miguel de Icaza

PlayScript

Those of you that saw my What is in Mono presentation at MonkeySpace know that there is a new language in the .NET world being developed by Zynga called PlayScript.

Check their home page on GitHub with cute videos!

PlayScript is a superset of ActionScript and it was based on Mono's C# 5.0 compiler. PlayScript is ActionScript augmented with C# 5.0 features.

Zynga's PlayScript allows developers that have some ActionScript/Flash code to bring their code to any platform that supports the ECMA Intermediate Language (Microsoft .NET and Mono) and blend it with other .NET languages.

But the PlayScript has a fabulous feature, it allows mixing code in both C# and PlayScript in the same compilation unit. For example, you can use the compiler like this:


	# Compile the C# source foo.cs into foo.exe
	$ mcs foo.cs

	# Compile the Playscript source bar.play into bar.exe
	$ mcs bar.play

	# Compile both C# and Playscript into a single executable
	$ mcs foo.cs bar.play
	

On a larger scale, you can see this in action in the pscorlib library. This library contains a blend of PlayScript and C# source code compiled into a single binary.

They have implemented the accelerated 3D APIs from Flash and ported some popular ActionScript libraries to PlayScript, like the Starling framework and Away3D.

They have also an add-on for Xamarin Studio that you can use to get IDE support for it.

At Xamarin we are working to integrate their language changes into Mono directly. You can track the integration work in the playscript-mono branch at GitHub.

Just like Unity's UnityScript, developers will be encouraged to use strong types to improve the performance of their applications.

You can join the PlayScript forum to track the progress of the project. And you can keep up with exciting developments.

Update: Some background on Playscript.

Posted on 20 Aug 2013 by Miguel de Icaza

What is new on Mono

Slides from my MonkeySpace "What is new in Mono?" talk:

Posted on 20 Aug 2013 by Miguel de Icaza

Callbacks as our Generations' Go To Statement

This week, as I was preparing my presentation on C# sung on iOS and Android, I came to the realization that callback-based programming has somehow become an acceptable programming model.

It has become an acceptable programming model, in the same way that using IF/GOTO to write programs was an acceptable model back in the 60's. They are acceptable because we did not have anything better to replace them.

Today, both C# and F# support a programming model that will do to our generation what structured programming, high-level languages and control flow primitives did to the developers in the 60's.

Sadly, many developers when they hear the word "C# async" immediately think "I have callbacks in my language already". Variations of this include "Promises are better", "Futures is the way to go", "Objective-C has blocks" and "the operating system provides that". All of these statements are made by people that have yet to study C# async or to grasp what it does.

This is my attempt at explaining why the C# async model is such a leap forward for developers.

Callbacks are a Band Aid

Callbacks have improved significantly over the years. In the pure C days, if you wanted to use callbacks, you would have to write code like this:

void cback (void *key, void *value, void *user_state)
{
	// My data is stored in the user_state, fetch it
	// in this case, just a simple int.

	int *sum = (int *) user_state;

	*sum = *sum + *(int *)value;
}

int sum_values (Hashtable *hash)
{
	int sum = 0;

	hash_table_foreach (hash, cback, &sum);
	return sum;
}

Developers would have to pass around these pointers to the state that they managed manually, which is just very cumbersome.

Today with languages that support lambdas, you can write code instead that can capture the state, so things like the above become:

int sum_values (Hashtable hash)
{
	int sum = 0;
	hash.foreach ((key, value) => { sum += value; });
	return sum;
}

Lambdas have made writing code a lot simpler, and now we see this on UI applications that use events/lambdas to react to user input and Javascript apps on the browser and the client that use callbacks to get their job done.

In Node.js's case the original idea was to scale a server by removing blocking operations and instead offering a pure callback-driven model. For desktop applications, often you want to chain operations "on response to a click, download a file, then uncompress it, then save it to the location specified by the user", all while interleaving some bits of user interface and background operation.

This leads to nested callbacks after callbacks, where each indentation level is executing at some point in the future. Some people refer to this as Callback Hell.

During this week preparation, Marco Arment happened to tweet this:

This is a common idiom. On our web site, when we launched Async, we shared this sample:

private void SnapAndPost ()
{
    Busy = true;
    UpdateUIStatus ("Taking a picture");
    var picker = new Xamarin.Media.MediaPicker ();
    var picTask = picker.TakePhotoAsync (new Xamarin.Media.StoreCameraMediaOptions ());
    picTask.ContinueWith ((picRetTask) => {
        InvokeOnMainThread (() => {
            if (picRetTask.IsCanceled) {
                Busy = false;
                UpdateUIStatus ("Canceled");
            } else {
                var tagsCtrl = new GetTagsUIViewController (picRetTask.Result.GetStream ());
                PresentViewController (tagsCtrl, true, () => {
                    UpdateUIStatus ("Submitting picture to server");
                    var uploadTask = new Task (() => {
                        return PostPicToService (picRetTask.Result.GetStream (), tagsCtrl.Tags);
                    });
                    uploadTask.ContinueWith ((uploadRetTask) => {
                        InvokeOnMainThread (() => {
                            Busy = false;
                            UpdateUIStatus (uploadRetTask.Result.Failed ? "Canceled" : "Success");
                        });
                    });
                    uploadTask.Start ();
                });
            }
        });
    });
}

The problem with these nested callbacks is that you can see very quickly that these are not code bases you want to be working with. Currently it does some basic error handling, but it does not even attempt to do some better error recovery.

Thinking about extending the above functionality makes me pause, perhaps there is something else I can do to avoid patching the above function?

And if I wanted to do better error recovery or implement a better workflow I can see myself annoyed at both the bookkeeping that I need to do, make sure that "Busy" value is property updated on every possible exit (and possible exits I add).

This is ugly to the point that your mind starts to wander "perhaps there is a new article on hacker news" or "did a new cat get posted on catoverflow.com?".

Also notice that in the sample above there is a context switching that takes place on every lambda: from background threads to foreground threads. You can imagine a real version of this function being both larger, getting more features and accumulating bugs in corner cases that were not easily visible.

And the above reminded me of Dijkstra's Go To Statement Considered Harmful. This is what Dijkstra's had to say in the late 60's about it:

For a number of years I have been familiar with the observation that the quality of programmers is a decreasing function of the density of go to statements in the programs they produce. More recently I discovered why the use of the go to statement has such disastrous effects, and I became convinced that the go to statement should be abolished from all "higher level" programming languages (i.e. everything except, perhaps, plain machine code). At that time I did not attach too much importance to this discovery; I now submit my considerations for publication because in very recent discussions in which the subject turned up, I have been urged to do so.

My first remark is that, although the programmer's activity ends when he has constructed a correct program, the process taking place under control of his program is the true subject matter of his activity, for it is this process that has to accomplish the desired effect; it is this process that in its dynamic behavior has to satisfy the desired specifications. Yet, once the program has been made, the "making" of the corresponding process is delegated to the machine.

My second remark is that our intellectual powers are rather geared to master static relations and that our powers to visualize processes evolving in time are relatively poorly developed. For that reason we should do (as wise programmers aware of our limitations) our utmost to shorten the conceptual gap between the static program and the dynamic process, to make the correspondence between the program (spread out in text space) and the process (spread out in time) as trivial as possible.

And this is exactly the kind of problem that we are facing with these nested callbacks that cross boundaries.

Just like in the Go To days, or the days of manual memory management, we are turning into glorified accountants. Check every code path for the proper state to be properly reset, updated, disposed, released.

Surely as a species we can do better than this.

And this is precisely where C# async (and F#) come in. Every time you put the word "await" in your program, the compiler interprets this as a point in your program where execution can be suspended while some background operation takes place. The instruction just in front of await becomes the place where execution resumes once the task has completed.

The above ugly sample, then becomes:

private async Task SnapAndPostAsync ()
{
    try {
        Busy = true;
        UpdateUIStatus ("Taking a picture");
        var picker = new Xamarin.Media.MediaPicker ();
        var mFile = await picker.TakePhotoAsync (new Xamarin.Media.StoreCameraMediaOptions ());
        var tagsCtrl = new GetTagsUIViewController (mFile.GetStream ());
        // Call new iOS await API
        await PresentViewControllerAsync (tagsCtrl, true);
        UpdateUIStatus ("Submitting picture to server");
        await PostPicToServiceAsync (mFile.GetStream (), tagsCtrl.Tags);
        UpdateUIStatus ("Success");
    } catch (OperationCanceledException) {
        UpdateUIStatus ("Canceled");
    } finally {
        Busy = false;
    }
}	

The compiler takes the above code and rewrites it for you. There is no longer a direct mapping between each line of code there to what the compiler produces. This is similar to what happens with C# iterators or even lambdas.

The above looks pretty linear. And now, I can see myself feeling pretty confident about changing the flow of execution. Perhaps using some conditional code that triggers different background processes, or using a loop to save the picture in various locations, or applying multiple filters at once. Jeremie has a nice post that happens to do this.

Notice that the handling of that annoying "Busy" flag is now centralized thanks to the finally clause. I can now guarantee that the variable is always properly updated, regardless of the code path in that program and the updates that take place to that code.

I have just delegated the bookkeeping to the compiler.

Async allows me to think about my software in the very basic terms that you would see in a flow-chart. Not as a collection of tightly coupled and messy processes with poorly defined interfaces.

Mind Liberation

The C# compiler infrastructure for Async is actually built on top of the Task primitive. This Task class is what people in other languages refer to as futures, promises or async. This is probably where the confusion comes from.

At this point, I consider all of these frameworks (including the one in .NET) just as lower-level plumbing.

Tasks encapsulate a unit of work and they have a few properties like their execution state, results (if completed), and exceptions or errors that might have been thrown. And there is a rich API used to combine tasks in interesting ways: wait for all, wait for some, combine multiple tasks in one and so on.

They are an important building block and they are a big improvement over rolling your own idioms and protocols, but they are not the means to liberate your mind. C# async is.

Frequently Asked Questions about Async

Today I had the chance to field a few questions, and I wanted to address those directly on my blog:

Q: Does async use a new thread for each operation?

A: When you use Async methods, the operation that you requested is encapsulated in a Task (or Task<T>) object. Some of these operations might require a separate thread to run, some might just queue an event for your runloop, some might use kernel asynchronous APIs with notifications. You do not really know what an Async method is using behind the scenes, that is something that the author of each user will pick.

Q: It seems like you no longer need to use InvokeOnMainThread when using Async, why?

A: When a task completes, the default is for execution to be resumed on the current synchronization context. This is a thread-local property that points to a specific implementation of a SynchronizationContext.

On iOS and Android, we setup a synchronization context on the UI thread that will ensure that code resumes execution on the main thread. Microsoft does the same for their platforms.

In addition, on iOS, we also have a DispatchQueue sync context, so by default if you start an await call on a Grand Central Dispatch queue, then execution is resumed on that queue.

You can of course customize this. Use SynchronizationContext and ConfigureAwait for this.

Finally, the PFX team has a good FAQ for Async and Await.

Async Resources

Here are some good places to learn more about Async:

Posted on 15 Aug 2013 by Miguel de Icaza

Richard Dawkins should revisit the letter to his 10 year old daughter

Like Richard Dawkins, I am also an atheist. I have also enjoyed his books and I am peripherally aware of his atheist advocacy.

Recently a letter Richard Dawkins wrote to his 10 year old daughter made the rounds.

He needs to ammend the letter and explain to her that it is not enough to find evidence, it is also important to be able to reason effectively about this evidence and avoid a series of logical pitfalls.

He failed to do this in a series of poorly thought out tweets, starting with this:

He followed up with a series of tweets to try to both explain the above as well as retweeting various people that came out in his defense with statements like:

I found the entire episode unbecoming of a scientist.

His original tweet, while true, does not have the effect of trying to advance our understanding of the world. It is at best a troll.

We expect from scientists to use the processes, techniques and tools of science math and logic to advance our understanding of the world, not resort to innuendo, fallacies and poor logical constructions to prove our points.

Correlation Does Not Imply Causation

Among others, I do not expect a scientist to imply that correlation implies causation. Which is what this tweet did.

Today he posted a large follow up where he explains what lead him to make this statement and also to selectively address some of the the criticism he received. He addressed the simpler criticism, but left out the meaty ones (you can find them on the replies to his tweet).

Dawkins failed to address the major problem with his tweet, which was exactly the use of correlation to imply causation.

Instead, he digs down deeper:

Twitter's 140 character limit always presents a tough challenge, but I tried to rise to it. Nobel Prizes are a pretty widely quoted, if not ideal, barometer of excellence in science. I thought about comparing the numbers of Nobel Prizes won by Jews (more than 120) and Muslims (ten if you count Peace Prizes, half that if you don't). This astonishing discrepancy is rendered the more dramatic when you consider the small size of the world's Jewish population. However, I decided against tweeting that comparison because it might seem unduly provocative (many Muslim "community leaders" are quite outspoken in their hatred of Jews) and I sought a more neutral comparison as more suitable to the potentially inflammable medium of Twitter. It is a remarkable fact that one Cambridge college, Trinity, has 32 Nobel Prizes to its credit. That's three times as many as the entire Muslim world even if you count Peace Prizes, six times as many if you don't. I dramatised the poverty of Muslim scientific achievement, and the contrast with their achievements in earlier centuries, in the following brief tweet: "All the world's Muslims have fewer Nobel Prizes than Trinity College, Cambridge. They did great things in the Middle Ages, though."

Now we know that Richard was not merely stating a couple of facts on his original tweet. He was trying to establish a relationship between a religion and scientific progress.

One possible explanation that does not involve Muslim-hood is that the majority of muslims live in impoverished nations (see map). Poverty and access to resources are likely bigger reasons for the lack of advancement in the sciences than belonging to a particular religion.

Is my theory better than Richard's? We could test the theory by looking at the list of Nobel laureates per country.

Let us consider my country, Mexico, a poor country compared to the wealth of the UK. We have twice as many people living in Mexico compared to the UK. Sadly, we only have three Nobel laureates vs Trinity College's thirty two.

If we expand the scope to Latin America which has half a billion people. Even with this, we can only muster sixteen laureates vs Trinity's 32.

Let us look into the African continent, with its billion people. They manage to only score 20 Nobel laureates.

And shockingly, the wealthier the nation, the more laureates. South Africa accounts for half of Africa's laureates (with ten), Egypt which has a better economy than most other African nations and gets tasty American aid gets five, which leaves another five for the rest of the continent.

If I had some axe to grind against Mexicans, Spanish speakers, Africans, Muslims, Bedouins, Brazilians, or Latin Americans I could probably make a statement as truthful as Richard's original tweet, which could be as offensive to those popuations and just like Richard prove absolutely nothing.

I think we have stronger evidence that access to wealth has an effect on how many people get this award than a religion.

The second flaw in his argument is to identify a University with access to funds, and a fertile ground for research and education to a group of people linked only by religion.

Perhaps people go to places like Trinity College becasue it is a fertile ground for research and education. If that is the case, then we have an explanation for why Trinity might have more Nobel laureates.

Luckily, Cesar Hidalgo's research on understanding prosperity shows what we intuitively know: that economic development clusters around existing centers. That is why actors, writers and directors move to LA, financiers move to New York and why companies ship their high-end phone manufacturing to China. You go where there is a fertile ground. Richard, instead of reading the long papers from Cesar, you might want to watch this 17 minute presentation he did at TEDx Boston.

So is Trinity one of these clusters? Can we find other clusters of research and expect them to have a high concentration of Nobel prize laureates? Let me pick two examples, MIT which is next door to my office has 78 laureates and I used to hang out at Berkeley because my mom graduated from there, and they have 22.

So we have three universities with 132 Nobel laureates.

The following statement is just as true as Richard's original tweet, and as pointless as his. Except I do not malign a religion:

All the world's companies have fewer Nobel Prizes than Universities do. Companies did great things in the Middle Ages though.

In fact there is a universe of different segments of the population that have fewer Nobel Prizes as Trinity. And every once in a while someone will try to make connections just like Richard did.

People will make their cases against groups of people based on language, race, sexual preferences, political orientation, food preferences, religion or even what video games they play.

We can not let poor logic cloud our judgement, no matter how importants our points are.

I agree with Richard that I want less religion in this world, and more science-based education. But if we are going to advocate for more science-based education, let us not resort to the very processes that are discredited by science to do so.

The Origins of the Tweet

We now know that Richard could just not stomach someone saying "Islamic science deserves enormous respect" and this is why he launched himself into this argument.

I can only guess that this happened because he was criticizing religion or Islam and someone told him "Actually, you are wrong about this, Islam contributed to X and Y" and he did not like his argument poked at.

The right answer is "You are correct, I did not consider that" and then try to incorporate this new knowledge into having a more nuanced position.

The answer is not to spread a meme based on a fallacy.

Posted on 09 Aug 2013 by Miguel de Icaza

CFKArgentina Live-Tweets Evo Morales Airplane Crisis

Last night Cristina Kirchner, Argentina's President, live tweeted the events around Evo Morales' European airplane hijacking:

  • I returned from la Rosada, Olivos 21:46 hs. They notify me, President Correa is on the phone. “Rafael? Put him on” [link]
  • “Hey Rafa, how are you?” He tells me he is angry and anguished, “You do not know what is happening?” [link]
  • “No, what is happening?”. I was in lala-land. Odd, because I am always paying attention... and viglant. I had just finished a meeting. [link]
  • “Cristina, they detained Evo with his airplane and they won’t let him leave Europe” [link]
  • “What? Evo? Evo Morales has been detained?” Immediately, I remember the last picture I saw with him, in Rusia [link]
  • Next to Putin, Nicolas Maduro and other Chiefs of State. “But what happened Rafael?” [link]
  • “Multiple countries revoked the flight permit and he is in Vienna”, he replies [link]
  • Definitely, everyone is crazy. A Chief of State and his airplane have total immunity. This level of impunity is unacceptable. [link]
  • Rafal tells me that he will call urgently a Ollanta Umala for an urgent meeting of UNASUR [link]
  • I call Evo. On the other line, his voice responds calmly “Hello friend, how are you doing?”. He asks me how I am doing? [link]
  • He has thousands of years of civilization more than me. He describes the situation. “I am here, in a small room in the airport...” [link]
  • “I am not going to let them inspect my airplane. I am not a thief”. Simply perfect. Strength Evo. [link]
  • CFK: “Let me call the Cancilleria. I want to see the jurisdiction, agreements and which court to go to. I’ll call you later”. “Thank you friend”. [link]
  • They confirm the absolute immunity by the right consuetudionario, embodied on the convention of 2004 and the Haye Court. [link]
  • If Austria does not let him leave, or wants to inspect his airplane, he can go to the international court of the Haya and ask.... [link]
  • Yes! AN INJUCTION. I dont’t know if I should laugh or cry. You realize what Injunctions are used for [link]
  • Well, if not, we can send a judge from here. MOther of God! What world! [link]
  • I call Evo again. His Minister of Defense takes note. In Austria it is 3am. They are going to try to reach the authorities [link]
  • I talk with Pepe (Mujica). He is outraged. He is right. The whole thing is very humiliating. Rafa calls me again [link]
  • Ollanta notifies me that he will call for a meeting of UNASUR. It is 00:25AM. Tomorrow will be a day long and hard. Calm. They wont succeed. [link]
Posted on 03 Jun 2013 by Miguel de Icaza
Older entries »
This is a personal web page. Things said here do not represent the position of my employer.