2016-11-22 5 views
0

私はRailsアプリケーションを作成しましたが、多くのインスタンス変数を使用していましたが、そのほとんどはビューには必要ありません。パフォーマンスを向上させるために、未使用のインスタンス変数を置き換える必要がありますか?インスタンス変数の代わりにローカル変数を使用して、Railsのパフォーマンス(メモリを節約)を改善していますか?

サンプルコード:

def show 

custom_fields_data = fetch_custom_field_data 

@selected_custom_fields_opt_from_view = [] 
if custom_fields_data.present? 
    @listings = @listing.category.listings.where("price_cents!=? AND open= ?",0,true).reject { |l| l.author.main_admin? } 

    @selected_custom_fields_opt_from_view = custom_fields_data.map do |custom_field_data| 
    CustomField.find(custom_field_data[0]).options.find(custom_field_data[1]) 
    end 


    @listings.each do |listing| 

    # array to store the selected a custom field's option from Database 
    selected_custom_fields_opt_from_db = [] 

    listing.custom_field_values.each do |custom_field_value| 
     selected_custom_fields_opt_from_db.push(custom_field_value.selected_options.first) 
    end 
    if selected_custom_fields_opt_from_db.uniq.sort == @selected_custom_fields_opt_from_view.uniq.sort || (@selected_custom_fields_opt_from_view - selected_custom_fields_opt_from_db).empty? 
     similar_listing.push(listing) 
    end 
    end 

    @listings = similar_listing 

end 


@listing_with_filters = similar_listing.present? ? @listings.first : @listing 

@selected_tribe_navi_tab = "home" 
unless current_user?(@listing.author) 
    @listing.increment!(:times_viewed) 
end 

@current_image = if params[:image] 
        @listing.image_by_id(params[:image]) 
       else 
        @listing.listing_images.first 
       end 

@prev_image_id, @next_image_id = if @current_image 
            @listing.prev_and_next_image_ids_by_id(@current_image.id) 
           else 
            [nil, nil] 
           end 

payment_gateway = MarketplaceService::Community::Query.payment_type(@current_community.id) 
process = get_transaction_process(community_id: @current_community.id, transaction_process_id: @listing.transaction_process_id) 
form_path = new_transaction_path(listing_id: @listing.id) 

delivery_opts = delivery_config(@listing.require_shipping_address, @listing.pickup_enabled, @listing.shipping_price, @listing.shipping_price_additional, @listing.currency) 

@category = @listing.category 
@template_listing = @category.template_listing 
if @current_user 
    # For Pivot table 
    @selected_custom_field = params[:custom_field] if params[:custom_field] 
    @listing_for_pivot = Listing.new 
    @listing_images = @listing.listing_images 
    @shape = get_shape(@listing.listing_shape_id) 
    @unit_options = ListingViewUtils.unit_options(@shape[:units], unit_from_listing(@template_listing)).first if @shape 
    @custom_field_questions = @category.custom_fields 
    @numeric_field_ids = numeric_field_ids(@custom_field_questions) 
    @category_tree = CategoryViewUtils.category_tree(
     categories: ListingService::API::Api.categories.get(community_id: @current_community.id)[:data], 
     shapes: get_shapes, 
     locale: I18n.locale, 
     all_locales: @current_community.locales 
) 
    if @template_listing.present? 
    @listing_for_pivot.title = @template_listing.title 
    @listing_for_pivot.description = @template_listing.description 
    @listing_images = @template_listing.listing_images if @template_listing.listing_images.present? 
    @listing_for_pivot.listing_shape_id = @template_listing.listing_shape_id 
    end 
    if (@current_user.location != nil) 
    temp = @current_user.location 
    temp.location_type = "origin_loc" 
    @listing_for_pivot.build_origin_loc(temp.attributes) 
    else 
    @listing_for_pivot.build_origin_loc(:location_type => "origin_loc") 
    end 
    @custom_field_area = CategoryCustomField.where(category_id: @category.id, custom_field_id: @category.custom_fields.pluck(:id)) 

    @row = @category.custom_field_row 
    @row = @custom_field_area.first.custom_field if @row.nil? && @custom_field_area.first 

    @column = @category.custom_field_column 
    @column = @custom_field_area.second.custom_field if @column.nil? && @custom_field_area.second 

    @filters = @category.custom_field_filters 
    @filters = @custom_field_area.all.from(1).map { |category_custom_field| category_custom_field.custom_field } if @filters.nil? && @custom_field_area.size > 2 


    @selected_value_for_filter = [] 
    if @filters.present? 
    if @selected_custom_field 
     @filters.each do |filter| 
     if (@selected_custom_field["#{filter.id.to_s}_"]) 
      @selected_value_for_filter.push(filter.options.find(@selected_custom_field["#{filter.id.to_s}_"])) 
     else 
      @selected_value_for_filter.push(filter.options.first) 
     end 
     end 
    else 
     @filters.each do |filter| 
     @selected_value_for_filter.push(filter.options.first) 
     end 
    end 
    end 
    # Pivot table section end 
end 

@applicant = @category.listings.pluck(:author_id).uniq 
@suggested_business_accounts = @category.people.where("people.id NOT IN (?)", @applicant); 
if @suggested_business_accounts.present? 
    @business_locations = 
     @suggested_business_accounts.map do |person| 
     person.location 
     end 
    @business_locations.compact! 
end 

render locals: { 
    form_path: form_path, 
    payment_gateway: payment_gateway, 
    # TODO I guess we should not need to know the process in order to show the listing 
    process: process, 
    delivery_opts: delivery_opts, 
    listing_unit_type: @listing.unit_type 
} 
end 
+0

あなたの問題を見つけやすいようにコードを挿入できますか? –

+0

@ Walfie:サンプルコードを追加しました。ありがとうございます。 – anandh

+0

回答者があなたの問題を理解しやすくなります。 –

答えて

0

インスタンス変数の代わりに、ローカル変数を使用して、少なくともメモリワイズ悪い考えです。

インスタンス変数は、それを保持するオブジェクトが存在する間に存在します。逆に、ローカル変数は定義されたメソッド/ブロックの内部にのみ存在します。

ガベージコレクタは、メソッド以外の場所でインスタンス変数を使用するかどうかは気にしません。

したがって、メソッド内でのみ使用するインスタンス変数がある場合は、それらをローカル変数に変更します。

1

インスタンス変数をビューに送信したくない場合は、使用しないことをお勧めします。変数のスコープは最も狭くなければならないため、ビューでインスタンス変数を使用していない場合は、ビューをローカルに変換する必要があります。

関連する問題