Skip to content

country_select method causing a ton of object allocation in views #226

Description

@ykamin-booth

I have recently been troubleshooting memory bloat in my app and discovered that views that included the country_select method in forms were causing high object allocation--significantly higher than when I just manually replaced the method with a large select tag.

Here's an example form (new devise registration) and how I use country_select

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name),  html: { multipart: true, novalidate: false}) do |f| %>
  <%= f.error_notification %>
  <div class="form-group">
    <%= f.input :email,
        required: true,
        autofocus: false,
        hint: "This will also be your Username",
        input_html: { autocomplete: "email", class: "form-control-no-validations form-control form-control-lg" }%>
   </div>
   <div class="form-group">
    <%= f.input :name,
       required: true,
       input_html: {autocomplete: "name", class: "form-control-no-validations form-control form-control-lg" }%>
    </div>
    <p class="mb-2">Phone Number</p>
    <div class="form-group">
    <%= country_select(nil, "country_code", { selected: @country_code, include_blank: true, format: :with_country_code}, { class: 'custom-select', id: 'phone_country_codes', style: 'width: 30%;' } ) %>
...

The memory trace in Skylight shows the following when this page is loaded:
image

When I replace the method with a select tag like this:

<select class="custom-select" id="phone_country_codes" style="width: 30%;" name="country_code"><option value="" label=" "></option>
<option value="AS">+1 (American Samoa)</option>
<option value="AI">+1 (Anguilla)</option>
<option value="AG">+1 (Antigua and Barbuda)</option>
<option value="BS">+1 (Bahamas)</option>
<option value="BB">+1 (Barbados)</option>
<option value="BM">+1 (Bermuda)</option>
<option value="CA">+1 (Canada)</option>
<option value="KY">+1 (Cayman Islands)</option>
<option value="DM">+1 (Dominica)</option>
<option value="DO">+1 (Dominican Republic)</option>
<option value="GD">+1 (Grenada)</option>
<option value="GU">+1 (Guam)</option>
<option value="JM">+1 (Jamaica)</option>
...

I get avg. allocations on the order of 15,000.

Do you know what accounts for this? It happens whether or not I use the custom format and with country_select version 8 or 9.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions