Skip to content

Conversation

Copy link

Copilot AI commented Oct 16, 2025

Problem

The Airports gem was using inefficient O(n) linear searches through all 6,076 airports for lookups by IATA and ICAO codes, causing poor performance especially when these methods are called frequently. Additionally, the icao_codes method was regenerating its result array on every call.

Solution

Implemented three targeted performance optimizations:

1. find_by_iata_code - Direct Hash Lookup

Before: Iterated through all airports using all.find { |airport| airport.iata == iata_code }

After: Direct O(1) hash lookup using parsed_data[iata_code] since the data is already keyed by IATA code

2. find_by_icao_code - ICAO Index

Before: Iterated through all airports using all.find { |airport| airport.icao == icao_code }

After: Introduced a cached icao_index that maps ICAO codes to airport data for O(1) lookups

3. icao_codes - Memoization

Before: Regenerated the array on every call with parsed_data.values.map { ... }

After: Added memoization with @icao_codes ||= to cache the result

Performance Improvements

Method Before (1000 calls) After (1000 calls) Improvement
find_by_iata_code 33.1ms 3.7ms 8.9x faster
find_by_icao_code 33.0ms 5.8ms 5.7x faster
icao_codes (100 calls) 61.0ms 0.6ms 101x faster

Testing

  • All 23 existing tests continue to pass
  • Added 3 new performance regression tests to prevent future slowdowns
  • Verified all methods work correctly with manual testing
  • Rubocop passes with no offenses

Backward Compatibility

All changes are 100% backward compatible. No public APIs were modified, and all existing behavior is preserved.

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:10
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 performance issues and apply fixes Optimize performance with hash lookups and caching for airport searches 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