Skip to content

Optimize lookup methods for significant performance improvements (7-58x faster)#192

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/audit-performance-issues-3
Draft

Optimize lookup methods for significant performance improvements (7-58x faster)#192
Copilot wants to merge 4 commits intomainfrom
copilot/audit-performance-issues-3

Conversation

Copy link
Copy Markdown

Copilot AI commented Oct 16, 2025

Overview

This PR addresses performance bottlenecks in the airports gem by optimizing the most frequently used lookup methods. The changes convert O(n) linear searches to O(1) hash lookups and add memoization where appropriate, resulting in 7-58x performance improvements while maintaining full backward compatibility.

Performance Improvements

1. find_by_iata_code - ~7x faster

  • Before: Used all.find to iterate through all 6,076 airports (O(n))
  • After: Direct hash lookup using parsed_data[iata_code] (O(1))
  • Impact: 0.328s → 0.047s for 10,000 lookups

Since the data is already structured as a hash with IATA codes as keys, we can directly access airports without iterating through the entire collection.

2. find_by_icao_code - ~10x faster

  • Before: Used all.find to iterate through all airports (O(n))
  • After: Created an ICAO index for O(1) hash lookups
  • Impact: 0.334s → 0.032s for 10,000 lookups

Added a new private icao_index method that builds a memoized hash mapping ICAO codes to airport data, enabling instant lookups.

3. icao_codes - ~58x faster

  • Before: Recreated the array on every call
  • After: Memoized the result with @icao_codes ||=
  • Impact: 0.058s → 0.001s for 100 calls

The array is now computed once and cached for subsequent calls.

Code Quality Improvements

  • Added defensive nil checks in icao_index to prevent indexing invalid data
  • Added .compact to icao_codes to filter out any potential nil values
  • Removed outdated TODO comment about Ruby 2.5+ support
  • Updated .gitignore to exclude vendor/bundle/

Testing

  • ✅ All 23 existing tests pass
  • ✅ No Rubocop offenses detected
  • ✅ Backward compatibility maintained (no API changes)
  • ✅ Performance verified with benchmarks

Technical Details

The optimizations leverage the existing data structure more efficiently:

# IATA lookup: Direct hash access
airport_data = parsed_data[iata_code]
airport_from_parsed_data_element(airport_data) if airport_data

# ICAO lookup: Uses memoized index
def self.icao_index
  @icao_index ||= parsed_data.values.each_with_object({}) do |airport_data, index|
    icao = airport_data["icao"]
    index[icao] = airport_data if icao
  end
end

# ICAO codes: Memoized array
@icao_codes ||= parsed_data.values.map { |airport_data| airport_data["icao"] }.compact

These changes are surgical and minimal, affecting only the performance-critical paths without changing the public API or breaking existing functionality.

Original prompt

Audit for potential performance issues and apply fixes

Created from VS Code via the GitHub Pull Request extension.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits October 16, 2025 06:08
Co-authored-by: timrogers <116134+timrogers@users.noreply.github.com>
Co-authored-by: timrogers <116134+timrogers@users.noreply.github.com>
Co-authored-by: timrogers <116134+timrogers@users.noreply.github.com>
Copilot AI changed the title [WIP] Audit for potential performance issues and apply fixes Optimize lookup methods for significant performance improvements (7-58x faster) Oct 16, 2025
Copilot AI requested a review from timrogers October 16, 2025 06:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants