防止 json/pure 在 Rails 中加载

发布于 2024-12-27 21:42:23 字数 365 浏览 4 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别忘他 2025-01-03 21:42:23

为了防止加载 json/pure,可以这样做:

require 'json/ext'
JSON.freeze

您必须在需要 json/pure 之前运行它。

To prevent json/pure from being loaded, this works:

require 'json/ext'
JSON.freeze

You have to run this before json/pure is required.

無處可尋 2025-01-03 21:42:23

不知道这是否适用于您的情况,并且治疗方法可能比疾病更糟糕:

module Kernel
  alias old_require require

  def require(path)
    if path =='json/pure'
       # do something appropriate
    else
       old_require path
    end
  end
end

当我想对 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:

module Kernel
  alias old_require require

  def require(path)
    if path =='json/pure'
       # do something appropriate
    else
       old_require path
    end
  end
end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文