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

Open
opened 2026-08-05 06:06:02 -04:00 by saavagebueno · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbirdio/netbird/pull/3391
Author: @redouan-rhazouani
Created: 2/26/2025
Status: 🔄 Open

Base: mainHead: fix_perf_base62


📝 Commits (7)

  • dc8eb40 Enhance Base62 unit tests by explicitly verifying encoding and decoding accuracy
  • 757e178 Reduce heap allocations and improves performance of base62.Encode by avoiding dynamic memory usage for small, fixed-size data
  • 88189c5 [Base62.Decode] use iterative multiplication instead of math.Pow() to avoid less precise floating point operations
  • 8fa6021 [Base62.Decode] return error if input string is empty
  • 1bad771 Add overflow check when calculating the decoded value to prevent silent overflow of uint32
  • 2981ce5 [Base62.Decode] add unit test for input strings containing invalid character
  • 0c6e9f1 [Base62.Decode] optimize decoding by replacing strings.IndexRune with a fixed-size array for O(1) lookups, improving performance and reducing memory overhead

📊 Changes

2 files changed (+100 additions, -45 deletions)

View changed files

📝 base62/base62.go (+50 -31)
📝 base62/base62_test.go (+50 -14)

📄 Description

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

🔄 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/netbirdio/netbird/pull/3391 **Author:** [@redouan-rhazouani](https://github.com/redouan-rhazouani) **Created:** 2/26/2025 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix_perf_base62` --- ### 📝 Commits (7) - [`dc8eb40`](https://github.com/netbirdio/netbird/commit/dc8eb409653cd2a1e531058bf69d26709e776dd6) Enhance Base62 unit tests by explicitly verifying encoding and decoding accuracy - [`757e178`](https://github.com/netbirdio/netbird/commit/757e1783442497c21813c282787dd29a6512c49a) Reduce heap allocations and improves performance of base62.Encode by avoiding dynamic memory usage for small, fixed-size data - [`88189c5`](https://github.com/netbirdio/netbird/commit/88189c580e382bdad244259a4a005a16a7402ff6) [Base62.Decode] use iterative multiplication instead of math.Pow() to avoid less precise floating point operations - [`8fa6021`](https://github.com/netbirdio/netbird/commit/8fa60211f861756861a57ba1c1e9dad73edc3819) [Base62.Decode] return error if input string is empty - [`1bad771`](https://github.com/netbirdio/netbird/commit/1bad7712375b07b4ad815bcdb351599a0d551efc) Add overflow check when calculating the decoded value to prevent silent overflow of uint32 - [`2981ce5`](https://github.com/netbirdio/netbird/commit/2981ce514386b9d0183c8321682eae558ef18d9f) [Base62.Decode] add unit test for input strings containing invalid character - [`0c6e9f1`](https://github.com/netbirdio/netbird/commit/0c6e9f1075d01da51ab50ed26a0ce3aece497d7c) [Base62.Decode] optimize decoding by replacing strings.IndexRune with a fixed-size array for O(1) lookups, improving performance and reducing memory overhead ### 📊 Changes **2 files changed** (+100 additions, -45 deletions) <details> <summary>View changed files</summary> 📝 `base62/base62.go` (+50 -31) 📝 `base62/base62_test.go` (+50 -14) </details> ### 📄 Description ## 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 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
saavagebueno added the pull-request label 2026-08-05 06:06:02 -04:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: DYNR/netbird#22499