Skip to content

Don't rewrite it in Rust. Rewrite the 5% that's slow: profile your app, port the hot functions with your coding agent, prove they behave the same. Python, JS/TS, Ruby, Java, Kotlin, Go, PHP, C/C++, C#, R.

5.6 KB
Raw Blame
# frozen_string_literal: true

# Shared value helpers for fast-anything's Ruby side, and the call recorder
# (preloaded with "ruby -r" when FAST_ANYTHING_RECORD_OUT is set). Not meant to be used by hand.
require "json"

module FastAnything
  class Unsupported < StandardError; end

  FLOATS = { "NaN" => Float::NAN, "Infinity" => Float::INFINITY, "-Infinity" => -Float::INFINITY }.freeze

  module_function

  def enc(v)
    case v
    when nil, true, false, Integer then v
    when Float then v.finite? ? v : { "$float" => v.to_s }
    when String
      s = v.encoding == Encoding::UTF_8 ? v : v.encode(Encoding::UTF_8)
      raise Unsupported, "binary String" unless s.valid_encoding?
      s
    when Array then v.map { |x| enc(x) }
    when Hash
      raise Unsupported, "Hash with non-String keys" unless v.keys.all?(String)
      { "$hash" => v.map { |k, x| [k, enc(x)] } }
    when Symbol then raise Unsupported, "Symbol"
    else raise Unsupported, v.class.name
    end
  rescue EncodingError
    raise Unsupported, "binary String"
  end

  def dec(v)
    case v
    when Array then v.map { |x| dec(x) }
    when Hash
      tag, x = v.first
      case tag
      when "$float" then FLOATS.fetch(x)
      when "$hash" then x.to_h { |k, e| [k, dec(e)] }
      else raise "unknown tag #{tag}"
      end
    else v
    end
  end

  # Arguments for a port's C bridge, where NaN and infinity, which JSON has no numbers for, travel
  # as {"$float": name}. The port's reply comes back the same way.
  def wire(v)
    case v
    when Float then v.finite? ? v : { "$float" => v.to_s }
    when Array then v.map { |x| wire(x) }
    when Hash then v.transform_values { |x| wire(x) }
    else v
    end
  end

  def unwire(v)
    case v
    when Array then v.map { |x| unwire(x) }
    when Hash then v.size == 1 && FLOATS.key?(v["$float"]) ? FLOATS[v["$float"]] : v.transform_values { |x| unwire(x) }
    else v
    end
  end

  # Floats match within 1e-9 of their size, or of scale (the largest number in the expected Array
  # holding them). -0.0 isn't 0.0: it prints differently.
  def same(a, b, scale = 0.0)
    if a.is_a?(Float) || b.is_a?(Float)
      return false unless a.is_a?(Float) && b.is_a?(Float)
      return a.nan? && b.nan? if a.nan? || b.nan?
      return 1.0 / a == 1.0 / b if a == b
      return a.finite? && b.finite? && (a - b).abs <= 1e-9 * [a.abs, b.abs, scale].max
    end
    if a.is_a?(Array) && b.is_a?(Array)
      big = [a.grep(Numeric).map(&:abs).select { |x| x < Float::INFINITY }.max || 0, Float::MAX].min.to_f
      return a.length == b.length && a.zip(b).all? { |x, y| same(x, y, big) }
    end
    return a.keys.sort == b.keys.sort && a.all? { |k, x| same(x, b[k]) } if a.is_a?(Hash) && b.is_a?(Hash)

    a.class == b.class && a == b
  end

  # Calls the method with the values of its parameters in order ([a, b, xs, scale] for
  # a, b = 2, *xs, scale: 1): *rest spread out again, keywords by name.
  def invoke(method, receiver, values, blk = nil)
    positional = []
    keywords = {}
    method.parameters.reject { |kind, _| %i[keyrest block nokey].include?(kind) }.zip(values) do |(kind, name), value|
      case kind
      when :rest then positional.concat(value)
      when :key, :keyreq then keywords[name] = value
      else positional << value
      end
    end
    method.bind_call(receiver, *positional, **keywords, &blk)
  end
end

if ENV["FAST_ANYTHING_RECORD_OUT"]
  module FastAnything
    @kept = []
    @calls = 0
    @skipped = 0
    @unsupported = {}
    @limit = Integer(ENV.fetch("FAST_ANYTHING_RECORD_LIMIT", "500"))
    @rng = Random.new(0xFA57)

    class << self
      attr_reader :kept

      def store(slot, c)
        if @kept.length < @limit then @kept << c else @kept[slot % @limit] = c end
      end

      def note(e)
        @skipped += 1
        @unsupported[e.message] = true
      end

      def record(original, receiver, args, blk)
        @calls += 1
        slot = @kept.length < @limit ? @kept.length : @rng.rand(@calls)
        c = nil
        if slot < @limit
          begin
            c = { "args" => enc(args) } # before the call, in case it mutates its arguments
          rescue Unsupported => e
            note(e)
          end
        end
        begin
          result = invoke(original, receiver, args, blk)
        rescue StandardError => e
          if c
            c["raises"] = e.class.name
            store(slot, c)
          end
          raise
        end
        if c
          begin
            @mutates = true if enc(args) != c["args"]
          rescue Unsupported
            nil
          end
          begin
            c["result"] = enc(result)
            store(slot, c)
          rescue Unsupported => e
            note(e)
          end
        end
        result
      end

      def save
        File.write(ENV.fetch("FAST_ANYTHING_RECORD_OUT"), @kept.map { |c| JSON.generate(c) + "\n" }.join)
        File.write(ENV.fetch("FAST_ANYTHING_RECORD_META"),
                   JSON.generate({ "calls" => @calls, "kept" => @kept.length, "skipped" => @skipped, "unsupported" => @unsupported.keys, "mutates" => @mutates || false }))
      end
    end
  end

  # bind: a lambda with the method's own parameters that returns their values, so arguments
  # (defaults, *rest, keywords) are bound the way the method binds them
  $__fast_anything_wrap = lambda do |owner, name, bind|
    original = owner.instance_method(name)
    was_private = owner.private_method_defined?(name)
    owner.send(:define_method, name) do |*args, **kw, &blk|
      FastAnything.record(original, self, instance_exec(*args, **kw, &bind), blk)
    end
    owner.send(:private, name) if was_private
  end

  at_exit { FastAnything.save }
end