Optimize performance with hash lookups and caching for airport searches #191
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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_codesmethod was regenerating its result array on every call.Solution
Implemented three targeted performance optimizations:
1.
find_by_iata_code- Direct Hash LookupBefore: 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 code2.
find_by_icao_code- ICAO IndexBefore: Iterated through all airports using
all.find { |airport| airport.icao == icao_code }After: Introduced a cached
icao_indexthat maps ICAO codes to airport data for O(1) lookups3.
icao_codes- MemoizationBefore: Regenerated the array on every call with
parsed_data.values.map { ... }After: Added memoization with
@icao_codes ||=to cache the resultPerformance Improvements
find_by_iata_codefind_by_icao_codeicao_codes(100 calls)Testing
Backward Compatibility
All changes are 100% backward compatible. No public APIs were modified, and all existing behavior is preserved.
Original prompt
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.