<p dir="auto"><code class="notranslate">preferred_languages.expand</code> in browse_helper.rb is a bit expensive, due to the Locale handling that's being triggered for every single element (see stacktrace below).</p>
<p dir="auto">However, preferred_languages are not expected to change during a request. They seem like a good candidate for caching. preferred_languages is already cached in the application_controller, but the expanded version is not.</p>
<p dir="auto">Caching should work on a request basis. After the request is done, the instance variable should be empty. Unit tests somehow don't have a concept of a request, that's why I had to manually set the value back to nil.</p>
<p dir="auto">In case an element doesn't have any tags, there isn't much point calling <code class="notranslate">feature_name</code>. Maybe this empty tags check could be moved from <code class="notranslate">printable_element_name</code> inside <code class="notranslate">feature_name</code>?</p>
<p dir="auto">I've seen about 50% performance improvement on top of <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="3339084824" data-permission-text="Title is private" data-url="https://github.com/openstreetmap/openstreetmap-website/issues/6338" data-hovercard-type="pull_request" data-hovercard-url="/openstreetmap/openstreetmap-website/pull/6338/hovercard" href="https://github.com/openstreetmap/openstreetmap-website/pull/6338">#6338</a> by caching preferred_languages this way.</p>
<a href="https://github.com/user-attachments/assets/795494ab-366b-4dc9-9b59-4a6e9988e72c">locale_keys.png (view on web)</a>

<hr>

<h4>You can view, comment on, or merge this pull request online at:</h4>
<p>  <a href='https://github.com/openstreetmap/openstreetmap-website/pull/6344'>https://github.com/openstreetmap/openstreetmap-website/pull/6344</a></p>

<h4>Commit Summary</h4>
<ul>
  <li><a href="https://github.com/openstreetmap/openstreetmap-website/pull/6344/commits/b2dafd774e1626923678d0c4ee85fc88099c0684" class="commit-link">b2dafd7</a>  Cache preferred_languages</li>
</ul>

<h4 style="display: inline-block">File Changes </h4> <p style="display: inline-block">(<a href="https://github.com/openstreetmap/openstreetmap-website/pull/6344/files">2 files</a>)</p>
<ul>
  <li>
    <strong>M</strong>
    <a href="https://github.com/openstreetmap/openstreetmap-website/pull/6344/files#diff-32118282fc2a82b607c0052b4a7bf90a81869cfae973c63e9552534800c14525">app/helpers/browse_helper.rb</a>
    (8)
  </li>
  <li>
    <strong>M</strong>
    <a href="https://github.com/openstreetmap/openstreetmap-website/pull/6344/files#diff-b68a09a5dc0e0cffcd99b27761111b06a86bb45001912b9fabf3618a3dc464d1">test/helpers/browse_helper_test.rb</a>
    (3)
  </li>
</ul>

<h4>Patch Links:</h4>
<ul>
  <li><a href='https://github.com/openstreetmap/openstreetmap-website/pull/6344.patch'>https://github.com/openstreetmap/openstreetmap-website/pull/6344.patch</a></li>
  <li><a href='https://github.com/openstreetmap/openstreetmap-website/pull/6344.diff'>https://github.com/openstreetmap/openstreetmap-website/pull/6344.diff</a></li>
</ul>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />Reply to this email directly, <a href="https://github.com/openstreetmap/openstreetmap-website/pull/6344">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AAK2OLOOI7ZZQMR4CHXNB7T3O527ZAVCNFSM6AAAAACES5DTZGVHI2DSMVQWIX3LMV43ASLTON2WKOZTGM2DMNJYGM4TSNI">unsubscribe</a>.<br />You are receiving this because you are subscribed to this thread.<img src="https://github.com/notifications/beacon/AAK2OLJKBQ72TJJELZ2FRTT3O527ZA5CNFSM6AAAAACES5DTZGWGG33NNVSW45C7OR4XAZNFJFZXG5LFVJRW63LNMVXHIX3JMTHMO6GRXM.gif" height="1" width="1" alt="" /><span style="color: transparent; font-size: 0; display: none; visibility: hidden; overflow: hidden; opacity: 0; width: 0; height: 0; max-width: 0; max-height: 0; mso-hide: all">Message ID: <span><openstreetmap/openstreetmap-website/pull/6344</span><span>@</span><span>github</span><span>.</span><span>com></span></span></p>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/openstreetmap/openstreetmap-website/pull/6344",
"url": "https://github.com/openstreetmap/openstreetmap-website/pull/6344",
"name": "View Pull Request"
},
"description": "View this Pull Request on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>