Skip to content
Jan 26 12

Having fun with the Play! Framework

by Kevin Hoffman

Those of you who have read this blog for a while know that I’ve spent a considerable amount of time with ASP.NET. In fact, I’ve been using ASP.NET since version 1.0, written several books that involve ASP.NET (including ASP.NET 4.0 Unleashed with Nate Dudek), and have done a ton of work with ASP.NET MVC. I’ve also written a pile of applications in Ruby on Rails. I’ve even written an application with Groovy on Grails.

To say that I am a fan of MVC-based web application frameworks would be like saying that a Bugati Veyron is a “kinda fast” car.

So when I saw Play!, a Java-based (and Scala, but we’ll cover that in another post) MVC web application framework, I figured I’d give it a shot. I have to admit that I was a bit skeptical at first. The concept of quick, fast development in a fluid, agile style doesn’t exactly scream “Java”, but I was open to having my mind changed.

The first thing I noticed was that there are no class files. The “play” interactive shell (which I believe can be run as a service for production deployments) takes care of the live compilation for you whenever anything changes. Sweet! That is about as un-java as you can expect… I fully expected to have to run some obtuse Maven build every time I changed the color of a span on a page.

The structure smells very much like an ASP.NET MVC application. There’s an app folder and beneath that you have controllers, models, and views. Each controller class is just a Java class that inherits from a base controller and is responsible for rendering a page.

There’s a routing table that works very much like ASP.NET MVC’s internal routing tables but doesn’t require me to write code to generate routes like MVC, they’re in a text file like Ruby and Grails:

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /                                       Application.index

# Ignore favicon requests
GET     /favicon.ico                            404

# Map static resources from the /app/public folder to the /public path
GET     /public/                                staticDir:public

# Catch all
*       /{controller}/{action}                  {controller}.{action}

There’s a great deal of flexibility in this routes file that I haven’t covered. If you’re interested, head over to the Play! website and check the documentation and tutorials which are actually pretty good.

The Play! template language looks very similar to ASP.NET MVC as well, allowing you to blend HTML elements, function calls, variables, and more all in one fairly seamless HTML file. It’s not quite as concise as the ASP.NET MVC Razor syntax, but not as ugly as the old non-Razor syntax:

#{extends 'main.html' /}
#{set title:'Hello World' /}

Greetings, ${userName}<br/>

A view like this is made possible by calling render(userName) inside a controller. Note that unlike a property bag style usage from ASP.NET, I don’t have to give the userName variable a key – the view template knows that variable name implicitly. If I passed in a complex object such as user, then I could do things like ${user.name} in my template view.

The #{extends ‘main.html’ /} tag works very much like master pages or content place holders in the ASP.NET world. Main.html has some wrapper markup and then it identifies where extensions can take place. The hello.html content will appear wherever that content extension is indicated. You can get fairly advanced and have multiple content placeholders in a template, you can chain extensions like master page inheritance.

Finally, the other thing I liked about Play! is that it uses a built-in dependency management system for the framework itself but then resorts to Maven for resolving external dependencies. So, if my application depends on some other artifact that is floating around in a public nexus, I can just add a line like this to my dependencies.yml file:

require:
- play
- org.ektorp -> org.ektorp 1.2.1

When I run play dependencies from the command line, the dependencies are resolved, downloaded, and stored in my lib directly, along with all transitive dependencies.  To be honest, if I had to manage my own dependencies for an MVC framework, I would never have even made it to “hello world”.

Overall I’m fairly impressed with Play! and will be continuing to play around with it and see what it can do and how it might be limited (or superior) to other MVC frameworks I’ve used in the past.

Jan 13 12

Spacial Schemas and Cognitive Engineering

by Kevin Hoffman

In a previous post, I shared some of my initial thoughts on what appears to be a new, more rigorous discipline for designing interfaces called Cognitive Engineering. Cognitive Engineering is all about using information from many different areas of science to feed an engineering discipline designed to produce interfaces with very small impedance between a user’s mental model and the form and function of the application.

One of these many areas of science involves something called spatial schemas. Our minds are incredibly good at dealing with the concept of space. From the moment our senses first start being processed by our mind, our mind is constantly constructing spatial models. These spatial schemas can aid in everything from memory to locomotion to pathfinding and even to logic.

