Skip to content
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

Improve Base62 encoding/decoding performance and robustness #3391

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

redouan-rhazouani
Copy link

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

  • 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

…avoiding dynamic memory usage for small, fixed-size data
… avoid less precise floating point operations
…nt overflow of uint32

The current implementation could produce incorrect results for very long input strings
… a fixed-size array for O(1) lookups, improving performance and reducing memory overhead
@CLAassistant
Copy link

CLAassistant commented Feb 26, 2025

CLA assistant check
All committers have signed the CLA.

@mlsmaycon mlsmaycon requested a review from Copilot March 22, 2025 22:29

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 improves Base62 encoding and decoding performance while enhancing robustness and error handling. Key changes include:

  • Optimized encoding by using a fixed-size buffer and iterative multiplication.
  • Improved decoding with a fixed-size lookup array, added overflow and invalid character checks.
  • Expanded unit tests to cover edge cases including empty input, overflow, and invalid characters.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
base62/base62.go Refactored encoding/decoding logic for performance and robustness.
base62/base62_test.go Added comprehensive tests for encoding/decoding, error handling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants