Thursday, December 13, 2012

Passing of an era: No more i386

Thus spake Linus Torvalds:

Pull "Nuke 386-DX/SX support" from Ingo Molnar:
 "This tree removes ancient-386-CPUs support and thus zaps quite a bit
  of complexity:

    24 files changed, 56 insertions(+), 425 deletions(-)

  ... which complexity has plagued us with extra work whenever we wanted
  to change SMP primitives, for years.

  Unfortunately there's a nostalgic cost: your old original 386 DX33
  system from early 1991 won't be able to boot modern Linux kernels
  anymore.  Sniff."

I'm not sentimental.  Good riddance.

Tuesday, December 04, 2012

Guicing your jars, part 2

In Guicing your jars I mention using Java's service loader feature to automate finding modules for Guice, along these lines:

public class MetaInfServicesModule
        extends AbstractModule {
    @Override
    protected void configure() {
        for (final Module module : ServiceLoader.load(Module.class))
            install(module);
    }
}

What I did not mention was the secret sauce, Kohsuke's MetaInfServices annotation processor which creates the META-INF/services file for you:

@MetaInfServices(Module.class)
public class FooModule
        extends AbstractModule {
    @Override
    protected void configure() {
        // Awesome foo-ness here
    }
}

Now using your Maven runtime dependencies to control the classpath, Guice automatically installs your modules:

Guice.createInjector(new MetaInfServicesModule());

(Dependencies between modules is beyond the scope of this post!)

Coming in Kernel 3.7 from The H-Open

  1. Filesystems & storage
  2. Networking
  3. Infrastructure
  4. Drivers
  5. CPU and platform code

Friday, November 30, 2012

WebPageTest

Ryan Hurst posts How Facebook can avoid losing $100M in revenue when they switch to always-on SSL, an interesting discussion of web page performance when using SSL.

What I noticed: WebPageTest, a fantastic resource for tuning public web pages courtesy AOL, Google & others. WebPageTest can also be downloaded, for testing your internal web site.

Sunday, November 25, 2012

Monday, November 19, 2012

Alternative to Java 8 virtual extension methods

Tired of waiting for Java 8? Project Lombok brings a little piece to you today, virtual extension methods similar to those proposed for JDK8. The most interesting bit: Lombok works using only JDK annotation processors and no required run-time library.

JUnit testing that a call blocks

Certain that I missed an existing solution, I post this in hopes of helping anyone who also misses it. But if you know a better way, please comment.

Usage

class SomeTest {
    @Test(timeout = 1000L)
    public void shouldBlock() {
        assertBlocks(new BlockingCall() {
            @Override
            public void call()
                    throws InterruptedException {
                // My blocking code here
            }
        });
    }
}

Implementation

import javax.annotation.Nonnull;
import java.util.Timer;
import java.util.TimerTask;

import static java.lang.String.format;
import static java.lang.Thread.currentThread;
import static org.junit.Assert.fail;

/**
 * {@code BlockingCall} supports blocking call assertion for JUnit tests.
 *
 * @author <a href="mailto:binkley@alumni.rice.edu">B. K. Oxley (binkley)</a>
 */
public interface BlockingCall {
    /**
     * Calls blocking code.  The blocking code must throw {@code
     * InterruptedException} when its current thread is interrupted.
     *
     * @throws InterruptedException if interrupted.
     */
    void call()
            throws InterruptedException;

    /** Wrapper class for block assertion. */
    public static final class Assert {
        /**
         * Asserts the given <var>code</var> blocks at least 100ms.  Interrupts
         * the blocking code after 100ms and checks {@code InterruptedException}
         * was thrown.  When not blocking, appends <var>block</var> to the
         * failure message.
         *
         * @param block the blocking call, never missing
         */
        public static void assertBlocks(@Nonnull final BlockingCall block) {
            final Timer timer = new Timer(true);
            try {
                final Thread current = currentThread();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        current.interrupt();
                    }
                }, 100L);
                block.call();
                fail(format("Did not block: %s", block));
            } catch (final InterruptedException ignored) {
            } finally {
                timer.cancel();
            }
        }
    }
}

UPDATE: Idioms like this would be far more attractive with Java 8 lambdas:

public void shouldBlock() {
    assertBlocks(() -> { /* blocking code */ });
}

Friday, November 09, 2012

Python's enumerate for Java

Surprisingly I did not find Python's enumerate in Guava Iterables.

A little static import and all is well:

import com.google.common.collect.UnmodifiableIterator;

import javax.annotation.Nonnull;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Iterator;
import java.util.Map.Entry;

/**
 * {@code EnumerateIterable} wraps an iterable like Python {@code enumerate}.
 *
 * @author <a href="mailto:binkley@alumni.rice.edu">B. K. Oxley (binkley)</a>
 */
public final class EnumerateIterable<T>
        implements Iterable<Entry<Integer, T>> {
    @Nonnull
    private final Iterable<T> delegate;

    public EnumerateIterable(@Nonnull final Iterable<T> delegate) {
        this.delegate = delegate;
    }

    /**
     * Creates a new enumerated iterable for the given <var>delegate</var>.
     *
     * @param delegate the underlying iterable, never missing
     * @param <T> the underlying wrapped type
     *
     * @return the new enumerated iterable, never missing
     */
    @Nonnull
    public static <T> Iterable<Entry<Integer, T>> enumerate(@Nonnull final Iterable<T> delegate) {
        return new EnumerateIterable<>(delegate);
    }

    @Nonnull
    @Override
    public Iterator<Entry<Integer, T>> iterator() {
        return new EnumerateIterator<>(delegate);
    }

    private static final class EnumerateIterator<T>
            extends UnmodifiableIterator<Entry<Integer, T>> {
        @Nonnull
        private final Iterator<T> it;
        int i;

        EnumerateIterator(@Nonnull final Iterable<T> delegate) {
            it = delegate.iterator();
            i = 0;
        }

        @Override
        public boolean hasNext() {
            return it.hasNext();
        }

        @Override
        @Nonnull
        public Entry<Integer, T> next() {
            return new SimpleImmutableEntry<>(i++, it.next());
        }
    }
}

UPDATE: Completeness requires a static factory method for arrays as well as iterables.

ISO8601 UTC with XStream

I need ISO8601 conversion with XStream for UTC (GMT/Zulu) time. Originally I went with ISO8601DateConverter which comes with XStream and uses Joda. However I found that it read in UTC (GMT/Zulu) time but wrote back out local time.

To fix this I wrote my own converter using JAXB in the JDK, and dropped the Joda runtime dependency from my project.

I unit tested full roundtrip with "2012-10-22T12:34:56Z". The original converter parsed correctly but serialized as "2012-10-22T07:34:56-05:00". My converter serializes as the input string.

import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;

import java.util.Date;
import java.util.GregorianCalendar;

import static java.util.TimeZone.getTimeZone;
import static javax.xml.bind.DatatypeConverter.parseDateTime;
import static javax.xml.bind.DatatypeConverter.printDateTime;

/**
 * {@code UTCConverter} converts {@code java.util.Date} objects in ISO8601
 * format for UTC without milliseconds.
 *
 * @author <a href="mailto:binkley@alumni.rice.edu">B. K. Oxley (binkley)</a>
 */
public final class UTCConverter
        extends AbstractSingleValueConverter {
    @Override
    public boolean canConvert(final Class type) {
        return Date.class.isAssignableFrom(type);
    }

    @Override
    public String toString(final Object obj) {
        final GregorianCalendar gmt = new GregorianCalendar(getTimeZone("GMT"));
        gmt.setTime(Date.class.cast(obj));
        return printDateTime(gmt);
    }

    @Override
    public Object fromString(final String str) {
        return parseDateTime(str).getTime();
    }
}

Thursday, October 25, 2012

Tuesday, October 23, 2012

The sensible man

Jon Pither is the sensible man. He writes a practical argument for Clojure over Java. Which is more industrial, Scala or Clojure? Yes.

Thursday, October 18, 2012

Unsafe magic heap

A brilliant post from Martin Thompson, Compact Off-Heap Structures/Tuples In Java, using sun.misc.Unsafe to avoid VM-managed heap and bypass JVM memory limits. Not of the feint of heart.

UPDATE: A lot of interesting things in Thompson's comments.

Monday, October 15, 2012

JSR-255 (JMX2) and Guice

On a new project with Guice, then, how do I magically wire my managed objects to JMX? Sadly JSR-255, aka JMX2, did not make JDK6 or JDK7 (there's always JDK8). I live in the here and now, and relied before on Spring JMX annotations and MBeanExporter.

There's no full implementation of JMX2, but covering a good minimal feature set is pojo-mbean. How to get Guice to register @MBean-annotated objects? Use injectors:

class SomeModule extends AbstractModule {
    @Override
    protected void configure() {
        bindListener(Matchers.any(), new JMXTypeListener());
    }
}

class JMXTypeListener implements TypeListener {
    @Override
    public <I> void hear(TypeLiteral<I> type,
            TypeEncounter<I> encounter) {
        Class<? super I> rawType = type.getRawType();
        // From pojo-mbean; eventually JSR-255
        if (rawType.isAnnotationPresent(MBean.class))
            encounter.register(new InjectionListener<I>() {
                @Override
                public void afterInjection(final I injectee) {
                    try {
                        // From pojo-mbean; eventually JSR-255
                        new MBeanRegistration(injectee,
                                objectName(rawType)).
                            register();
                    } catch (Exception e) {
                        encounter.addError(e);
                    }
                }
            });
    }

    private static <I> ObjectName objectName(Class<? super I> type)
            throws MalformedObjectNameException {
        return new ObjectName(type.getPackage().getName(), "type",
                type.getSimpleName());
    }
}

HTML5 Tips

I rarely have use for these things, but this one is highly practical: 8 Superlative Practices for Efficient HTML5 coding.

Thursday, September 06, 2012

The awesomeness of Uncle Bob

Uncle Bob pens The New CTO. A lunchtime conversation opens:

“So, what did you think of that?” I asked as we sat down at our regular table in the cafeteria. As I scanned the other tables in the lunchroom I could see that many other teams were leaning in to their conversation and speaking in semi-hushed tones. The normally light-hearted lunchtime banter had been replaced with a new intensity.

“It started pretty well.” said Jasper. “I mean he was nice enough at first, introducing himself as the new CTO and all.”

“Yeah, but then it started to get weird.” said Jasmine. “I mean, how dare he imply that we’re not behaving professionally? We’ve been working our asses off!”

I won't spoil the ending.

Sunday, August 12, 2012

Wednesday, August 01, 2012

Typing more to type less

My code is littered with bits like this:

import com.google.common.collect.UnmodifiableIterator;

public final class TimesIterable
        implements Iterable<Integer> {
    private final int n;

    public static Iterable<Integer> upTo(final int n) {
        return new TimesIterable(n);
    }

    public TimesIterable(final int n) {
        this.n = n;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new UnmodifiableIterator<Integer>() {
            private int i = 0;

            @Override
            public boolean hasNext() {
                return i < n;
            }

            @Override
            public Integer next() {
                return ++i;
            }
        };
    }
}

All so I can write:

import static TimesIterable.upto;
import static java.lang.System.out;

public static void main(final String... args) {
    for (int n : upTo(4))
        out.println(n);
}

To my surprise Google is not quite there yet. (I'd prefer to be wrong).

Zing zings

Azul's Zing remains amazing technology:

Where a typical JVM may spend time battling with garbage collection, McCandless says an in-memory test with the full Wikipedia English-language site loaded worked with no garbage collection pauses under Zing JVM, even with a 140GB heap.

Now free for open source.

Thursday, July 12, 2012

ESR the wit

I forgot what a wit Eric Raymond can be:


Conway’s law
is a well-known fact of life in technology organizations. Eric S. Raymond noted that “[i]f you have four groups working on a compiler, you’ll get a 4-pass compiler”.

From Conway's original:

[O]rganizations [...] are constrained to produce designs which are copies of the communication structures of these organizations.

Tuesday, July 10, 2012

Scrum is ...

Scrum is ... complex and completely over the top. That is if it isn't rescued from itself.

I still prefer XP to everything else, that may be the bias of familiarity. Scrum, and anything else agile, still beats the alternatives. To reiterate:

  • Individuals and interactions over processes and tools
  • Working software over comprehensive documentation
  • Customer collaboration over contract negotiation
  • Responding to change over following a plan

Wednesday, June 27, 2012

A string of pearls

Stuart Archibald at OpenGamma has published a series of tour de force posts. The lastest is Accessing Native Maths Libraries. FORTRAN, gcc, code generation, dynamic linking, magic code glue. What's not to love?

Tuesday, June 19, 2012

Time lies when you're calling functions

Noah Sussman notes Falsehoods programmers believe about time. My favorite:

  1. You can't be serious.

Nearly as good:

That thing about a minute being longer than an hour was a joke, right?

No.

Generating random Java strings

I found lots of misfitting advice searching Google for how to generate random strings in Java. The misfits assumed I was generating unique keys, often for web page cookies.

What I searched for was random Java strings along the lines of new Random().nextInt(), to use for probabilistic testing, but I failed to find this so I rolled my own.

Not a thing of beauty, but accomplished by goal. Hopefully someone might find this useful:

String randomString(final Random random,
        final int minLength, final int maxLength) {
    final int length = random.nextInt(maxLength - minLength) + minLength;
    final char[] chars = new char[length];
    for (int i = 0, x = chars.length; i < x; )
        do {
            final int cp = random.nextInt(0x10FFFF + 1);
            if (!Character.isDefined(cp))
                continue;
            final char[] chs = Character.toChars(cp);
            if (chs.length > x - i)
                continue;
            for (final char ch : chs)
                chars[i++] = ch;
            break;
        } while (true);

    return new String(chars);
}

To examine what you get back, consider Character.UnicodeBlock.of(int codePoint).

Monday, June 18, 2012

New in Guava 12: TypeToken, a better Class or TypeLiteral

I'm slow to read up on the Guava 12 release — it came out in April.

Among the changes is this gem: TypeToken.

Guava provides TypeToken, which uses reflection-based tricks to allow you to manipulate and query generic types, even at runtime. Think of a TypeToken as a way of creating, manipulating, and querying Type (and, implicitly Class) objects in a way that respects generics.

Note to Guice users: TypeToken is similar to Guice's TypeLiteral class, but with one important difference: it supports non-reified types such as T, List<T> or even List<? extends Number>; while TypeLiteral does not. TypeToken is also serializable and offers numerous additional utility methods.

I look forward to giving this a spin next time the need arises.

Wednesday, June 13, 2012

Emacs, your makefile friend

After long hiatus I find myself against writing (editing) makefiles. Using Emacs compile command makes all the difference. Emacs runs make in another process, sending output to an independent buffer (window, tab, panel) default named *compilation*.

In the *compilation* buffer, Emacs colorizes make's output. This greatly aids comprehension. As icing, Emacs recognizes output patterns from dozens of popular tools, and highlights them as information, warning or error as appropriate.

Emacs simplifies navigation with hyperlinking. The TAB key in *compilation* steps through warnings and errors. Magic.

As a bonus, I looked up common patterns stored in the Emacs variable, compilation-error-regexp-alist. The info page for this variable includes a link to "compilation.txt", a complete list of sample output for each supported tool. It was trivial for me to find right at the top:

* GNU style

symbol: gnu

foo.c:8: message
../foo.c:8: W: message
/tmp/foo.c:8: warning: message

That last line was my ticket. I arrange to echo similar text when something needs attention in my shell command, but without failing the entire build. I take care to break the output into separate shell echo commands so Emacs does not match prematurely when make displays executed commands. I could have prefixed my shell line with "@" (ampersand) to suppress make from printing it, but coding standards here discourage that.

a-file:
        if $(some_bad_condition_set_in_makefile) ; then \
        echo -n $@ ; \
        echo :1: warning: $@ is foobar. >&2 ; fi

I emulate the output of one of the tools Emacs groks, and I get the same magical colorizing and hyperlinking with my output:

making in /some/path
if true ; then \
 echo -n a-file ; \
 echo :1: Warning: a-file is foobar. >&2 ; fi
a-file:1: warning: a-file is foobar.

In *compilation* the TAB key jumps me to the start of a-file when it exists.

Tuesday, June 12, 2012

The generic getter idiom in Java

In our code base we represent Google protobuf messages as rooted trees, informally just trees. Nodes are necessarily heterogeneous: some are value nodes—strings, numbers, etc.—, some are roots of more trees, that is, node collections.

I recently replaced code like this:

StringNode childA = (StringNode) parent.get("child A's name");
Int32Node childB = (Int32Node) parent.get("child B's name");

With code like this:

StringNode childA = parent.get("child A's name");
Int32Node childB = parent.get("child B's name");

How did I do this? I changed the definition of "get" from:

public Node get(final String name) {
    // Clever implementation
}

To:

public <N extends Node> N get(final String name) {
    // Same implementation with some "(N)" casts
}

When the compiler can infer the return type of "get", it will, saving you writing now and reading later. In those cases where it cannot infer the type, you still help:

final String valueA = (String) ((StringNode) parent).get("child A's name").getValue();
final Integer valueB = (Integer) ((Int32Node) parent).get("child B's name").getValue();

Becomes:

final String valueA = parent.<StringNode>get("child A's name").getValue();
final Integer valueB = parent.<Int32Node>get("child B's name").getValue();

There is still a type cast, but you spell it differently using the generics system. Note "getValue" uses the same idiom as "get"; the return type can be inferred, no extra writing needed.

I'm going to call this pattern the Generic Getter Idiom until I find a good reference.

Related: Crossing generics and covariant returns

Thursday, June 07, 2012

The view of things to come Java

Lukas Eder notes the possibility of collection and structural literals in Java. A man can dream, can't he? Happily, this dream is built on more than wisps of fancy, just. I don't hold my breath.

Friday, June 01, 2012

Thursday, May 24, 2012

Picture GCC mangling

Matt Godbolt posts GCC Explorer - an interactive take on compilation.

One of the things I spend a fair amount of time doing at work is compiling my C/C++ code and looking at the disassembly output. Call me old-fashioned, but I think sometimes the only way to really grok your code is to see what the processor will actually execute. Particularly with some of the newer features of C++11 — lambdas, move constructors, threading primitives etc — it’s nice to be able to see how your elegant code becomes beautiful (and maybe even fairly optimal) machine code.

I’d managed to get my pipeline for taking small snippets of C code, building them with GCC, de-mangling the output, musing on the assembly, tweaking the input and then repeating over and over again.

See github for sources.

Wednesday, May 16, 2012

Uncle Bob on No DB

Another beautiful rant from Uncle Bob:

Here’s what an application should look like. The use cases should be the highest level and most visible architectural entities. The use cases are at the center. Always! Databases and frameworks are details! You don’t have to decide upon them up front. You can push them off until later, once you’ve got all the use cases and business rules figured out, written, and tested.

8th light must be something else.

Wednesday, May 09, 2012

Is Continuous Delivery agile?

Keif Morris thoughtfully compares/contrasts Agile to Continuous Delivery (CD).

Of course the answer to the question posed in the post title is Yes, Continuous Delivery is agile. Very agile, in fact.

Keif raises several concerns traditional Agile has with Continuous Delivery. I admit to being from the old school, XP, and am a little nervous around CD. But I embrace its spirit.

With Agile leaving small release messes to clean up each iteration, or for Waterfall one giant mess to clean up in the release phase, is bad for the nerves too. At least CD picks up after itself in an ongoing basis.

As a daily Java programmer Maven makes a hash of this as Keif points out. But as with many things related to Maven, you gain a few pains and lose several others for net betterment. How does CD treat the snapshot ailment?

Friday, April 27, 2012

ESR on "C" portability across time

ESR posts on portability of "C" across time. Most times you port across machines and operating systems. "C" is rare in making straight-forward the task of porting from the past into the future.

Tuesday, April 17, 2012

sed is Turing complete

My command line tool is cooler than your command line tool. Thank you, Peteris Krumins, for linking. And don't forget his book.

UPDATE: A testament to my typing skills, the original title of this post was "sed is Turning complete".

Thursday, April 12, 2012

Mythryl: Another great language introduction

Another great language introduction, this one for Mythryl:

Howdy! I’m Cynbe, lead Mythryl developer.

Why do I do it?

Let me tell you.

I spent the first four years of this millennium doing eighty-hour weeks at a Fortune 5 company in a division internally famous for producing more revenue per employee than the IRS.

I remember arriving home at three AM Christmas Day, sleeping thirty-six hours straight, and then driving right back to work.

It was a cool trip in its way, but over time the stress does get to you. By four years in, vomiting blood in the wee hours was starting to seem entirely normal.

It was time for a change.

By then I had written well over a million lines in C plus substantial amounts in other languages. I felt ready to take it to the next level.

“A language that doesn’t affect the
way you think about programming
is not worth knowing.”
Alan Perlis

So I looked around to see what was new and improved. I’d learned APL, assembly, C, Fortran, Lisp, Pascal, Smalltalk, Snobol, SQL and so Forth in the 1970s, but after that there had been a long dry spell. C++, J, Java, Perl, Python, Ruby, sure, but they hardly catapult us into a new era of butterflies and rainbows. They did not expand my mind like Lisp and Smalltalk.

Happily, mostly-functional programming languages had just reached the Ready For Prime Time point.

My favorite was SML/NJ, from the nice folks who gave us the laser, the transistor, and Unix.

Unfortunately, it was research-grade code cloaked in academic jargon which hadn’t seen an end-user release that millennium.

Fortunately, I was looking for something to do.

So I set about hammering this magnificent raw material into a modern production quality open source software development platform.

To my mind Mythryl deftly combines C speed, Lisp power, and Ruby convenience with the critical new ingredients of Hindley-Milner typing, state of the art generics and just the right level of side effects.

I’m in love!

Thursday, April 05, 2012

Real Options

Shane Hastie interviews Chris Matts and Olav Maassen on Real Options, an agile technique to improve IT decision making.

Do read it all. A taste:

InfoQ: Please can you briefly explain Real Options. 

Olav: Options have value, options expire, never commit early unless you know why.

Options are a way of looking at decision making. Financial options work in very narrow areas, in the financial marketplace they entail paying a fee to defer making a choice about purchasing a share or financial instrument until a later date. Our thinking about Real Options was inspired by financial options, but they are much broader in application.

Real Options are more to do with the psychology of how people make decisions than with the choices available to them. 

InfoQ: Please explain. 

Chris: People hate uncertainty, so much so that they would rather be wrong than uncertain. In a “rational preference” the hierarchy of decision making would be:

  1. Have the right answer
  2. Be uncertain about the answer
  3. Have the wrong answer

The reality is we prefer to be wrong rather than be uncertain, so the hierarchy is:

  1. Have the right answer
  2. Have the wrong answer
  3. Be uncertain about the answer

We can tell this because when people are faced with uncertainty, they would rather make any decision, even if it is the wrong one.

Generally people would rather have a definite wrong answer than be unsure about an answer.

Real Options is about understanding when to make a decision, rather than how to make decisions. By adding the “when” to the decision making process we remove the uncertainty and enable people to make better decisions.

Like financial options have a fixed contractual expiry date, real options have a conditional expiry date.

Wednesday, March 21, 2012

Jline moves forward

One of my favorite Java libraries, jline, released 2.6 recently. Among the goodies is support for ~/.inputrc. This is as close to readline as Java gets.

Tuesday, March 13, 2012

The science of cloud computing

The physics arXiv blog at MIT Technology Review posts The Hidden Risk of a Meltdown in the Cloud.

As with anytime you bet the farm on a technology, try to have a backup plan.

Monday, March 12, 2012

re: Romans, rubies and the D

Kajetan Rzepecki posts Romans, rubies and the D, a clever comparison of Ruby missing method look up and the compile-time equivalent in D. (It's short, please read.)

This is a clean example in favor of proper macros (not the "CPP" sort).

I've long been impressed by D. It makes up for many of the faults in C++, which impresses me less as time goes on. What is impressive about D? I appreciate that the first thing it says about itself is:

D is a systems programming language. Its focus is on combining the power and high performance of C and C++ with the programmer productivity of modern languages like Ruby and Python. Special attention is given to the needs of quality assurance, documentation, management, portability and reliability.

And that, therefore, this comes only second:

The D language is statically typed and compiles directly to machine code. It’s multiparadigm, supporting many programming styles: imperative, object oriented, and metaprogramming. It’s a member of the C syntax family, and its appearance is very similar to that of C++.

That says something good about taste in my book.

Friday, March 02, 2012

Showing test failure name for JUnit parameterized tests

A popular complaint against JUnit is lack of human-readable names for parameterized test cases. There are several solutions, this is mine.

/**
 * {@code ParameterizedTest} simplifies parametered JUnit tests by including a "case label" to
 * identify the failing branch.  (JUnit only provides an index).  The failure message format
 * becomes "descriptive string from JUnit: case label: assertion failure message".
 * <p/>
 * Fully preserves the stack trace of failing tests.
 * <p/>
 * Example: <pre>
 * public class MyTest {
 *     &#64;Parameters
 *     public static Collection<Object[]> parameters() {
 *         return ...; // See JUnit documentation for parameterized tests
 *     }
 *
 *     public MyTest(final String caseLabel, final X x, final Y y) {
 *         super(caseLabel);
 *         this.x = x;
 *         this.y = y;
 *     }
 *
 *     &#64;Test
 *     public void shouldWork() {
 *         // Body of test method ...
 *     }
 * }
 * </pre>
 * When {@code shouldWork()} fails the failure message is more meaningful, including the
 * {@link #caseLabel "case label"} in the JUnit error message.
 *
 * @author <a href="mailto:binkley@alumni.rice.edu">B. K. Oxley (binkley)</a>
 */
@RunWith(Parameterized.class)
public abstract class ParameterizedTest {
    private final String caseLabel;

    /**
     * The JUnit test rule (test extension) which fails bad parameterized cases but provides a
     * {@link #caseLabel case label} identifing the failed test branch.
     */
    @Rule
    public final TestRule parameterizedTestRule = new TestWatcher() {
        @Override
        protected void failed(final Throwable e, final Description description) {
            final AssertionError x = new AssertionError(
                    description.getDisplayName() + ": " + caseLabel + ": " + e.getMessage());
            x.setStackTrace(e.getStackTrace());
            throw x;
        }
    };

    /**
     * Constructs a new {@code ParameterizedTest} for the given <var>caseLabel</var>.
     *
     * @param caseLabel the case label, never missing
     */
    protected ParameterizedTest(@Nonnull final String caseLabel) {
        this.caseLabel = caseLabel;
    }
}

Friday, February 24, 2012

Testing thread affinity

Peter Lawrey posts on thread affinity with Java. This is a post full of numbers, good numbers. If you write low latency code in Java, this is for you.

Tuesday, February 21, 2012

More git: moving modules between repos

Not the coolest merge, EVER! but much more grokable, Greg Bayer provides Moving Files from one Git Repository to Another, Preserving History.

The problem: I want to move repo-A/module1 to repo-B/module1 while preserving history. Bayer to the rescue. Thanks, Greg!

Saturday, February 18, 2012

How to introduce a new programming language

The Julia folks provide show the way to introduce a new programming language:

We are greedy: we want more.

We want a language that’s open source, with a liberal license. We want the speed of C with the dynamism of Ruby. We want a language that’s homoiconic, with true macros like Lisp, but with obvious, familiar mathematical notation like Matlab. We want something as usable for general programming as Python, as easy for statistics as R, as natural for string processing as Perl, as powerful for linear algebra as Matlab, as good at gluing programs together as the shell. Something that is dirt simple to learn, yet keeps the most serious hackers happy. We want it interactive and we want it compiled.

(Did we mention it should be as fast as C?)

Thanks to Michael J. Simoni for pointing out Julia. More here.

Friday, February 17, 2012

Losing malloc, gaining the world

At work we've dropped the malloc that comes with GCC in favor of tcmalloc from Google. Our creaking, monolithic C++ backend gains 10-20% performance, unsurprising given the history of malloc.

Concatenative Programming

Jon Purdy on concatenative programming, which I had never heard of before. Basically it is purer functional programming for those of you who think Haskell is too corporate. (Actually, I enjoyed the read.)

P.S. — A remark from a clever fellow, no slouch he: That made my head hurt and reminded me of, when I was a freshman, trying to do a few chapters of Abelson & Sussman on my HP 28S calculator, which had RPN and (I realized) first-class functions.. I miss RPN calculators.

Thursday, February 09, 2012

Beating the PyPy drum

Laurie Tratt beats the PyPy drum in Fast Enough VMs in Fast Enough Time. Want to know more about VMs? Writing your own language or mini-language? Read on.

Friday, February 03, 2012

Type Theory for the Uninitiated

Kalani Thielen presents The Algebra of Data, and the Calculus of Mutation, or Type Theory for the Uninitiated. Fine calculus a bit soft, algebra a little dull, programming a walk in the park? Type theory is for you.

Wednesday, January 25, 2012

1-1 beats N-M

SpiderMonkey, the JavaScript engine in Firefox, is moving towards a 1-1 threading model, away from a N-M model: JSRuntime is now officially single-threaded. Let me explain by looking back.

In Solaris the threading model is M-N, that is, for each program with N user threads there are M kernel threads servicing the program. This was thought to be the most flexible design and kernel threads were a limited resource.

Linux eventually settled on a 1-1 model, that is, for each program with N user threads there are N kernel threads each mapped uniquely to a user thread. This was found to be the simplest to code and the most performant in practice.

Back to JavaScript:

A single SpiderMonkey runtime (that is, instance of JSRuntime) — and all the objects, strings and contexts associated with it — may only be accessed by a single thread at any given time. However, a SpiderMonkey embedding may create multiple runtimes in the same process (each of which may be accessed by a different thread).

I read this to say: 1-1 mapping from SpiderMonkey to user thread. The same simplifications and performance gains Linux saw from the 1-1 model will be gained for SpiderMonkey.

Old lessons learned again; better than the alternative.

Tuesday, January 24, 2012

Spawn of Java

Brian McCallister writes Java Daemonization with posix_spawn(2), or as I like to think of it, Spawn of Java. The problem description:

The traditional way of daemonizing a process involves forking the process and daemonizing it from the current running state. This doesn’t work so well in Java because the JVM relies on several additional worker threads, and fork only keeps the thread calling fork. So, basically, you need to start a new JVM from scratch to create a child process.

The traditional way of launching a new program is to fork and exec the program you wish to start. Sadly, this also fails on Java because the calls to fork and exec are seperate, non-atomic (in the platonic sense, not the JMM sense) operations. There is no guarantee that the exec will be reached, or that the memory state of the JVM will even be sound when the exec is reached, because you could be in the middle of a garbage collection and pointers could be all over. In practice, this would happen exceptionally rarely, at least. Charles Nutter has talked about this problem in JRuby as well.

Visit Brian's post for the solution.

(You may also enjoy his related POSIX from Java post.)

Major code irritants

I could not agree more with this Java anti-pattern: Storing money in floating point variables.

In my interviews of Java candidates (or C++, for that matter) I always include questions about creating a hypothetical Money class, and pay close attention to their ideas for representation. International candidates often fare better on not assuming US dollars, but most all candidates fail completely on avoiding floating point.

Friday, January 13, 2012

Defaulting message fields in protobuf

Not well documented is the technique for defaulting message fields in protobuf. Say you have a field of message type, named "start" here:

message Complex {
    required int64 real = 1;
    optional int64 imaginary = 2 [default = 0];
}

message Vector {
    required Complex start = 1;
    required int64 length = 2;
}

How to you provide a default value, say at the origin? Like this:

import "google/protobuf/descriptor.proto";

extend google.protobuf.FieldOptions {
    // Pick the field number that is right for you!
    optional Complex complex = 50000;
}

message Vector {
    optional Complex start = 1
    [(complex) = { real: 0 imaginary: 0 }];
    required int64 length = 2;
}

See the Custom Options section for details.

Wednesday, January 11, 2012

Write your own kernel

James Malloy will show you how!

Better than null

A rich, delightful post from Joel Neely on an alternative to returning null to signal "not found" or "No": have the caller supply a callback with appropriate methods for condition signaling. In Java:

interface Found {
    void present(final String name);

    void absent(final String name);
}

class Phonebook {
    private final Set<String> names = new HashSet<>;

    void add(final String name) {
        names.add(name);
    }

    void remove(final String name) {
        names.remove(name);
    }

    void find(final String name, final Found found) {
        if (names.contains(name))
            found.present(name);
        else
            found.absent(name);
    }
}

class LookupPhone extends Phone implements Found {
    private final Phonebook phonebook;

    LookupPhone(final Phonebook phonebook) {
        this.phonebook = phonebook;
    }

    @Override
    public void present(final String name) {
        ringUp(name);
    }

    @Override
    public void absent(final String name) {
        complainIsUnknown(name);
    }

    void placeCall(final String name) {
        phonebook.find(name, this);
    }
}

(Keyed directly into the editor: apologies for typos.)

Tuesday, January 10, 2012

Bill Bejeck writes on Guava EventBus. This is very similar to the "MagicBus" code I wrote for a client while at ThoughtWorks in the mid-naughts. Essentially it is an in-process event bus respecting Java method signatures rather than a string-based topic space. You can write decoupled code in a pub-sub style within your own application.

In some weird sense it is an implementation of the COMEFROM instruction. When a "caller" publishes the right parameter list, subscribing methods are invoked as if called directly. Adding new subscribers changes the meaning of the call site. The difference is that control returns to the publisher after all subscribers run; COMEFROM is a complete transfer of control and does not multiplex.

Long live INTERCAL!