将部分转换为方法/块以提高速度 [英] Convert a partial to method/block for speed

查看:79
本文介绍了将部分转换为方法/块以提高速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个呈现部分的循环

I have a loop that renders a partial

1000.times do |i|
  render partial: 'test', locals: {i: i}
end

这真的很慢,foreach 渲染调用最多 0.1 毫秒,即使部分只打印出 i

this is really slow, up to 0.1 ms for foreach render call, even if the partial only prints out i

my_partial = render_to_method(partial: 'test')
1000.times do |i|
  my_partial(locals: {i: i})
end

现在这应该以更快的方式做同样的事情吧?但我不知道该怎么做.

Now this should be doing the same thing way faster right? But I don't know how to do this.

更新:

我试过这样做:

Haml::Engine.new(File.read(File.expand_path File.dirname(FILE)+"/../table/#{column.macro}/_#{column.display_type}.haml"))
.‌​render(OpenStruct.new({column_value: column_value, object: object})) 

两大缺点:

  • 视图的路径不会像使用渲染时那样监视回退(parital: 'partial' 将在当前视图目录、某些 gem 以及视图/应用程序中查找 parital.
  • Rails 视图助手不再可用

更新 2:

许多答案试图通过使用其他技术来解决问题.我的实际应用无法应用此技术.https://github.com/antpaw/bhf/blob/master/app/views/bhf/pages/_platform.haml#L54‌ 主要是因为这一行是一个动态字符串,有时会链接到甚至不存在于 gem 中的部分在 main_app 中定义.我想知道为什么做一些如此基本的事情如此困难: 1. 抓住视图.2. 渲染它.(3. 再次渲染.)

Many of the answers try to solve the problem by using other techniques. I my real application this techniques can't be applied. https://github.com/antpaw/bhf/blob/master/app/views/bhf/pages/_platform.haml#L54‌​ mainly because this line is a dynamic string that sometimes links to partials that don't even exsist in the gem and are defined in the main_app. I wonder why it's so hard to do something that is so basic: 1. grab the view. 2. render it. (3. render it again.)

更新 3:

@Flash Gordon 建议了这个 https://gist.github.com/antpaw/d6670c23d6f08e35812e#file-gistfile1-haml-L54

@Flash Gordon suggested this https://gist.github.com/antpaw/d6670c23d6f08e35812e#file-gistfile1-haml-L54

template = lookup_context.find_template "#{column.macro}/#{column.display_type}", ['bhf/table'], true
template.render(self, {column_value: column_value, object: object})

它几乎可以工作,只是在 locals 上遇到了一些麻烦.但是已经感觉抓模板的部分和渲染部分已经很好的分离了.

it almost works just having some troubles with locals. But it already feels like the part where you grab the template and the render part is well separated.

推荐答案

以下是我之前回答中的一个示例.它是从 PartialRenderer 来源中提取的.

Here is an example from one of my previous answers. It's extracted from PartialRenderer sources.

- local_names = [:i]
- partials = {}
- 1000.times do |i|
  - name = 'name_%s' % (i % 10)
  - partials[name] ||= lookup_context.find_template(name, lookup_context.prefixes, true, local_names)
  = partials[name].render(self, i: i)

我建议您使用辅助方法包装它.请记住,本地人的名字出现在这里两次:第一个在 local_names 中作为数组,第二个在 hash 的键中作为 #render 方法.

I'd recommend you to wrap it with a helper method. Keep in mind that locals' names appear here twice: first in local_names as an array and second in hash's keys passed as the second argument of #render method.

这篇关于将部分转换为方法/块以提高速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