solnic / coercible

Powerful, flexible and configurable coercion library. And nothing more.
MIT License
137 stars 16 forks source link

Improve unsupported coercion performance by optionally caching error #23

Closed ivoanjo closed 1 year ago

ivoanjo commented 7 years ago

As identified in https://github.com/solnic/coercible/issues/16 and https://github.com/jruby/jruby/issues/4540, coercible's use of errors to signal unsupported coercions has a measurable impact on performance, especially when using JRuby.

To workaround this problem, this PR adds the option (controlled by the setting the COERCIBLE_CACHE_UNSUPPORTED_COERCION_ERROR environment variable to '1', off by default) to reuse a cached error object, rather than raising a new instance every time. This saves a big slice of the cost of using errors, but trades it off with having a useless backtrace -- all UnsupportedCoercion errors will share the same backtrace.

It also includes a benchmark that can be used to measure the impact of this change. Here are some results of the benchmark on my local machine (Core i7-6500U, Ubuntu 17.04):