-
-
Notifications
You must be signed in to change notification settings - Fork 15
Optimize IcaoDecoder.isMilitary() by eliminating regex recompilation #291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Optimize IcaoDecoder.isMilitary() by eliminating regex recompilation #291
Conversation
Fixed a critical performance issue where 30+ regex patterns were being compiled on every call to isMilitary(). The patterns are now compiled once as static readonly class fields and reused via .test() method. Performance impact: - Before: 30+ regex compilations per isMilitary() call - After: Zero regex compilations (patterns compiled once at class load) This significantly improves performance for high-throughput ACARS message processing scenarios. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Bug Report
Comments? Email us. |
|
Warning Rate limit exceeded@makrsmark has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 19 minutes and 38 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (2)
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughReplaced regex-based military ICAO detection with numeric-range checks by adding a private static Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20–30 minutes
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
lib/IcaoDecoder.ts (1)
108-109: Consider more descriptive variable names.While the local aliases reduce verbosity, single-letter names like
iandpreduce clarity. Consider using more descriptive names:- const i = this.icao; - const p = IcaoDecoder.MILITARY_PATTERNS; + const icaoAddress = this.icao; + const patterns = IcaoDecoder.MILITARY_PATTERNS;Then update the references in the return statement accordingly (e.g.,
patterns.US_MIL_1.test(icaoAddress)).
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
lib/IcaoDecoder.ts(2 hunks)
🔇 Additional comments (2)
lib/IcaoDecoder.ts (2)
5-100: Excellent performance optimization!The refactoring to static readonly pre-compiled patterns is exactly the right approach. This eliminates 30+ regex compilations per method call and improves performance for high-throughput scenarios.
111-229: Pattern usage is correct and efficient.The refactored code correctly uses
.test()method with the pre-compiled patterns. The logic flow and control structure are preserved while achieving the performance optimization goals.Note: Some ranges still use string comparisons (lines 133, 212), which is fine and was present in the original code. String comparison works correctly for hex address ranges due to lexicographic ordering.
lib/IcaoDecoder.ts
Outdated
| const i = this.icao; | ||
| const p = IcaoDecoder.MILITARY_PATTERNS; | ||
|
|
||
| return ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make MILITARY_PATTERNS an array of regexp instead of a dictionary and then just return MILITARY_PATTERNS.some((p) => p.test(i));
all the comments can move up to the definition
lib/IcaoDecoder.ts
Outdated
| isMilitary() { | ||
| let i = this.icao; | ||
| const i = this.icao; | ||
| const p = IcaoDecoder.MILITARY_PATTERNS; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you take my other comment, remove as it's not saving anything
Bug Report
Comments? Email us. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR refactors the IcaoDecoder.isMilitary() method to eliminate a critical performance bottleneck by replacing 30+ regex pattern compilations per call with a static array of pre-compiled numeric ranges. The implementation now uses efficient integer range comparisons instead of repeated regex matching.
Key Changes:
- Replaced regex-based pattern matching with numeric range comparisons using a static lookup array
- Converted military ICAO ranges to a structured array of hex tuples for O(n) range checking
- Added comprehensive test coverage for the refactored
isMilitary()method across all military ranges
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| lib/IcaoDecoder.ts | Refactored isMilitary() to use static numeric range comparisons instead of regex patterns, significantly improving performance |
| lib/IcaoDecoder.test.ts | Added comprehensive test suite covering all military ICAO ranges with boundary and edge case testing |
| lib/plugins/Label_14.test.ts | Added test file for Label_15 position report decoding with multiple message format variants |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
lib/plugins/Label_14.test.ts
Outdated
| import { MessageDecoder } from '../MessageDecoder'; | ||
| import { Label_15 } from './Label_15'; | ||
|
|
||
| describe('Label_15', () => { |
Copilot
AI
Dec 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test file is named Label_14.test.ts but imports and tests Label_15. The filename should be Label_15.test.ts to match the plugin being tested.
lib/plugins/Label_14.test.ts
Outdated
| }); | ||
| }); | ||
|
|
||
| test('decodes short variant missing unkown', () => { |
Copilot
AI
Dec 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected spelling of 'unkown' to 'unknown'.
| test('decodes short variant missing unkown', () => { | |
| test('decodes short variant missing unknown', () => { |
lib/plugins/Label_14.test.ts
Outdated
| expect(decodeResult.remaining.text).toBe('--- 42'); | ||
| }); | ||
|
|
||
| test('decodes off variant no unkown', () => { |
Copilot
AI
Dec 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected spelling of 'unkown' to 'unknown'.
| test('decodes off variant no unkown', () => { | |
| test('decodes off variant no unknown', () => { |
Bug ReportName: Incorrect exclusion range for Australia military ICAO addresses Comments? Email us. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Bug Report
Comments? Email us. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (3)
lib/IcaoDecoder.ts (1)
5-59: Numeric range–basedisMilitarylooks correct and achieves the perf goalThe static
MILITARY_RANGEStable plus the simple numeric range scan give you exactly oneparseIntand an O(N) check with early exit, and the ranges line up with the new tests (including the Austria/Australia exceptions). This is a solid improvement over per-call regex compilation.Two entirely optional tweaks you might consider later:
- Add a small guard for malformed ICAO strings (e.g.,
if (Number.isNaN(n)) return false;), just to make the intent explicit that non-hex / partially-hex inputs are always treated as non‑military.- If you prefer a more declarative style, you could express the loop as
return IcaoDecoder.MILITARY_RANGES.some(([start, end]) => n >= start && n <= end);without changing behavior.Also applies to: 66-75
lib/IcaoDecoder.test.ts (1)
3-302: StrongisMilitarycoverage; consider tightening one test descriptionThe test suite does a good job exercising boundaries and “just outside” values for all of the configured ranges, plus some clear negative cases — this should make future changes to
MILITARY_RANGESrelatively safe.One small consistency nit: the Germany test case is named
'should match 3e8000-3ebfff', butMILITARY_RANGESencodes[0x3ea000, 0x3ebfff], and you only assert values starting at3ea000. If3ea000is indeed the intended lower bound, it may be worth updating the description (or adding an assertion for3e8000if that’s supposed to be included) to avoid confusion for future readers.lib/plugins/Label_14.test.ts (1)
1-123: Label_15 tests align well with decode logic; only minor naming nitsThese tests exercise all the key branches of
Label_15.decode(short vs OFF variants, with/without date/altitude, and the invalid-path handling) and validate both structured fields andremaining.text, which should give good confidence in the plugin behavior.Two minor, purely cosmetic nits you might consider when you next touch this file:
- The file is named
Label_14.test.tsbut only testsLabel_15; renaming it toLabel_15.test.tswould make it easier to find by convention.- A couple of test descriptions say
unkowninstead ofunknown, which can make searching slightly less predictable.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
lib/IcaoDecoder.test.ts(1 hunks)lib/IcaoDecoder.ts(1 hunks)lib/plugins/Label_14.test.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
lib/plugins/Label_14.test.ts (2)
lib/plugins/Label_15.ts (1)
Label_15(8-61)lib/utils/result_formatter.ts (1)
text(386-394)
lib/IcaoDecoder.test.ts (1)
lib/IcaoDecoder.ts (1)
IcaoDecoder(1-77)
|
An error occured. This error may be due to rate limits. If this error persists, please email us. |
Bug Report
Comments? Email us. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Bug Report
Comments? Email us. |
Fixed a critical performance issue where 30+ regex patterns were being compiled on every call to isMilitary(). The patterns are now compiled once as static readonly class fields and reused via .test() method.
Performance impact:
This significantly improves performance for high-throughput ACARS message processing scenarios.
🤖 Generated with Claude Code
Summary by CodeRabbit
Refactor
Tests
✏️ Tip: You can customize this high-level summary in your review settings.