Skip to content

Refactoring code #169

Description

@dukaev

I noticed that many examples in code/ do not match the template.

require "benchmark/ips"

def fast
end

def slow
end

Benchmark.ips do |x|
  x.report("fast code description") { fast }
  x.report("slow code description") { slow }
  x.compare!
end

Activity

  1. ashwinisukale commented on Oct 11, 2022

    @ashwinisukale

    I found following code examples where we are not following the template

    https://github.com/fastruby/fast-ruby/blob/main/code/enumerable/sort-vs-sort_by.rb - Here instead of faster and fastest, can we say fast and faster

    https://github.com/fastruby/fast-ruby/blob/main/code/hash/keys-include-vs-key.rb - Can we change method name to slow and faster, as we are following same template for all our examples

    https://github.com/fastruby/fast-ruby/blob/main/code/hash/values-include-vs-value.rb - Can we change method name to slow and faster, as we are following same template for all our examples

    https://github.com/fastruby/fast-ruby/blob/main/code/method/call-vs-send-vs-method_missing.rb - Instead of fastest, rename to fast

    https://github.com/fastruby/fast-ruby/blob/main/code/proc-and-block/proc-call-vs-yield.rb - can we rename slow1, slow2 etc to what actually that method is doing

    https://github.com/fastruby/fast-ruby/blob/main/code/string/mutable_vs_immutable_strings.rb - specify which is slower and faster, as per the template we follow in all examples

    https://github.com/fastruby/fast-ruby/blob/main/code/string/sub!-vs-gsub!-vs-%5B%5D%3D.rb - Can we change name from slow_1 to what method is eg slow_sub

  2. self-assigned this
    on Sep 29, 2026
  3. linked a pull request that will close this issueRefactoring by template #170on Sep 29, 2026
  4. linked a pull request that will close this issueName each benchmark's winner and lint it #243on Sep 29, 2026
  5. JuanVqz commented on Sep 29, 2026

    @JuanVqz
    Member

    I noticed this too and worked on it, so that one glance at a file under code/ tells you which code is fast and which is slow.

    @dukaev proposed this first in #170, putting the fast code first in 21 files. #243 finishes that idea.

    The repo now has a linter that enforces the template, so new benchmarks can't drift from it. It fails CI when a Benchmark.ips block doesn't call x.compare!, sets its own timing, never runs, or doesn't put exactly one fastest/faster/fast report first:

    • Linter:
      # Checks that every benchmark under code/ has the same shape, so they all run,
      # print a comparison, and are measured the same way:
      #
      # - every Benchmark.ips block calls x.compare!
      # - no custom timing (Benchmark.ips(20), x.time = 20, x.warmup = 5, x.config(time: 20) in any hash syntax),
      # so every file uses the default
      # - no Benchmark.ips sits inside a method that never runs from the top of the file,
      # which would benchmark nothing at all
      # - every Benchmark.ips block states which report should win: exactly one
      # report calls `fastest` (else `faster`, else `fast`), and it comes first
      #
      # Usage: ruby .github/scripts/lint-benchmarks.rb [files...] (needs Ruby 3.3+)
    • CI job:
      # Every benchmark has the same shape (see lint-benchmarks.rb). Reads the
      # files without running them, on the newest Ruby: the lint needs Prism.
      lint:
      runs-on: ubuntu-latest
      steps:
      - uses: actions/checkout@v4
      - name: Lint benchmarks
      run: docker compose run --rm -T --entrypoint ruby ruby_4.0 .github/scripts/lint-benchmarks.rb
    • Passing run on main: https://github.com/fastruby/fast-ruby/actions/runs/36504571544/job/109202974677
    • Naming rules for contributors:
      Name them by rank and list the winner first.
      The names grow outward from the line between fast and slow: the recommended side uses `fast`, then `faster`, then `fastest`; the other side uses `slow`, then `slower`, then `slowest`.
      The names are relative: `slow` only means slower than `fast`.
      When a ranking could look odd, say why in one line, like in `code/array/length-vs-size-vs-count.rb`:

    Closing this one. Thanks for raising it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions