Some UWP and a little ClojureScript

I’ve been following Lucian Wischik’s interesting series of posts on writing Windows 10 applications in .NET. While I was on holiday he posted an article on writing NuGet packages for UWP but unfortunately the article was removed from the web site when I was only half a way through it. The place holder mentions that the NuGet team were going to talk about the issues and, sure enough, a post has now appeared on NuGet support for UWP in NuGet 3.1. In summary you will now be able to declare project references using a project.json (in much the same way as ASP.NET 5 projects). This will also use the transitive references idea, where you list only the top level projects that you require, and NuGet will take care of fetching their dependencies (and also resolve the appropriate versions when there are conflicts), in contrast to the current model where dependencies of dependencies make their way into your packages.config.

In other news, David Nolen has just announced on his blog that ClojureScript, the variant of Clojure that uses JavaScript as its target language, can now compile itself. Having been through the bootstrapping process myself for both ML and Common Lisp compilers, its always very satisfying when you can get rid of the other implementation, and finally get the compiler to work on itself. His post is rather cool, as it embeds some of the implementation of the translation process into the actual post, and you can run these in the browser that you are using to read the post. Some of this work sits on top of reader conditions, one of the two key features that were recently added to Clojure 1.7 (the other being transducers).

Finally, if Windows Update fails to install Windows 10 repeatedly), then this post has the answer, or rather it works for some people. For me, the relaunched Windows Update failed again, so in the end I needed to manually download by following this link.

Posted in Uncategorized | Leave a comment

C# await inside catch and finally leads to some interesting semantics

I remember when await was introduced into C# – the feature took us away from the callback hell that was developing, but it also required some understanding of the underlying code generation to really see what was going on in simple looking code. The abstraction led to a few rather confusing effects, such as always throwing the first exception of an AggregateException in the await.

At the time people were interested in why you couldn’t use await inside catch or finally blocks, and the answer always came down to confusing semantics. In the recently released C# 6, await is now available in catch and finally blocks, so the question is how this has been achieved without breaking the existing semantics. And I think the answer is that the semantics of some C# forms has now been changed in a way that again requires knowledge of the underlying transforms to understand.

Let’s take the async version of the standard thread abort construct. The call to Abort() notionally throws a ThreadAbortException which can be caught and processed by catch and finally blocks, but which has the interesting semantic of being re-raised when the processing block finishes (unless you reset the abort).

static async Task AsyncVersion()
Console.WriteLine(“Can’t stop me!!”);
Console.WriteLine(“After abort”);

We can explain the behavior of this code fairly easily. The Abort throws the ThreadAbortException, which is caught by the finally block. This finally block prints “Can’t stop me!!” and then the exception is rethrown when the finally block finishes. Hence the “After abort” is never printed.

Now change the finally block to

Console.WriteLine(“Can’t stop me!!”);
await Task.Run(() => Console.WriteLine(“Next”));

When you run this version of the code nothing is printed, and it’s hard to understand why without looking at the code generation. The async method is translated to a class that implements a state machine and, crucially, the finally is no longer a finally in the generated IL code, but is instead translated to a catch block that handles all exceptions.

Reflector shows us:

catch (object obj1)
obj2 = obj1;
this.<>s__1 = obj2;

ie the finally isn’t processed as a CLR finally, but is instead translated to a catch which re-enters the state machine processing. Of course, this means that the ThreadAbort exception which is rethrown at the end of any catch block is going to be rethrown too early, avoiding the exception of the code in the finally block.

I can see that it is a benefit to be able to await in catch and finally blocks, and I guess we’ve had to accept in the past that C# is not the assembly language of .NET. However, the code generation approach to implementing these high level constructs means that sometimes we can see through the abstraction as we can here. I’m not quite sure how important that is.

Posted in Uncategorized | Leave a comment

Advanced Topics In Types and Programing Languages

Advanced Topics in Types and Programming Languages  edited by Benjamin Pierce

I’d been meaning to read this book for a while, and managed to buy it with various present money after Xmas. It consists of ten chapters by different authors on ten topics ranging from type systems to proof-carrying code.

The first three chapters discuss various type systems – substructural type systems which control the use of variables in the type context and which lead to linear typing in one of the variants, dependent types where you are allowed to do calculations at the type level and which is being popularised by languages such as Idris, and a very interesting chapter on effect systems which discusses the region-based memory management work on Tofte and Talpin. In the latter work, the type of a program contains information about the allocation contexts which can allow the runtime to manage dynamic memory using a stack based approach.

The next two chapters cover the low level use of types. Typed assembly language would allow assembly language to be typed to check properties such as memory safety, and proof carrying code allows an executable to contain proof of its own safety and enough information for the safety to be checked by the target that is going to execute the code.

The next two chapters cover methods for reasoning about programs using their types, followed by a fantastic section on types for programming in the large. This section contains a chapter on the design considerations of the ML type system – this was very interesting. This section also contains a chapter on type definitions.

The last chapter is on type inference and discusses the typing of ML programs using constaint solving, rather than the usual unification based Hindley-Milner approach.

The book contains loads of interesting idea, but some of the theory is perhaps a little off-putting. If you really want to grips with the details, there are lots of exercises, or you can choose to simply skim these and still keep up with the exposition.

Posted in Uncategorized | Leave a comment

/dev/summer had some good talks as usual

It was /dev/summer at the weekend, where there were a few very interesting talks. Rather than going to the Clojure or Haskell tracks, this time I attended the double session on Go and a couple of sessions by  Gleb Bahmutov .

The Go session took a little while to get going, with the first 45 minutes hardly touching the language after some questions around the directory structure of a Go project on the disc. This meant that we didn’t get to see any large examples, thought the witty presentation and insightful answers to the questions led me to come away with basic Go knowledge that I can now try to put into practice.

The presentations by Gleb were full of good interesting ideas. The first presentation looked at the issues around npm modules and their dependencies (though the ideas applied more generally), and how semantic versioning isn’t typically supported by the modules. The clever idea of the presentation was a tool that tests semantic compatibility by running unit tests against the old and new versions of the module. Failures in the tests imply incompatibility and by having users send their results to a central server it is possible to check how safe a module upgrade is going to be. The second presentation looked at using code snippets inside Chrome to get various metrics on the page such as load time and render time, and also showed us how to combine this with the various profiling tools. Having not used these tools before, this was a great introduction to speeding up a web application.

I also attended the lightning talks which were a useful and informative set on general open source and browser testing.

It was a little strange to come across these talks. I’d just been reading about module dependencies in the .Net Nuget world after the publication of this post which talks about some changes to Nuget to make it support the new CoreClr world where the number of profiles is too great to simply name profiles using an integer value, and package authors will instead need to declare their dependencies. It also links to the bait and switch technique. I was also doing some interesting reading on the new WebAssembly technology and came across this great interview with Brendan Eich that explains things well.

Two other interesting blog posts. A talk on cloud scale event processing using Rx, which mentions some of the changes required to scale from the desktop version to a server based version – features needed included a way to checkpoint a query. This post on Rust’s type system is also interesting.

Posted in Uncategorized | Leave a comment

So what happened at //build

//build happened at the beginning of May, and I’ve been spending a fair amount of time lately working my way through the session videos that have been appearing on Channel 9. I’ll try to summarise the various videos that I have watched.

By far the most important video is the interview with Don Box. After his prominence in the COM and .NET communities he seemed to disappear off the radar some time ago. In this brief interview he tells us what he has been up to in the XBox team.

Roslyn makes it easy for users to write their own compiler extensions, and this talk pushes the idea of getting library writers producing compiler helpers to aid users with integration of their library into a product. There’s general discussion about Roslyn here. There’s a general state of the union talk on the .NET ecosystem, which shows how much innovation is happening in the .NET world at the moment. This talk discusses building cross-platform .NET.

