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 perf of base64 encoding check on a consumer hot path #2371

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Havret
Copy link

@Havret Havret commented Nov 29, 2024

This PR improves the performance of the Base64 encoding check, which is performed for every message processed by the consumer. This change complements the work done in #2370.

Below are the results of benchmarking the new implementation (Base64.IsValid) against the existing Convert.FromBase64String approach:

| Method           | Mean          | Error        | StdDev       |
|----------------- |--------------:|-------------:|-------------:|
| IsBase64         |      24.52 ns |     0.074 ns |     0.066 ns |
| FromBase64String | 138,883.49 ns | 2,535.710 ns | 2,247.841 ns |

The code used to produce the benchmark results::

class Program
{
    static void Main(string[] args)
    {
        BenchmarkRunner.Run<IsBase64StringBenchmark>();
    }
}

public class IsBase64StringBenchmark
{
    private readonly string _invalidBase64 = string.Join("", Enumerable.Repeat("This is not base64!", 10000));

    [Benchmark]
    public bool IsBase64()
    {
        return Base64.IsValid(_invalidBase64);
    }

    [Benchmark]
    public bool FromBase64String()
    {
        try
        {
            _ = Convert.FromBase64String(_invalidBase64);
            return true;
        }
        catch (FormatException)
        {
            return false;
        }
    }
}

@Havret Havret requested review from a team as code owners November 29, 2024 09:11
@confluent-cla-assistant
Copy link

🎉 All Contributor License Agreements have been signed. Ready to merge.
✅ Havret
Please push an empty commit if you would like to re-run the checks to verify CLA status for all contributors.

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