防止 json/pure 在 Rails 中加载
我正在编写一个 Rails 应用程序,使用“json”gem,它可以在“json/pure”和“json/ext”实现之间切换的行为导致了问题。具体来说,有时 gem 会引入这样的行:
require 'json/pure'
When thatgenesis, JSON.parser 和 JSON.generator 全局切换到缓慢的、基于 ruby 的版本。然后每次调用 JSON.parse 和 JSON.generate 都很慢。
我想避免这样的情况:在我不知情的情况下,不断变化的 gem 依赖项突然使我的网站速度变慢。如何防止加载“json/pure”、强制使用“json/ext”或以其他方式防止将来出现此问题?
I'm writing a Rails application, using the 'json' gem, and its behavior where it can switch between the 'json/pure' and 'json/ext' implementations is causing problems. Specifically, sometimes a gem introduces a line like
require 'json/pure'
When that happens, JSON.parser and JSON.generator switch to the slow, ruby-based version globally. Then every call to JSON.parse and JSON.generate are slow.
I want to avoid the situation where a changing gem dependency, unbeknownst to me, suddenly makes my site much slower. How can I prevent 'json/pure' from ever being loaded, enforce 'json/ext' usage, or otherwise prevent this problem in the future?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了防止加载 json/pure,可以这样做:
您必须在需要 json/pure 之前运行它。
To prevent json/pure from being loaded, this works:
You have to run this before json/pure is required.
不知道这是否适用于您的情况,并且治疗方法可能比疾病更糟糕:
当我想对 Rails 应用程序中各种启动瓶颈的性能进行基准测试时,我已经对 require 进行了猴子修补。也许您可以明白为什么这些 gem 试图要求“json/pure”,并可能以另一种方式阻止它。
我发现 gem 源代码通常非常可读。捆绑打开对我有用。
Don't know if this will work in your case, and the cure could be worse than the disease:
I've monkey patched require when I wanted to benchmark the performance of various startup bottlenecks in our rails app. Perhaps you could see why these gems are trying to require 'json/pure' and perhaps stop it another way.
I find gem source code to be generally very readable. bundle open does the trick for me.