We take for granted how pervasive spatial schemas and referential spatial models are in our daily lives. For example, when we compare two abstract concepts we often do so by gesturing with one hand and then saying, “… on the other hand, …”. Without a built-in spatial schema in our mind to represent the difference between the left and right sides of our bodies, can you imagine that a comparison like this would be easy?

Another fascinating example of how spatial schemas help us is with models that have been so burned into our consciousness that we can actually compare stimuli against those models before performing the relatively slow step of cognition.

For example, if you’re a soldier (or play one in a video game) and someone near you shouts “on your 5!”. Your body will have already started the 150 degree turn necessary to turn to “your 5″. This happens in around 10 milliseconds whereas it takes over 300ms for a stimulus to make a round-trip through your conscious mind. Why is this possible?

It’s because the stuff that happens unconsciously (the high-performance part of your mind, the part of your mind that has spent hundreds of millions of years evolving to help you survive long enough to continue the species) can do a rapid comparison against a retained spatial schema… that of an analog clock.

Our brains are actually quite good at determining direction (angles) and even if we don’t know how or why, we are aware of subtle differences in angles even down to 1 degree. So when someone shouts “on your 5″ the high-speed portion of your brain is already encoding the mapping between 5 on an analog clock to the 150 degree turn into signals to send to your muscles to accomplish this… and it does this all before your conscious mind has processed that because from an evolutionary standpoint, if your conscious mind was responsible for mapping spatial schemas to survive, you’d be dead. It takes less than 300ms for an animal to jump out “from your 5″ and start mauling your dumb, hairless butt.

So what does this have to do with interface design?

It seems obvious when you think about it but you need something (like this informative, albeit dry, textbook) to get you thinking about it. Knowing how the brain constructs spatial models and knowing how these spatial models aid memory, logic, reasoning, and comparison will help you build interfaces that take advantage of this.

Interfaces built this way can “cheat” … if you differentiate things spatially, a user won’t have to waste valuable cognitive time processing that differentiation. For example, if a user sees “A > B” on the screen, that little blurb has to go all the way through to their slow brain (cognition) to figure out that A is greater than B, which will create a spatial model in their head representing the two objects.

How much work do you think your brain has to do if object A appears physically higher on the screen relative to B? None. That display model maps exactly to the spatial model that the brain would have constructed as a result of the slow round-trip to parse “A>B”.

What if one object is bigger than another one, visually? What if one object appears far to the left while others appear far to the right? What if an object is rotated ever-so-slightly? All of these spatial representations of entities can be designed to map the spatial model that we expect a human brain to create from more obtuse, less informative interfaces … so why not save the user the effort and create that model ahead of time, right there in the interface.

Assign meaning to space, size, spatial relationships, directionality, curvature, velocity … all of these things can be used to create amazing interfaces, not because they will be pretty or shiny, but because they will not slow the user down and will not force their minds to do work they don’t need to be doing.

Anyway, as I said in the other blog post I haven’t yet really tried to use any of these techniques to build user interfaces but I have seen the results of similarly engineered interfaces and it is really powerful stuff.  Hopefully reading this blog has made you think a little bit more about the role interfaces play and how they interact at a chemical, biological, neurological level with our users.

Jan 13 12

Stopping SOPA and PIPA

by Kevin Hoffman

If you are unfamiliar with the bills coming up for vote soon, SOPA and PIPA, then check out this blog post over at WordPress, which also contains an excellent video describing the situation.

SOPA, the Stop Online Piracy Act, and PIPA, the Protect-IP Act, have fairly innocent motivations, or at least they appear to. On the surface, it looks like any number of other bills introduced to stop people from pirating movies and downloading illegally obtained, copyrighted material from the Internet.

The problem isn’t the motivation, the problem is the implementation. Hollywood is pissed that people are downloading their stuff for free and that money isn’t going into their hands. Of course, Hollywood isn’t looking at the numbers showing people still paying for their crap just like always. Remember, Hollywood also tried to ban the sale of VCRs before they realized they could make bucketloads of cash on VHS sales.

Saying that the US needs to be able to filter and censor the Internet in order to stop online piracy is like the local police force banning crowbars because they are occasionally chosen by criminals as a way to break into someone’s house.

The implementation of this legislation is in the hands of people who are out of touch with the group of people who would be harmed by this legislation. The wording is loose, ambiguous, and open to interpretation by people who are not technologists, which means it might start out accomplishing its goal but can quickly be morphed into something far worse.

If the entertainment industry is pissed off that people are pirating their music because DRM implementations suck, then it is the entertainment industry’s job to innovate their way into the 21st century and find a way to engage customers through the Internet and many, many companies are doing that now (look at how many people gladly shell out monthly subscriptions to Hulu and Netflix for streaming content). The entertainment industry, who contributes very little to the overall economy – especially over the Internet – has no business getting the government to censor the Internet.

I don’t normally do politics on this blog and when I do, I generally stay on the fence but I feel very strongly that SOPA and PIPA, if passed, will quickly grow to do far more harm than good.

So, follow the link at the bottom of that WordPress article, figure out who you need to call or e-mail and make your voice heard. These people work for us not the other way around, make them do their jobs and vote the way their constituents want them to.

Jan 12 12

First Impressions of Cognitive Engineering

by Kevin Hoffman

This week I’ve been receiving some training in what appears to be the relatively new field of Cognitive Engineering. This training comes from Brad Paley of didi.com.

Hopefully all of us computer professionals are aware of the importance of user interface. However, many designers confuse “prettiness” with “usefulness” and many developers think that a simple grid view or spreadsheet should suffice because rows and columns are a perfectly acceptable model for that data.

I’ve known for quite some time now that spreadsheets are an evil abomination and force our brains to do things that they should not have to do for 8 hours a day but haven’t really had the tools, machinery, or vocabulary to articulate why. Thanks to this class, I believe I now have the vocabulary to articulate why one interface design is better than another.

The core of this discipline is the notion that an expert at some particular job function has a mental model of the entities and tasks involved in accomplishing their job. It is our responsibility as designers using Cognitive Engineering to extract their mental model and use techniques and information from a half dozen fields of science completely unrelated to computers in order to produce a design that provides a user experience model that has little to no impedance between their mental model.

To summarize, the user should be able to look through their user interface and the widgets with which they interact on screen should not only come as close as possible to their mental model but, perhaps more importantly, the interface should be designed in such a way that as much pre-cognitive processing as possible can be done by the user.

We know which tasks take the brain a long time to do. We know which tasks increase cognitive load. We know what things human cognition, the human eye, our ears, our spatial processing system are all adept at. The key to this discipline is using all of that information to guide the design process. If we can design an interface such that a user can rapidly categorize, sort, associate, and parse most of it in the 10s of milliseconds of time the brain spends before involving conscious thought, then users will be able to plow through their job tasks with insane productivity using software designed this way.

They will feel less fatigued after using software designed this way and probably won’t even know why. They should be able to sit in front of this software and because they are an expert in their domain, the screen should so closely resemble their mental model of their job that they should be able to just work.

They won’t be thinking about clicking, mouse dragging, typing, searching, or right-clicking. They’ll be thinking about completing orders, shipping boxes, canning tuna – the software disappears in service of their job.

At least, that’s the promise of this discipline. I’ve seen much of this thinking in Cooper’s Interaction Design field and there is a lot of overlap here.  The focus of cognitive engineering is really on the physical, chemical mechanics involved in the brain’s information processing and reaction capabilities and using that science to inform design.

I haven’t yet used this discipline to produce a UI by extracting an expert’s mental model yet but, for the first time in a long time, I can’t wait to try this out and produce a design. It’s a rare thing that a few quick sessions can inspire this much interest in me but this has and only time and experience will tell if it produces a better product.

Jan 10 12

Using the Disruptor to Power an MMORPG

by Kevin Hoffman

In my previous blog post, I introduced the two remaining readers of this blog to a concept and an implementation called the Disruptor, a high-performance ringbuffer-based event pipelining system that can be used to get huge performance boosts to event processing provided you don’t write foolish code.

It takes a while for most people to get used to the idea of a Disruptor, especially when just about every sample I’ve seen for the Disruptor has been related to the financial domain. Those who know me will be well aware of the fact that as I approach a new technology, I typically ask, “Yeah, that’s nice, but can you game with it?

Turns out, the Disruptor can be used to game. Well, it can be used to write a game server for an MMORPG :) BTW, for those who don’t know, MMORPG is Massively Multiplayer Online Roleplaying Game, and examplars of its ilk are World of Warcraft, Star Wars the Old Republic, Guild Wars, etc.

