More Julia

Julia language logo

Decided to take another day on Julia, write something more serious and see how that goes.

There's an uber-juno "IDE" plugin for Atom, which at least turns on syntax highlighting and puts an interactive console in the editor. Yay. It's not capable of linting yet, though it says it is.

So I'm rewriting a simple 1970s-style dungeon crawl game (Chorus:"As if you could make any other kind of game, Mark!" Mark:"I assure you I could, I just choose not to.") as a test of data structures and application programming in Julia. It's a little challenging, but not impossible.

Using Modules

The current directory is not included in the default LOAD_PATH, so you can't import local modules right off. One solution is to put it in your startup:

% mkdir -p ~/.julia/config
% echo '@everywhere push!(LOAD_PATH, pwd())' >>~/.julia/config/startup.jl

All names are in the same namespace. This means your module and a struct or method in it can't have the same names… My current solution has been to pluralize the module, so GridMaps contains a struct GridMap.

The export rules are a little annoying. If you use the @enum macro to make a ton of constants, they aren't exported when you export the enum type; you can either manually export each constant name, or just use ModuleName.ConstantName in other modules. Bleh.

Debugging

Bug #1: The terminator problem is pretty bad in Julia:

% cat Foo.jl
module Foo
for i=1:10
    println(i)
#missing end
end #module

% julia Foo.jl
ERROR: LoadError: syntax: incomplete: "module" at /Users/mdh/Code/CodeJulia/Foo.jl:1 requires end
Stacktrace:
 [1] include at ./boot.jl:317 [inlined]
 [2] include_relative(::Module, ::String) at ./loading.jl:1038
 [3] include(::Module, ::String) at ./sysimg.jl:29
 [4] exec_options(::Base.JLOptions) at ./client.jl:229
 [5] _start() at ./client.jl:421
in expression starting at /Users/mdh/Code/CodeJulia/Foo.jl:1

Good luck finding that missing end if you have a 1000-line module. C used to be just as bad about semicolons and braces, but modern compilers are pretty good at guessing where you fucked up. Python's whitespace-as-control is brilliant, because you can't ever do that. A passable solution would be each control keyword having its own unique end keyword, but it's too late for that. In the actual bug, I had to comment out half the code, run the module, uncomment and comment the other half, repeat until I isolated it.

Bug #2: Type annotations need to be very generic or left off entirely. As code-as-documentation, I declare a function as parseLine(line::String), and it gives me:

MethodError(Main.Foo.parseLine, ("a",), 0x00000000000061bb)

Well, thanks. Turns out I need to use AbstractString, because a prior function returns an AbstractString and not String. Or I can just leave the typing off, as was my first instinct.

String Concatenation

This is super ugly. Currently I've fallen back on:

sb = Vector()
push!(sb, "part of ")
push!(sb, "a string")
return join(sb, "")

Strings aren't mutable, and there's no StringBuffer/NSMutableString equivalent. The other option is to use an IOBuffer. I haven't done timings yet to see which is faster/uses less memory, I just find pushing to a vector simpler.

Switch

There's no switch statement. I could put functions in a dictionary and dispatch on that, which is not ideal for looping over a bunch of simple values, and requires passing around control flags instead of a simple break or return. Caveman solution is a chain of if/elseif, but I don't like it. Possibly a macro could be written?

Binary

Turns out you can make binaries: Julia apps on the App Store: Building and distributing an application written in Julia

It's an ugly process, that Nathan's working around, but it's a start. This should 100% be in the core libraries, and should have a cross-compiler.

Getting this working is tomorrow's problem, I think.

Comments are closed.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)

Mentions

  • Last time, I was uncertain about string concatenation, so I did a test:
    #!/usr/bin/env julia

    const kTestText = "abcdefghijklmnopqrstuvwxyz0123456789n"
    const kLoops = 10000

    function stringString()
    s = ""
    for i in 1:kLoops
    s = "$s$kTestText"
    end
    return s
    end

    function bufferString()
    sb = IOBuffer()
    for i in 1:kLoops
    print(sb, kTestText)
    end
    return String(take!(sb))
    end

    function vectorString()
    sb = Vector()
    for i in 1:kLoops
    push!(sb, kTestText)
    end
    return join(sb, "")
    end

    function typedVectorString()
    sb = Vector{AbstractString}()
    for i in 1:kLoops
    push!(sb, kTestText)
    end
    return join(sb, "")
    end

    println("*** stringString")
    @timev stringString()
    sleep(2)

    println("n*** bufferString")
    @timev bufferString()
    sleep(2)

    println("n*** vectorString")
    @timev vectorString()
    sleep(2)

    println("n*** typedVectorString")
    @timev typedVectorString()

    *** stringString
    1.100197 seconds (21.24 k allocations: 1.725 GiB, 15.94% gc time)
    elapsed time (ns): 1100197167
    gc time (ns): 175349222
    bytes allocated: 1851904041
    pool allocs: 11292
    non-pool GC allocs:9950
    GC pauses: 79

    *** bufferString
    0.006864 seconds (11.80 k allocations: 1.134 MiB)
    elapsed time (ns): 6864042
    bytes allocated: 1189493
    pool allocs: 11794
    non-pool GC allocs:3
    realloc() calls: 8

    *** vectorString
    0.017380 seconds (26.68 k allocations: 2.191 MiB)
    elapsed time (ns): 17380237
    bytes allocated: 2297091
    pool allocs: 26659
    non-pool GC allocs:9
    realloc() calls: 8

    *** typedVectorString
    0.031384 seconds (44.75 k allocations: 2.999 MiB, 10.00% gc time)
    elapsed time (ns): 31384383
    gc time (ns): 3137654
    bytes allocated: 3144221
    pool allocs: 44730
    non-pool GC allocs:8
    realloc() calls: 8
    GC pauses: 1

    Well, there's me told off. I expected #1 typedVector, #2 vector, #3 buffer, then stringString way at the bottom. Instead the first 3 are reversed.
    IOBuffer, as ugly as it is, is the clear winner. Vector did OK, but twice as much CPU & RAM loses. Amusing that typedVector is twice as slow and memory-heavy as the untyped (explained ). On larger loops, buffer gets slower, but vector remains a memory pig, and in GC that's unacceptable. Of course stringString is terrible, and it's almost exactly the same for string(s, kTestText).
    Time to rewrite some text processing.