-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
skip raise_unsupported_coercion for nil / performance issue #16
Comments
👍 for this, we're seeing a doubling of CPU usage due to the number of exceptions being raised an caught. If you just returned |
FWIW, we're using this monkeypatch to solve the issue in our Rails app: # config/initializers/coercible_monkeypatch.rb
# significant performance improvement by not raising exceptions when we can't coerce a value
# it's trapped and returned as "value" by Virtus anyway! 50% CPU improvement
require 'coercible'
module Coercible
class Coercer
class Object
def coerce_with_method(value, method, ref_method)
value.respond_to?(method) ? value.public_send(method) : value #was: raise_unsupported_coercion(value, ref_method)
end
end
end
end |
That's my patch. It's same problem but solved at Virtus level https://gist.github.com/kml/da4f7cf70008986b3ba5 |
👍 This issue just bit my team as well =( We were on JRuby which makes those very painful. Saved us 3 seconds, ~2k exceptions |
Also bit in jruby/jruby#4540 |
I spent some time looking at this today and it's definitely not trivial to optimize this for virtus while not possibly breaking other clients. In particular, this exception is part of coercible's interface, so just removing it may be ok for virtus use only, but not ok if some other gem relies on coercible. The same thing happens on virtus' side: the coercers can be configurable through virtus so just assuming that you don't have a custom coercer means that we could be stepping on someone else's toes. I'm kinda lost. And since virtus and coercible are not really in active development I don't think this will ever get a good fix. Probably we'll just have to look somewhere else, or just use a hack and pray that it never has a bad interaction. |
Ok so I've been revisiting this issue and have a workaround that doesn't change the semantics of the library, and that manages to avoid monkey patching. If someone is still interested in this, see #23 :) |
When a value is
nil
Coercible::UnsupportedCoercion
is raised. This raise is a real expensive operation. When I simply create a virtus model 1000.times I get the following profile resultfor this test program:
It would be great if there is an shortcut for nil or performance increase.
Partly duplicate of #15
The text was updated successfully, but these errors were encountered: