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!

Thursday, December 29, 2011

Friday, December 16, 2011

Linking profile to Google+

I've linked my Blogger profile to my Google+ account. Please comment if you have any thoughts on this, or if you see anything odd or missing as a result. Thanks!

Java server hiccups

Gil Tene at Azul has great insight into Java server performance gaffs. He calls them hiccups and provides tooling to find them.

Thursday, December 15, 2011

Monad in Java

The shortest, clearest description of monads in Java I have read:

public class OptionalMonad {
    public static <T> Optional<T> unit(T value) {
        return Optional.of(value);
    }

    public static <T, U> Optional<U> bind(
            Optional<T> value,
            Function<T, Optional<U>> function) {
        if (value.isPresent()) return function.apply(value.get());
        else return Optional.absent();
    }
}

Thanks to François Sarradin in a very nice post.

Friday, December 09, 2011

Netflix circuit breaker

Ben Schmaus of the Netflix team posts a lovely description of the degraded service features of their API. The real life use case is compelling. The dashboard is just brilliant.

Wednesday, November 30, 2011

The death of HyperCard, a whodunit

Stanislav Datskovskiy posts his theory on the murder of HyperCard. It wasn't the butler.

Baker's Treadmill and Disruptor

Flying Frog Consultancy in Cambridge, England posts a nice comparison between Disruptor and low-latency GC algorithms:

The core of [Disruptor's] idea is to accomplish all of this message passing using a single shared data structure, the disruptor, rather than using several separate concurrent queues.

Baker's Treadmill is a low-latency garbage collection algorithm. Allocated blocks in the heap are linked together to form a cyclic doubly-linked list that is divided into four segments using four iterators that chase each other around the ring as heap blocks are allocated, traced and freed.

Cleverly observed with a good diagram. As they say, read the whole thing.