Code Monkey home page Code Monkey logo

Comments (3)

tisba avatar tisba commented on August 17, 2024

Interesting 🤔

Just to be clear: The error with the timeout is to be expected, as it is in milliseconds.

I got curious and removed the IO-related part and using just Kernel.sleep having rescue in the attached method does influence the runtime quite a bit. In the following code with mini_racer timeout at 10ms, but sleeping for 1000ms. With rescue we need ~2 seconds, without ~12.78ms (slightly above the 10ms timeout we set):

# frozen_string_literal: true

require "bundler/inline"

gemfile do
  source "https://rubygems.org"

  gem "mini_racer", "0.12.0"
end

require "libv8-node"

puts "RUBY_VERSION : #{RUBY_VERSION}"
puts "RUBY_PLATFORM: #{RUBY_PLATFORM}"
puts "MiniRacer::VERSION: #{MiniRacer::VERSION}"
puts "Libv8::Node::LIBV8_VERSION: #{Libv8::Node::LIBV8_VERSION}"
puts "=" * 80

def sleep_rescue(duration_ms)
  sleep duration_ms.to_f / 1000
  :result_a
rescue MiniRacer::ScriptTerminatedError => e
  puts "attached: TIMEOUT! #{e.class} - #{e.message}"
  :timeout
rescue => e
  puts "attached: ERROR: #{e.class} - #{e.message}"
end

def sleep_no_rescue(duration_ms)
  sleep duration_ms.to_f / 1000
  :result_b
end

js_context = MiniRacer::Context.new(timeout: 10)
js_context.attach("sleep_rescue", method(:sleep_rescue))
js_context.attach("sleep_no_rescue", method(:sleep_no_rescue))

started = Time.now
puts "sleep_rescue:"
begin
  result = js_context.call("sleep_rescue", 1000)
  puts "RESULT: #{result}"
rescue => e
  puts "ERROR: #{e.class} - #{e.message}"
end
puts "==> #{Time.now - started} sec"
puts

started = Time.now
puts "sleep_no_rescue:"
begin
  result = js_context.call("sleep_no_rescue", 1000)
  puts "RESULT: #{result}"
rescue => e
  puts "ERROR: #{e.class} - #{e.message}"
end
puts "==> #{Time.now - started} sec"

resulting in this printed:

RUBY_VERSION : 3.3.1
RUBY_PLATFORM: arm64-darwin23
MiniRacer::VERSION: 0.12.0
Libv8::Node::LIBV8_VERSION: 11.8.172.17
================================================================================
sleep_rescue:
attached: TIMEOUT! MiniRacer::ScriptTerminatedError - Terminated during callback
RESULT: timeout
==> 2.01841 sec

sleep_no_rescue:
ERROR: MiniRacer::ScriptTerminatedError - Terminated during callback
==> 0.012784 sec

from mini_racer.

dvkch avatar dvkch commented on August 17, 2024

Thank you for such a detailed answer ! I had completely missed the fact that the timeout was in ms. I am very curious as to why the rescue would also impact such a delay, exceptions are often costly, but I wouldn't have imagined to that extent 🤔

from mini_racer.

tisba avatar tisba commented on August 17, 2024

I have not figured out where the time is spent exactly, but it is not in the Ruby method (added some crude debug statements in mini_racer directly). I'm suspecting this is happening in C-land somewhere, but that's out of reach for me to understand further.

That might be something @SamSaffron or @lloeki might be able to help better. IMO the huge delay is a bug.

from mini_racer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.