OS@BLK: Julia NamedTuples

OS@BLK – Julia NamedTuples.jl 1

BlackRock began its adventure into Julia 2 in Jan 2015 with a desire to explore the creation of a new environment for quantitative analysis. As part of these efforts BlackRock needed to develop a type stable record format which would work at run time.

Julia provides a rich set of primitives for defining type stable types at parse time, for example

    immutable MyType

For those new to Julia the above code defines a type named MyType which is both type stable and has the same overhead as the equivalent C struct. For many use cases this is sufficient, you can create a vector of MyType and get efficient access to the members of the type. Unfortunately, when we wanted to build a join function, we started to run into trouble.

    immutable YourType

    x = MyType( 3.142, 2 )
    y = YourType( 1 )
    z = join( x, y )

    typeof( z )

Ideally the type of the result of the join operator would be a new type but this requires that we know the type in advance.

    immutable DerivedType

Julia doesn’t natively support this, but it does have an extremely robust run time macro system. This is where NamedTuples comes in. NamedTuples allows the developer of the ‘join’ function to define a new type stable run time type. The pseudo code for the function looks something like the following:

    function join{T1,T2}( x::T1, y::T2 )
        return @NT( types of T1 && types of T2 )( x, y)

The ability to define, at runtime, stable – C struct equivalent – types is a game changer for high performance record based code. In languages such as C/C++ you would be invoking the compiler; in Java you would resort to a tool like BCEL 3. In Julia, you can use this naturally in line with your code. Go Julia!

BlackRock open sourced NamedTuples.jl in May of 2015. Since this time, it has been used in a number of large Julia projects including Query.jl 4 and IndexedTables.jl 5

  1. https://github.com/blackrock/NamedTuples.jl
  2. https://julialang.org/
  3. https://commons.apache.org/proper/commons-bcel/
  4. https://github.com/davidanthoff/Query.jl
  5. https://github.com/JuliaComputing/IndexedTables.jl

ID 152641