JuliaWeb / Hyperscript.jl

Hyperscript: A lightweight DOM representation for Julia
Other
101 stars 11 forks source link

improve speed [WIP] #13

Open SimonDanisch opened 5 years ago

SimonDanisch commented 5 years ago

Surprisingly, broadcast seems to do really badly with empty lists & Vector{Any} results. Removing the use of showable() made the biggest difference. Of course this can't stay like it, and we'll need to figure out a why to optimize this without giving up the functionality. This branch, at least for my benchmark, is ~7x faster at constructing & rendering a dom!

JeffreySarnoff commented 5 years ago

ping

tshort commented 5 years ago

For the showable issue, it's slow because it uses hasmethod here. One option would be to set up a Dict filled in with some precomputed answers. Then, showable only needs to be called for types not checked, yet. try might be an easier option to try, too.

tshort commented 5 years ago

Here's a version that lessens the use of showable for commonly used types. With this, tests pass on @SimonDanisch's branch.

function renderdomchild(io, rctx::RenderContext, ctx, x)
    if x isa Number || x isa AbstractString
        printescaped(io, x, escapechild(ctx))
    elseif x isa HTML
        print(io, repr(MIME("text/html"), x))
    else
        showable(MIME("text/html"), x) ? print(io, repr(MIME("text/html"), x)) : printescaped(io, x, escapechild(ctx))
    end
end

Here's a test script:

using Markdown, Hyperscript, BenchmarkTools
@tags div h1
@btime repr($(div("hello", h1("qwerqwer"), 33, "asdf")))
@btime repr($(div("hello", h1("qwerqwer"), 33, "asdf", md"*asdf* asdf")))
@btime repr($(div("hello", h1("qwerqwer"), 33, "asdf", HTML("<div>hello</div>"))))

As a side note, static_hasmethod from Tricks.jl has interesting implications for easy traits.