Skip to content

Commit 51e4204

Browse files
authored
Adds an optimization in case when a single number is passed to encode (#49)
1 parent 8c398de commit 51e4204

4 files changed

Lines changed: 60 additions & 5 deletions

File tree

src/Hashids.net/Hashids.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ private static void InitCharArrays(string alphabet, string seps, ReadOnlySpan<ch
142142
}
143143
}
144144

145+
#if NETCOREAPP3_1_OR_GREATER
146+
/// <summary>
147+
/// Encodes the provided number into a hashed string
148+
/// </summary>
149+
/// <param name="number">the number</param>
150+
/// <returns>the hashed string</returns>
151+
public string Encode(int number) => EncodeLong(number);
152+
#endif
153+
145154
/// <summary>
146155
/// Encodes the provided numbers into a hash string.
147156
/// </summary>
@@ -156,6 +165,20 @@ private static void InitCharArrays(string alphabet, string seps, ReadOnlySpan<ch
156165
/// <returns>Encoded hash string.</returns>
157166
public virtual string Encode(IEnumerable<int> numbers) => Encode(numbers.ToArray());
158167

168+
#if NETCOREAPP3_1_OR_GREATER
169+
/// <summary>
170+
/// Encodes the provided number into a hashed string
171+
/// </summary>
172+
/// <param name="number">the number</param>
173+
/// <returns>the hashed string</returns>
174+
public string EncodeLong(long number)
175+
{
176+
ReadOnlySpan<long> span = stackalloc[] { number };
177+
178+
return GenerateHashFrom(span);
179+
}
180+
#endif
181+
159182
/// <summary>
160183
/// Encodes the provided numbers into a hash string.
161184
/// </summary>
@@ -268,7 +291,7 @@ private string GenerateHashFrom(ReadOnlySpan<long> numbers)
268291
{
269292
Array.Copy(alphabet, 0, shuffleBuffer, startIndex, length);
270293
}
271-
294+
272295
ConsistentShuffle(alphabet, _alphabet.Length, shuffleBuffer, _alphabet.Length);
273296
var hashLength = BuildReversedHash(number, alphabet, hashBuffer);
274297

@@ -375,7 +398,7 @@ private long[] GetNumbersFrom(string hash)
375398

376399
if (lottery == '\0') /* default(char) == '\0' */
377400
return Array.Empty<long>();
378-
401+
379402
hashBreakdown = hashBreakdown.Substring(1);
380403

381404
hashArray = hashBreakdown.Split(_seps, StringSplitOptions.RemoveEmptyEntries);
@@ -447,4 +470,4 @@ private static void ConsistentShuffle(char[] alphabet, int alphabetLength, ReadO
447470
}
448471
}
449472
}
450-
}
473+
}

src/Hashids.net/IHashids.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ public interface IHashids
2929
/// <returns>the hex string</returns>
3030
string DecodeHex(string hash);
3131

32+
#if NETCOREAPP3_1_OR_GREATER
33+
/// <summary>
34+
/// Encodes the provided number into a hashed string
35+
/// </summary>
36+
/// <param name="number">the number</param>
37+
/// <returns>the hashed string</returns>
38+
string Encode(int number);
39+
#endif
40+
3241
/// <summary>
3342
/// Encodes the provided numbers into a hashed string
3443
/// </summary>
@@ -43,6 +52,15 @@ public interface IHashids
4352
/// <returns>the hashed string</returns>
4453
string Encode(IEnumerable<int> numbers);
4554

55+
#if NETCOREAPP3_1_OR_GREATER
56+
/// <summary>
57+
/// Encodes the provided number into a hashed string
58+
/// </summary>
59+
/// <param name="number">the number</param>
60+
/// <returns>the hashed string</returns>
61+
string EncodeLong(long number);
62+
#endif
63+
4664
/// <summary>
4765
/// Encodes the provided numbers into a hashed string
4866
/// </summary>

test/Hashids.net.benchmark/Program.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ public void RoundtripHex()
4343
var encodedValue = _hashids.EncodeHex(_hex);
4444
var decodedValue = _hashids.DecodeHex(encodedValue);
4545
}
46+
47+
[Benchmark]
48+
public void SingleNumber()
49+
{
50+
var encoded = _hashids.Encode(5);
51+
var encodeLong = _hashids.EncodeLong(5);
52+
}
53+
54+
[Benchmark]
55+
public void SingleNumberAsParams()
56+
{
57+
var encoded = _hashids.Encode(new []{ 1 });
58+
var encodedLong = _hashids.EncodeLong(new []{ (long)1 });
59+
}
4660
}
4761
}
48-
}
62+
}

test/Hashids.net.test/GeneralTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,4 @@ public void PublicMethodsCanBeMocked()
338338
mock.Object.Encode(new[] { 1 }).Should().Be("It works");
339339
}
340340
}
341-
}
341+
}

0 commit comments

Comments
 (0)