[PR #205] [MERGED] perf(netstack2): optimize reverse NAT lookup with O(1) map #382

Closed
opened 2026-04-12 06:40:43 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/fosrl/newt/pull/205
Author: @LaurenceJJones
Created: 12/16/2025
Status: Merged
Merged: 3/3/2026
Merged by: @oschwartz10612

Base: devHead: optimize-reverse-nat-lookup


📝 Commits (2)

  • 1cf75b0 perf: optimize reverse NAT lookup with O(1) map instead of O(n) iteration
  • 5977667 Merge branch 'fosrl:main' into optimize-reverse-nat-lookup

📊 Changes

1 file changed (+43 additions, -15 deletions)

View changed files

📝 netstack2/proxy.go (+43 -15)

📄 Description

Community Contribution License Agreement

By creating this pull request, I grant the project maintainers an unlimited,
perpetual license to use, modify, and redistribute these contributions under any terms they
choose, including both the AGPLv3 and the Fossorial Commercial license terms. I
represent that I have the right to grant this license for all contributed content.

Description

Replace O(n) linear search through NAT table with O(1) reverse lookup map for reply packet NAT translation.

Changes:

  • Add reverseConnKey type for reverse NAT lookups
  • Add reverseNatTable map to ProxyHandler for O(1) lookups
  • Populate both forward and reverse maps when creating NAT entries
  • Replace iteration-based reverse lookup with direct map access

Performance:

  • O(n) → O(1) complexity for reverse NAT lookups
  • Eliminates lock-held iteration on every reply packet
  • Removes string comparisons from hot path
  • Expected 10-50x improvement for reverse NAT lookups

This addresses Critical from performance analysis where reply path was walking the entire NAT table to find original mapping.

How to test?

Purely internal optimization, instead of walking the entire natTable every time to find where to reverse too. We create a map that is indexed by the reverse fields which stores the same netState data from the forward natTable.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/fosrl/newt/pull/205 **Author:** [@LaurenceJJones](https://github.com/LaurenceJJones) **Created:** 12/16/2025 **Status:** ✅ Merged **Merged:** 3/3/2026 **Merged by:** [@oschwartz10612](https://github.com/oschwartz10612) **Base:** `dev` ← **Head:** `optimize-reverse-nat-lookup` --- ### 📝 Commits (2) - [`1cf75b0`](https://github.com/fosrl/newt/commit/1cf75b00ff8022832dc4d4c9f2915fa87a5d5e6c) perf: optimize reverse NAT lookup with O(1) map instead of O(n) iteration - [`5977667`](https://github.com/fosrl/newt/commit/5977667291917aaf623a301da11601345b2d32dd) Merge branch 'fosrl:main' into optimize-reverse-nat-lookup ### 📊 Changes **1 file changed** (+43 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `netstack2/proxy.go` (+43 -15) </details> ### 📄 Description ## Community Contribution License Agreement By creating this pull request, I grant the project maintainers an unlimited, perpetual license to use, modify, and redistribute these contributions under any terms they choose, including both the AGPLv3 and the Fossorial Commercial license terms. I represent that I have the right to grant this license for all contributed content. ## Description Replace O(n) linear search through NAT table with O(1) reverse lookup map for reply packet NAT translation. Changes: - Add reverseConnKey type for reverse NAT lookups - Add reverseNatTable map to ProxyHandler for O(1) lookups - Populate both forward and reverse maps when creating NAT entries - Replace iteration-based reverse lookup with direct map access Performance: - O(n) → O(1) complexity for reverse NAT lookups - Eliminates lock-held iteration on every reply packet - Removes string comparisons from hot path - Expected 10-50x improvement for reverse NAT lookups This addresses Critical from performance analysis where reply path was walking the entire NAT table to find original mapping. ## How to test? Purely internal optimization, instead of walking the entire natTable every time to find where to reverse too. We create a map that is indexed by the reverse fields which stores the same netState data from the forward natTable. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-12 06:40:43 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/newt#382