The chat with Anders is also worth a watch

Visual Studio talks include an introduction to writing extensions, integrated Git in VS2015, some debugger tips and improvements in VS2015, and some methods for debugging performance problems. It’s not quite Visual Studio,  but there’s a deep dive into the cross platform code editor here.

There are some good videos on ASP.NET, introducing ASP.NET 5  and another on improving performance.

Moving on to Azure, the new reliable services are introduced here with a deep dive into reliable actors here. There are some new web site facilities on Azure covered here. There’s also a talk on some of the practical issues in developing efficient cloud applications.

I very much enjoyed this talk on the improved compiler technology Microsoft have been developing. They can use the same compiler backend to compile Objective-C as a means to extend a bridge to iPhone applications to bring them into the Windows platform. They have also got a bridge for Android applications.

JavaScript related talks include two on TypeScript, the first is an introduction followed by a discussion of the future direction. There is also a talk on WinJS changes. The  new Microsoft browser is covered here and here, and this talk discusses its new development tools.

With the success of Docker in the Linux world, Microsoft are keen on bringing it to Windows. The advantages of containers are covered in this talk. A walk through of producing a Docker application is interesting. These containers may well run on the new nano-server.

There one of two odds and ends. Continuum, using your phone as a normal computer was really interesting. Using other means of authentication was interesting and support of HTTP/2 were worth watching.

The mass of talks that I watched were on Universal Windows Apps, the progression of Metro style applications from the Windows 8 days. The idea now is that these applications are portable across devices that range from the phone to the large screen computer. The general platform is covered here, the app model is covered here and this covers their deployment. Microsoft is going to provide yet another bridge for covering installer based windows applications into windows universal applications. There is detail about the new lifecycle, the navigation model and app to app communication in this new world.

XAML plays a key role in implementing these applications, so there are many extensions to features like data binding as well as general XAML optimisation techniques. This talk discusses universal app development and this shows how they can be developed in VS2015. Publishing is also an important aspect. The design patterns for such apps  are supposed to make it possible to design an app that works on the small and large scales while remaining responsive. There are many talks on these issues including accessibility. Migrating an existing application was also covered.

All in all it was an interesting build. It was great to see more work on .NET and Roslyn, and the universal windows platform may turn out to be really interesting. Only time will tell.

Posted in Uncategorized | Leave a comment

Some low level locking papers

It’s been a bit quiet around here, as I have been making my way through 50 or so videos of talks from //build/. I’ll do a summary post when I get through them all.

In the meantime, I’ve also been reading a few low level papers. It’s quite interesting that hardware transactional memory is becoming more available, and so this paper on malloc placement is worth a read – the transactional memory uses the cache lines in the L1 and L2 caches to see if a value has been modified by another party while the transaction has been happening. This means that caches can be affected by memory patterns that place many values into locations which map to the same cache line.

On the same blog there were also good posts on the liveness benefits of LIFO lock patterns and implementations of spin-then-park locks.

This gitbook on the Linux kernel is also worth reading if you are interested on how the system gets from a reboot to a running kernel, and this paper describes the many techniques for x86 virtualization.

Posted in Computers and Internet | Leave a comment

Effective Modern C++

Effective Modern C++ by Scott Meyers

C++ has changed a lot over the years and this book covers loads of the subtle points around the newer additions to the language. As usual with the Scott Meyer books, the author takes a number (42 in this case) of items grouped into 8 categories and discusses the issues around the particular topic.

The topics covered include perfect forwarding and so-called universal references, smart pointers which after C++ 14 cover most of the memory management scenarios, lambda expressions and the related issues around variable capture (and whether to capture the variables by reference or by value), and a very interesting section on concurrency in the new C++.

As in his other books, some of the sections are a little overwhelming in technical detail, but you always come away with a better understanding of a topic than when you started reading the issue. A good read and a good introduction to modern effective C++.

Posted in Books | Leave a comment