First, let’s take a look at the problem that MMORPG server writers need to solve. Each server is typically running a bunch of individual processes. Each of these processes is responsible for handling a portion of the game’s traffic. For example, let’s say you’re in a castle on server kittenmaul. In theory, there might be a process dedicated to handling all of the messages that come in related to kittenmaul‘s Castle zone. These messages are usually a mix of TCP (guaranteed to arrive) and UDP (fire and forget). TCP sends the slow but important traffic whereas UDP sends the fast but non-mission-critical data like player positions. This flood of traffic assaults the server with messages like “player X fired a magic missile at player Y” and “player X is now at location -1,23,21″ and so on.  The server then needs to grab the important bits out of the message, make changes to in-memory state (probably by locking that portion of memory), and then free up processing threads or risk the dreaded zone lag where message processing server-side gets so slow that missiles travel through walls, trolls get stuck in trees, and players die hideous, blood-soaked deaths.

The pipeline to process these messages is probably a multi-threaded tangle of spaghetti that only a single Godlike hacker can decipher and he likes it that way. Buried deep (and I do mean DEEP) within the tangled morass of threading code is intelligence that reveals which messages can be processed in parallel and which ones need to be processed sequentially… if you’ve been vigilant. Chances are, that information is impenetrable and everything is wrapped in locks and critical sections and it’s impossible to tell what the real processing pipeline looks like without external documentation (and we all know how reliable that is 8 months after release).

Wouldn’t it be awesome if there was a way to represent the processing pipeline of messages, create simple, easy-to-maintain, small event handlers and have that be how the castle zone server maintains its pile of in-memory state? The Disruptor might actually help us out here!

For the sake of simplicity, let’s tackle just one small pipeline: spell firing. Without getting into too much detail on the division of roles between the MMO client and the server, let’s assume that the client sends the server messages that look like “player X fired spell Y at player Z” (having first passed some sort of sanity check to ensure that such an action is possible, which will be re-checked on the server as an anti-hacking measure).

Our kittenmaul server is actually running with a hot backup instance in case of failure. Each time we get a message on the live server, we replicate that message to the hot backup. We don’t want this replication to slow us down, so we need to do that “in the background” as it were. Another thing we can do in the background is journal the message to disk. We do this so that if the server goes down we can replay those messages from disk back into the server to restore the state to exactly where we left off (when was the last time you saw an MMORPG with that feature??). Once the logging and replication of the message are done, we are free to act on it by resolving the amount of damage done to the target player. Once we have the amount of damage done, we can then ship off an outbound message to the attacker and attackee’s clients containing the amount of damage done.

Those of you who have written traditional multi-threading code are probably breaking out in a cold sweat right about now. But, what if we could represent this in terms of an input disruptor and an output disruptor that create a super-fast processing pipeline?

Check out the following code:

ExecutorService executor = Executors.newFixedThreadPool(NUM_EVENT_PROCESSORS);

Disruptor<SpellFiredEvent> spellsDisruptor =
   new Disruptor<SpellFiredEvent>(SpellFiredEvent.EVENT_FACTORY, executor,
               new SingleThreadedClaimStrategy(RING_SIZE),
               new SleepingWaitStrategy());
spellsDisruptor.handleEventsWith(spellFiredLogger, spellFiredReplicator);
spellsDisruptor.after(spellFiredLogger, spellFiredReplicator)
               .handleEventsWith(spellFiredDamageResolver);

spellsRingBuffer = spellsDisruptor.start();

Disruptor<DamageDoneEvent> damageDoneEventDisruptor =
           new Disruptor<DamageDoneEvent>(DamageDoneEvent.EVENT_FACTORY, executor,
           new SingleThreadedClaimStrategy(RING_SIZE),
           new SleepingWaitStrategy());
damageDoneEventDisruptor.handleEventsWith(damageDoneHandler);

damageOutputRingBuffer = damageDoneEventDisruptor.start();

The expressive Disruptor DSL lets us fluently describe the activities that can take place in parallel and the ones which rely on the processing of previous events. In the preceding code, it is drop-dead obvious that both the logging and the replicating handlers must complete before we can resolve damage but logging and replication can occur at the same time.

And now in the damage resolution handler, we can dump the outbound damage done events to the output disruptor, which, in a real-world sample, would have down-stream handlers responsible for plucking messages off, converting them into UDP or TCP, and delivering them to clients:

