[PR #3391] Improve Base62 encoding/decoding performance and robustness #3717

Open
opened 2025-11-20 08:06:54 -05:00 by saavagebueno · 0 comments
Owner

Original Pull Request: https://github.com/netbirdio/netbird/pull/3391

State: open
Merged: No


Describe your changes

Performance & Robustness Enhancements

  • Optimize Base62 decoding by replacing strings.IndexRune with a fixed-size array for O(1) lookups, reducing memory overhead.
  • Reduce heap allocations and improve Base62.Encode performance by avoiding dynamic memory usage for small, fixed-size data.
  • Use iterative multiplication instead of math.Pow() to avoid floating-point precision issues.
  • Add an overflow check when calculating the decoded value to prevent silent overflow of uint32, ensuring correctness for long input strings.

Testing Improvements:

  • Enhance unit tests by explicitly verifying encoding and decoding accuracy.
  • Add unit tests for Base62 decoding: one for overflow handling and another for empty strings.
  • Add a unit test for input strings containing invalid characters.

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)
  • Extended the README / documentation, if necessary
**Original Pull Request:** https://github.com/netbirdio/netbird/pull/3391 **State:** open **Merged:** No --- ## Describe your changes ### Performance & Robustness Enhancements - Optimize Base62 decoding by replacing strings.IndexRune with a fixed-size array for O(1) lookups, reducing memory overhead. - Reduce heap allocations and improve Base62.Encode performance by avoiding dynamic memory usage for small, fixed-size data. - Use iterative multiplication instead of math.Pow() to avoid floating-point precision issues. - Add an overflow check when calculating the decoded value to prevent silent overflow of uint32, ensuring correctness for long input strings. ### Testing Improvements: - Enhance unit tests by explicitly verifying encoding and decoding accuracy. - Add unit tests for Base62 decoding: one for overflow handling and another for empty strings. - Add a unit test for input strings containing invalid characters. ## Issue ticket number and link ### Checklist - [x] Is it a bug fix - [ ] Is a typo/documentation fix - [x] Is a feature enhancement - [x] It is a refactor - [x] Created tests that fail without the change (if possible) - [ ] Extended the README / documentation, if necessary
saavagebueno added the pull-request label 2025-11-20 08:06:54 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: SVI/netbird#3717