view icon indicating copy to clipboard operation
view copied to clipboard

Investigate performance improvements

Open cllns opened this issue 10 months ago • 1 comments

According to https://github.com/KonnorRogers/view-layer-benchmarks, hanami-view is the slowest of the view layer libraries tested (including slower than Rails partials).

We should take a look to see if there's more performance improvements we can make. I don't expect hanami-view to be the fastest view library, since we offer many features other don't have, but I think it's reasonable to get our performance to be similar to Rails partials and Trailblazer cells (which would require a ~2x speed improvement).

I wonder if there's object allocations we can avoid. One idea I had was to skip wrapping exposures in a Part if the user doesn't have their own Part class defined. Maybe there are other opportunities for similar improvements.

Here's some previous work that was done to this same end: https://github.com/hanami/view/pull/223

cllns avatar Feb 16 '25 19:02 cllns

I modified the hanami-view test to use partials for the nested view and that gives us performance comparable to Rails and Trailblazer. I may open a PR to take that approach for that benchmark, but I wonder if we can do better, even with nested View objects.

❯ rake benchmark
/Users/sean/.asdf/installs/ruby/3.3.6/bin/ruby ./benchmark.rb
ruby 3.3.6 (2024-11-05 revision 75015d4c1f) [arm64-darwin23]
Warming up --------------------------------------
         hanami_view   351.000 i/100ms
          papercraft     1.416k i/100ms
            partials   313.000 i/100ms
               phlex   967.000 i/100ms
           ruby2html   919.000 i/100ms
   trailblazer_cells   396.000 i/100ms
      view_component   699.000 i/100ms
Calculating -------------------------------------
         hanami_view      3.494k (± 3.4%) i/s -     35.100k in  10.059270s
          papercraft     13.597k (± 3.5%) i/s -    135.936k in  10.010449s
            partials      3.172k (± 4.1%) i/s -     31.926k in  10.083207s
               phlex      9.723k (± 3.3%) i/s -     97.667k in  10.056697s
           ruby2html      9.237k (± 3.3%) i/s -     92.819k in  10.059976s
   trailblazer_cells      3.917k (± 3.2%) i/s -     39.204k in  10.018662s
      view_component      6.944k (± 3.2%) i/s -     69.900k in  10.077220s

Comparison:
          papercraft:    13597.2 i/s
               phlex:     9723.1 i/s - 1.40x  slower
           ruby2html:     9236.9 i/s - 1.47x  slower
      view_component:     6944.0 i/s - 1.96x  slower
   trailblazer_cells:     3917.5 i/s - 3.47x  slower
         hanami_view:     3493.6 i/s - 3.89x  slower
            partials:     3171.7 i/s - 4.29x  slower

cllns avatar Mar 12 '25 18:03 cllns