private final static EventHandler<SpellFiredEvent> spellFiredDamageResolver =
        new EventHandler<SpellFiredEvent>() {
            public void onEvent(SpellFiredEvent spellFiredEvent, long l, boolean b)
                                  throws Exception {
                System.out.printf("Resolving damage to %s via spell %d%n",
                   spellFiredEvent.getTargetId(), spellFiredEvent.getSpellId());
                handleCount++;
                long sequence = damageOutputRingBuffer.next();
                DamageDoneEvent event = damageOutputRingBuffer.get(sequence);
                event.setTargetId(spellFiredEvent.getTargetId());
                event.setDamage(5000); // OUCH!
                damageOutputRingBuffer.publish(sequence);
            }
        };

So, the moral of the story here kids is that Disruptors aren’t just for financial types. A second moral of the story is go grab yourself the Disruptor code and write your own MMORPG server :)

Jan 6 12

Hello Disruptor

by Kevin Hoffman

Before you read this post, I highly recommend you check out some of the other material available on the web regarding the Disruptor. I would start with the overview and then check out Martin Fowler’s review. If you’re already familiar with the Disruptor or you feel like continuing anyway, then keep reading.

Historically multi-threaded, or concurrent, applications have been extremely difficult to write and even harder to troubleshoot and maintain. They are tough to test in isolation and developers spend more time reasoning about semaphores, locks, critical sections, and re-entrancy than they do about the actual business problem their application is supposed to tackle.

The Disruptor aims to change that by creating a lightweight, easy-to-use component that allows developers to pump events through a pipeline that never creates locks and doesn’t incur the threading penalties of context switching. Some of the other optimizations the LMAX folks have made under the hood also tweak things for “mechanical sympathy”, aiming to maximize your application’s use of CPU caching to dramatically speed up processing.

With this in mind, I set out to try and create a simple “Hello world” application using the Disruptor. Really the the simplest way to do this is to create an application that has one event publisher and one event handler and you just pump hello-world messages into the Disruptor and see what happens.

This is exactly what I did.

To start, I created an event that I wanted to stream through my application, presumably to eventually be handled by some downstream business logic. Here’s the ValueEvent class, of which 99% is stolen from the LMAX documentation:

package com.kotancode.hellodisruptor;

import com.lmax.disruptor.*;

public final class ValueEvent {
    private long _value;

    public long getValue() {
	return _value;
    }

    public void setValue(long value) {
	_value = value;
    }

    public final static EventFactory<ValueEvent> EVENT_FACTORY =
        new EventFactory<ValueEvent>()
        {
            public ValueEvent newInstance() {
  	        return new ValueEvent();
            }
        };
}

Don’t worry too much about the event factory – it’s there to allow the ring buffer (you’ll see that shortly) to create new events of that type. Once we’ve got an event, we should be able to create an application that configures a disruptor pipeline, pumps messages into it, and handles the messages. In the sample code below, I am counting the number of messages I handle and then reporting on the time it took to handle them in terms of operations per second (in my case the operation is a simple increment done when I handle the event).

package com.kotancode.hellodisruptor;

import com.lmax.disruptor.*;
import com.lmax.disruptor.dsl.*;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class App {

    private final static int RING_SIZE = 1024 * 8;

    private static long handleCount = 0;

    private final static long ITERATIONS = 1000L * 1000L * 300L;
    private final static int NUM_EVENT_PROCESSORS = 8;

    private final static EventHandler<ValueEvent> handler =
        new EventHandler<ValueEvent>() {
	    public void onEvent(final ValueEvent event,
                                final long sequence,
                                final boolean endOfBatch) throws Exception {
		handleCount++;
	}
    };

    public static void main(String[] args) {
	System.out.println("Starting disruptor app.");

	ExecutorService executor = Executors.newFixedThreadPool(NUM_EVENT_PROCESSORS);

	Disruptor<ValueEvent> disruptor =
		new Disruptor<ValueEvent>(ValueEvent.EVENT_FACTORY, executor,
			new SingleThreadedClaimStrategy(RING_SIZE),
			new SleepingWaitStrategy());
	disruptor.handleEventsWith(handler);
	RingBuffer<ValueEvent> ringBuffer = disruptor.start();

	long start = System.currentTimeMillis();

        long sequence;
        ValueEvent event;
	for (long x=0; x<ITERATIONS; x++) {
	    sequence = ringBuffer.next();
	    event = ringBuffer.get(sequence);
  	    event.setValue(x);
	    ringBuffer.publish(sequence);
        }
	final long expectedSequence = ringBuffer.getCursor();

	while (handleCount < expectedSequence) { }

	long opsPerSecond = (ITERATIONS * 1000L) / (System.currentTimeMillis() - start);
	System.out.printf("op/s: %d, handled: %d", opsPerSecond, handleCount);
    }
}

I’m not going to go into a huge amount of detail here because a lot of this information is described on LMAX’s site and made a little more clearer after you read Martin Fowler’s review of the Disruptor technology.

There are a couple of fairly important things to note here. The first is that you’ll notice that when I want to publish an event, I don’t create a new instance of an event. Instead, I ask the ring buffer for it. This is because the ring buffer is highly optimized for performance. If I have a ring buffer with a “ring size” of 2048 (must be an even number), then I will consume a maximum of 2048 instances of ValueEvent. Each time I grab one of these off the ring, modify it, and then publish I am re-using an existing instance. There will never be more than (ringsize) instances of an event object.

The ExecutorService business is actually part of the standard Java concurrency libraries and gives me a strategy for how I want the disruptor to manage it’s workload. In this case, I’ve created a fixed thread pool size of 8 (just because I felt like it, not because it has any significant meaning) and that thread pool will be responsible for executing the event handlers that are pulling from the Disruptor that you register with the handleEventsWith() method.

This is just the tip of the iceberg. As you’ll see in a subsequent blog post, you can set up elaborate processing pipelines that give you real, visual, concrete visibility into how events are being processed asynchronously in your application without incurring the typical multi-threading penalties.

If you’re curious, I typically get between 8 million and 21 million operations (handle + increment) per second using this sample code. I’m guessing the fluctuation is related to what my PC is doing at the time but I haven’t been able to reliably get it to stay around the 20+ million mark.

So, if you’re building an application that might even remotely be considered multi-threaded or, better yet, considered an event or message processor, then I highly recommend you go out and play with the Disruptor and I promise I’ll have an even more fun example of using Disruptors in the next blog post.

Jan 6 12

Getting Injected with Guice

by Kevin Hoffman

In the past I have worked with several different tools that allow for Dependency Injection for .NET such as Spring.NET, Castle Windsor, Ninject, and of course Microsoft’s own Unity. It seems like every time a .NET developer gets bored, their pet project ends up being the creation of a new IoC container.

At its core, Dependency Injection is a pattern where developers write code against interfaces and then specific implementations of those interfaces are injected at runtime either by file-based configuration, code-based configuration, or some combination of convention and configuration.

For example, I might have written my application to use a repository called SmurfRepository, which I then use to store and retrieve smurfs. If this was a .NET application then I might have written all of my code to work against an interface called ISmurfRepository. I then allow the Inversion of Control container to inject an implementation of ISmurfRepository at runtime. This lets me do things like run against an in-memory smurf repository while I’m in testing mode and a SQL-backed Smurf repository when running in production mode.

Google wouldn’t be Google if they didn’t have their own library that did something that’s already been done before, so they have Guice. The value proposition for Guice is the same as it is with many Google libraries – it’s just brain-dead simple to use. If you’ve ever used Spring or Spring.NET then you know what bloated, clunky, convoluted dependency injection looks like. Guice is the anti-Spring, and that’s a good thing.

So let’s see what Guice looks like in action. First, we need to create our application code that works against interfaces so let’s create a class called ChessPiece:

public class ChessPiece {
    private GameBoard _gameBoard;

    @Inject
    public ChessPiece(GameBoard gameBoard) {
        _gameBoard = gameBoard;
    }

    // real implementation goes here.
}

This pattern should look fairly familiar to developers who have used DI before. The chess piece needs a reference to the game board on which it sits and it gets that reference at construction time. The interesting bit here is the @Inject attribute (I’m using the Guice version, not the javax version, since the Guice one is a bit more flexible). This annotation tells Guice that it can invoke this constructor with an injected implementation when some object requests and instance of this class. In the above code, GameBoard is an interface (e.g. you’d call it IGameBoard in .NET).

We might have multiple different classes that implement GameBoard such as one for unit testing, another for integration testing, and maybe one for production. We could also have the game boards differ in terms of creating a local game board (all players are in-process) or a networked game board (player 1 is local and player 2 is communicating with the game board via network messages). The purpose of this pattern is that it shouldn’t matter to consumers of the GameBoard interface which implementation has been injected at runtime.

To get an instance of the ChessPiece class, we just ask the injector for it:

MultiplayerModule module = new MultiplayerModule();
Injector injector = Guice.createInjector(module);
ChessPiece piece = injector.getInstance(ChessPiece.class);

Once you get a real object instance in the piece variable, Guice will have already located, instantiated, and supplied a concrete implementation of GameBoard and given it to the chess piece’s constructor. The bindings that tell Guice which implementation to use for a given interface can be done via Guice modules, like this one for a multiplayer environment:

public class MultiplayerModule extends AbstractModule {
    @Override
    public void configure() {
        bind(GameBoard.class).to(TCPNetworkMultiplayerGameBoard.class);
    }
}

I really like the way modules allow us to group a bunch of bindings together into a logical unit and that logical unit is a piece of code rather than a 5 bajillion line XML file. The code you see here (aside from the imports that I’ve snipped out) is all you need. There are no configuration files, no massive setup times, no rigging, just write your code against interfaces, mark your constructors, setters, and methods as injectable, and Bob’s your uncle.

As you can see, if there is one Dependency Injection library that exemplifies Kotan (elegant simplicity), then Guice is it.

Jan 3 12

Latency vs Throughput

by Kevin Hoffman

Lately I’ve been surrounded by discussions involving things such as high performance and people asking questions like “Well, how fast is it?” Not too long ago, I would’ve been inclined to simply answer that question but it’s really not that simple.

The new answer is, “That depends on what you want.” You might be ready to throw me to the wolves but bear with me, both enlightening analogy and video game screenshots are coming forthwith.

This brings me to two words I’ve been hearing an awful lot of lately: latency and throughput.

Latency, from a network standpoint, is a measure of delay or how long it takes for something to start in one place and arrive in another. Throughput, in this context, can be thought of as the amount of “stuff” processed within a given period of time. So, if we’re on a network talking about messages then throughput can be measured in messages over time whereas latency might be the time it takes for a single message to be sent and delivered.

The reason I wanted to write this blog post is that “fast” is actually a fairly nebulous term. You can have a system where it takes very little time to deliver a message but the number of messages processed over time is actually pretty poor and vise versa. The interesting thing is that quite often people have to make decisions or tune systems where the smallest latency isn’t always preferable because sometimes it doesn’t offer the maximum throughput.

Take, for example, the concept of playing a real-time strategy game like one of my favorites, Warcraft 3. In this game, playing as orcs, you have a resource such as gold. This resource comes from gold mines and you have to send little orc miners out to harvest this resource.

Orcs Mining Gold in Warcraft 3

Orcs Mining Gold in Warcraft 3

What’s interesting here is how closely this maps to network application performance. If you have one orc miner on your team, and he can carry 10 gold per trip and a trip takes 20 seconds to a particular gold mine, you have a latency of 40 seconds (the orc has to go out to fetch the gold and then come back). Your throughput here is the amount of resource processing over time that can be done, e.g. gold per minute. 10 gold every 40 seconds is .25 gold per second or 15 gold per minute.

More Orcs pulling gold from a mine

More Orcs pulling gold from a mine

So now you decide that 15 gold per minute just plain sucks. You need more cash to fund your world domination plans, so you spawn new orc miners. Let’s say you create 10 of them. Now you still have a latency of 40 seconds but your throughput is now 150 gold per minute, assuming the mine can sustain that kind of load.

This isn’t part of the actual game but, for the sake of argument now assume that there’s only room for 4 miners at a time inside the mine. At a latency of 40 seconds (determined by the walking speed of the orcs and distance to the mine), at any given time you’re now going to have 6 idle orcs outside the mine.

Now you have a very real-world situation where no matter how many more orcs you throw at this mine, your throughput stays the same. If the mine is closer to your base, then the waiting orcs spend less time getting to and from the mine but really, do you want those guys idle or would they be better spent making a longer walk to a more distant mine to avoid the cost of leaving them idle?

All this babble about orcs and mines and gold really boils down to the core point of this blog post: The next time someone asks you how fast your system is, resist the urge to give them an answer. Instead, ask them what they want to know – throughput or latency. Or better yet, give them three values – latency/throughput at max latency, latency/throughput at max throughput, and some number in the middle.

Hidden beneath that question is the search for the “sweet spot” where all your resources are maximized in their pursuit of speed and you’re probably giving up some latency in order to push more resources through the system.

The second point of this blog post: Everything I know about network optimization I learned from playing resource-oriented strategy games.

Dec 19 11

Type Erasure in Java

by Kevin Hoffman

This morning I ran into something that most C# developers never think about – type erasure. Many .NET developers who have written code that either utilizes generic classes or is a generic class don’t really know about type erasure, nor do they care. It’s perfectly acceptable for a C# developer not to care about type erasure.

However, when you have a C# background and you start attempting to write generics code in Java, type erasure will almost certainly rear its ugly head and it will probably make you scratch your head (or even slam that head on your desk repeatedy).

When C# implemented Generics, it did so atop a brand new CLR. When Java implemented generics, it did so under the false presumption that they were not allowed to change the compiled class file format. This causes a couple of wrinkles, including:

  • You can’t have a non-generic class Bob and a generic class Bob<M> in the same package. These two end up being the exact same type. C# programmers aren’t used to this restriction.
  • Generic methods retain the same name. This is the one that bit me this morning, and the focus of the code sample I’m about to show. This means you cannot overload based on type parameters to generic method arguments.
  • Even though you don’t see the runtime typecasting, it’s being done for you by the JVM. This means that, unlike C# generics which actually give us typecast-free performance, Java generics confer no performance improvement over their non-generic counterparts.

So, let’s take a look at some method overloading that I thought should work just fine:

public void doSomething(Sword<Air> weapon) { ... }
public void doSomething(Sword<Earth> weapon) { ... }
public void doSomething(Sword<Fire> weapon) { ... }

I thought that depending on the type binding of the incoming argument to the doSomething method, the JVM would happily choose the most appropriate method for me and allow me to have separate implementations. As it turns out, this code won’t even compile.

What it does is tell me that the various arguments to the overload of doSomething all “have the same erasure“. This means that after the Java compiler has happily stripped the generic type parameter from all of the methods in my code, it can no longer tell the difference between method signatures. UGH.

There are some elegant (and some not-so-elegant) ways around this problem, but the bottom line is that any C# developer who has somehow been dipped into the pool of Java absolutely needs to know what type erasure is and how it affects Java generics and related code.

Dec 9 11

Java Initializers vs C# Constructors

by Kevin Hoffman

Today I had yet another opportunity to discover and interesting (and unexpected) difference between C# and Java. I was looking through some code and noticed a bizarre construct – some code sitting within a pair of curly braces that had no method name.

I’ve seen this kind of thing before and C# has support for anonymous methods and even anonymous classes and I even knew that Java supported similar things. I thought maybe it was a shortcut anonymous method (which it is, in a way), but as it turned out it was something more interesting – a static initializer.

Java has two different paradigms that allow developers to perform initialization code whereas C# only supports one. Both languages support the notion of constructors but Java also supports something a little different called an initializer. This code is actually invoked before the constructor. Technically it is invoked at the time the class is loaded. This can become a really important thing to be aware of, especially if you’re dynamically loading the class or you’re referencing it via something like MyClass.class, which is a pattern that should be familiar to Objective-C developers used to doing things like [MyClass class] (in C# we might do something like typeof(MyClass)).

Anyway, Java doesn’t support the notion of a static constructor like C# does, so the only way to initialize static fields or to perform other initialization code at class load (as opposed to instance load) time is with a static initializer. Java also supports an instance initializer which is actually fired before the classes constructor. This actually makes for really elegant looking code if there is initialization that needs to be done before any of a number of chained constructors needs to be invoked.

To demonstrate what these initializers look like (and the order in which they are executed), consider the following class:


public class Monster {

	private int localInt = 21;
	private static int staticInt = 225;

	// this will look crazy to a C# dev...
	// Instance Initializer
	{
		System.out.printf("I'm in an initializer: %d, %d%n",
				localInt,
				staticInt);
		localInt = 23;
		staticInt = 80;
	}

	// STATIC initializer
	static {
		System.out.printf("It's statically delicious: %d%n",
				staticInt);
		staticInt = 500;
	}

	public Monster() {
		System.out.printf("Monster: Constructed %d, %d%n",
				localInt,
				staticInt);
	}
}

When this code executes, it prints the following:

It’s statically delicious: 225
I’m in an initializer: 21, 500
Monster: Constructed 23, 80
Built a monster.

As I said in the previous post about Java, I’m sure this is old hat to you veteran Java developers but for me, it’s something else I didn’t know about Java that I simply never knew because I hadn’t spent the time examining the language to find out.