Skip to content

Performance improvements for Encoding & Decoding #41

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

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

Conversation

janbiehl
Copy link

Hey, I benchmarked SqidIdEncoder and saw potential for performance improvements. So I went ahead and implemented some performance improvements. While some of them only apply to .net 8 and above, I hope they fit in with the vision of this project.

  • Unit tests still run successful

Benchmark source code

Source

using BenchmarkDotNet.Attributes;

namespace Sqids.Benchmarks;

[MemoryDiagnoser]
public class SqidIdDecoderBenchmark
{
	private readonly SqidsEncoder<long> _sqidsEncoder = new ();

	[Params("Uk", "DzPWXTJADcE", "bMZn4Y5Fq8QTCJoLjxPvGfB9Dh6mlz1Sgcu0KpkMyOEiIdrsHRW2VZtweX3aA7UNbhFm8ZG04y52lzNU6di")]
	public string EncodedId { get; set; } = string.Empty;

	[Benchmark]
	public IReadOnlyList<long> Decode() => _sqidsEncoder.Decode(EncodedId);
}
using BenchmarkDotNet.Attributes;

namespace Sqids.Benchmarks;

[MemoryDiagnoser]
public class SqidIdEncoderBenchmark
{
	private readonly SqidsEncoder<long> _sqidsEncoder = new ();

	[Params(1, 100, 1_000, 1_000_000, 1_000_000_000)]
	public int Id { get; set; }

	[Benchmark]
	public string Encode() => _sqidsEncoder.Encode(Id);
}

Benchmark before

Type Method Id EncodedId Mean Error StdDev Gen0 Allocated
SqidIdEncoderBenchmark Encode 1 ? 339.68 ns 0.236 ns 0.197 ns 0.0420 264 B
SqidIdEncoderBenchmark Encode 100 ? 603.40 ns 1.545 ns 1.445 ns 0.0782 496 B
SqidIdEncoderBenchmark Encode 1000 ? 604.90 ns 1.283 ns 1.200 ns 0.0782 496 B
SqidIdEncoderBenchmark Encode 1000000 ? 7,046.74 ns 8.617 ns 8.060 ns 0.9918 6224 B
SqidIdEncoderBenchmark Encode 1000000000 ? 14,116.36 ns 20.469 ns 18.145 ns 1.8616 11680 B
SqidIdDecoderBenchmark Decode ? bMZn4(...)NU6di [83] 1,121.78 ns 2.426 ns 2.026 ns 0.0134 88 B
SqidIdDecoderBenchmark Decode ? DzPWXTJADcE 567.55 ns 0.405 ns 0.379 ns 0.0134 88 B
SqidIdDecoderBenchmark Decode ? Uk 68.80 ns 0.062 ns 0.058 ns 0.0139 88 B

Benchmark after

Type Method Id EncodedId Mean Error StdDev Median Gen0 Allocated
SqidIdEncoderBenchmark Encode 1 ? 340.35 ns 1.327 ns 1.241 ns 340.29 ns 0.0048 32 B
SqidIdEncoderBenchmark Encode 100 ? 496.70 ns 11.138 ns 31.596 ns 482.06 ns 0.0048 32 B
SqidIdEncoderBenchmark Encode 1000 ? 541.43 ns 10.565 ns 24.063 ns 529.16 ns 0.0048 32 B
SqidIdEncoderBenchmark Encode 1000000 ? 3,085.81 ns 59.130 ns 149.430 ns 3,018.31 ns 0.0038 32 B
SqidIdEncoderBenchmark Encode 1000000000 ? 5,673.26 ns 53.813 ns 50.337 ns 5,674.56 ns - 40 B
SqidIdDecoderBenchmark Decode ? bMZn4(...)NU6di [83] 648.11 ns 7.812 ns 7.307 ns 645.51 ns 0.0134 88 B
SqidIdDecoderBenchmark Decode ? DzPWXTJADcE 506.34 ns 3.831 ns 3.584 ns 506.60 ns 0.0134 88 B
SqidIdDecoderBenchmark Decode ? Uk 58.11 ns 1.220 ns 2.875 ns 56.97 ns 0.0140 88 B

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.

1 participant