Skip to content

Reduced some allocations in QRCodeGenerator (NETCORE_APP only) #595

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 9 commits into
base: master
Choose a base branch
from
105 changes: 89 additions & 16 deletions QRCoder/QRCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#endif
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -120,8 +121,14 @@ public static QRCodeData GenerateQrCode(string plainText, ECCLevel eccLevel, boo
//Version was passed as fixed version via parameter. Thus let's check if chosen version is valid.
if (minVersion > version)
{
var maxSizeByte = CapacityTables.GetVersionInfo(version).Details.First(x => x.ErrorCorrectionLevel == eccLevel).CapacityDict[encoding];
throw new QRCoder.Exceptions.DataTooLongException(eccLevel.ToString(), encoding.ToString(), version, maxSizeByte);
// Use a throw-helper to avoid allocating a closure
Throw(eccLevel, encoding, version);

static void Throw(ECCLevel eccLevel, EncodingMode encoding, int version)
{
var maxSizeByte = CapacityTables.GetVersionInfo(version).Details.First(x => x.ErrorCorrectionLevel == eccLevel).CapacityDict[encoding];
throw new Exceptions.DataTooLongException(eccLevel.ToString(), encoding.ToString(), version, maxSizeByte);
}
}
}

Expand Down Expand Up @@ -217,8 +224,9 @@ private static QRCodeData GenerateQrCode(BitArray bitArray, ECCLevel eccLevel, i
// Place interleaved data on module matrix
var qrData = PlaceModules();

return qrData;
CodewordBlock.ReturnList(codeWordWithECC);

return qrData;

// fills the bit array with a repeating pattern to reach the required length
void PadData()
Expand Down Expand Up @@ -254,7 +262,7 @@ List<CodewordBlock> CalculateECCBlocks()
using (var generatorPolynom = CalculateGeneratorPolynom(eccInfo.ECCPerBlock))
{
//Calculate error correction words
codewordBlocks = new List<CodewordBlock>(eccInfo.BlocksInGroup1 + eccInfo.BlocksInGroup2);
codewordBlocks = CodewordBlock.GetList(eccInfo.BlocksInGroup1 + eccInfo.BlocksInGroup2);
AddCodeWordBlocks(1, eccInfo.BlocksInGroup1, eccInfo.CodewordsInGroup1, 0, bitArray.Length, generatorPolynom);
int offset = eccInfo.BlocksInGroup1 * eccInfo.CodewordsInGroup1 * 8;
AddCodeWordBlocks(2, eccInfo.BlocksInGroup2, eccInfo.CodewordsInGroup2, offset, bitArray.Length - offset, generatorPolynom);
Expand Down Expand Up @@ -286,7 +294,7 @@ int CalculateInterleavedLength()
for (var i = 0; i < eccInfo.ECCPerBlock; i++)
{
foreach (var codeBlock in codeWordWithECC)
if (codeBlock.ECCWords.Length > i)
if (codeBlock.ECCWords.Count > i)
length += 8;
}
length += CapacityTables.GetRemainderBits(version);
Expand All @@ -309,8 +317,8 @@ BitArray InterleaveData()
for (var i = 0; i < eccInfo.ECCPerBlock; i++)
{
foreach (var codeBlock in codeWordWithECC)
if (codeBlock.ECCWords.Length > i)
pos = DecToBin(codeBlock.ECCWords[i], 8, data, pos);
if (codeBlock.ECCWords.Count > i)
pos = DecToBin(codeBlock.ECCWords.Array![i], 8, data, pos);
}

return data;
Expand Down Expand Up @@ -484,7 +492,7 @@ private static void GetVersionString(BitArray vStr, int version)
/// This method applies polynomial division, using the message polynomial and a generator polynomial,
/// to compute the remainder which forms the ECC codewords.
/// </summary>
private static byte[] CalculateECCWords(BitArray bitArray, int offset, int count, ECCInfo eccInfo, Polynom generatorPolynomBase)
private static ArraySegment<byte> CalculateECCWords(BitArray bitArray, int offset, int count, ECCInfo eccInfo, Polynom generatorPolynomBase)
{
var eccWords = eccInfo.ECCPerBlock;
// Calculate the message polynomial from the bit array data.
Expand Down Expand Up @@ -532,9 +540,16 @@ private static byte[] CalculateECCWords(BitArray bitArray, int offset, int count
generatorPolynom.Dispose();

// Convert the resulting polynomial into a byte array representing the ECC codewords.
var ret = new byte[leadTermSource.Count];
#if NETCOREAPP
var array = ArrayPool<byte>.Shared.Rent(leadTermSource.Count);
var ret = new ArraySegment<byte>(array, 0, leadTermSource.Count);
#else
var ret = new ArraySegment<byte>(new byte[leadTermSource.Count]);
var array = ret.Array!;
#endif

for (var i = 0; i < leadTermSource.Count; i++)
ret[i] = (byte)leadTermSource[i].Coefficient;
array[i] = (byte)leadTermSource[i].Coefficient;

// Free memory used by the message polynomial.
leadTermSource.Dispose();
Expand Down Expand Up @@ -1017,8 +1032,15 @@ private static Polynom MultiplyAlphaPolynoms(Polynom polynomBase, Polynom polyno
}

// Identify and merge terms with the same exponent.
#if NETCOREAPP
var toGlue = GetNotUniqueExponents(resultPolynom, resultPolynom.Count <= 128 ? stackalloc int[128].Slice(0, resultPolynom.Count) : new int[resultPolynom.Count]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I don't know too little about the QR-specs, but the resultPolynom.Count <= 128 could be avoided if by spec the count can't be as high. Or we can change the threshold to a higher value.

If the count can't be as high, then the fallback to the array allocation could also be removed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When running the tests, it doesn't go over 64. Seems fine to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My tests showed the same (what a wonder 😉).
But in that regard I'm a bit paranoid...in the sense wenn the fallback is removed what will happen when a bigger buffer is needed (maybe now by some special inputs or anytime in the future when new additions may be done)? This would result in an exception. W/ the fallback there's a safety net.

Except it can be proven via the QR-spec that the count never will exceed a certain threshold (but does that hold in the future too?)

If the non-stackalloc path would be very frequent, then renting from the array-pool would be an option, but here it's a assumed to be rare / never taken path.

Would you still remove the fallback or let's just leave it as is?

Copy link
Contributor

@Shane32 Shane32 Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I’d definitely leave the fallback. There’s just no reason to remove it. The JIT will realize it’s not a common pathway and optimize it accordingly. But as you say, we neither of us know enough about how these polynomials work to know if it will always be < 128.

var gluedPolynoms = toGlue.Length <= 128
? stackalloc PolynomItem[128].Slice(0, toGlue.Length)
: new PolynomItem[toGlue.Length];
#else
var toGlue = GetNotUniqueExponents(resultPolynom);
var gluedPolynoms = new PolynomItem[toGlue.Length];
#endif
var gluedPolynomsIndex = 0;
foreach (var exponent in toGlue)
{
Expand All @@ -1036,7 +1058,11 @@ private static Polynom MultiplyAlphaPolynoms(Polynom polynomBase, Polynom polyno

// Remove duplicated exponents and add the corrected ones back.
for (int i = resultPolynom.Count - 1; i >= 0; i--)
#if NETCOREAPP
if (toGlue.Contains(resultPolynom[i].Exponent))
#else
if (Array.IndexOf(toGlue, resultPolynom[i].Exponent) >= 0)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before Linq was used w/ it's generic Contains method.

  • for AOT Linq requires way more code
  • it's an interface dispatch that isn't needed

Note: for .NET (Core) the span-based Contains is used.

#endif
resultPolynom.RemoveAt(i);
foreach (var polynom in gluedPolynoms)
resultPolynom.Add(polynom);
Expand All @@ -1046,20 +1072,66 @@ private static Polynom MultiplyAlphaPolynoms(Polynom polynomBase, Polynom polyno
return resultPolynom;

// Auxiliary function to identify exponents that appear more than once in the polynomial.
int[] GetNotUniqueExponents(Polynom list)
#if NETCOREAPP
static ReadOnlySpan<int> GetNotUniqueExponents(Polynom list, Span<int> buffer)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation doesn't need a Dictionary<,> to determine the non unique exponents.

It works as follows:

  1. a scratch buffer of the same size as the list is passed in
  2. exponents are written / copied to that scratch buffer
  3. scratch buffer is sorted, thus the exponents are in order
  4. for each item in the scratch buffer (= ordered exponents) it's compared w/ the previous one
  • if equal, then increment a counter
  • else check if the counter is $&gt;0$ and if so write the exponent to the result

For writing the result the same scratch buffer is used, as by definition the index to write the result is <= the iteration index, so no overlap, etc. can occur.

That way we avoid the need for a second scratch buffer.


Should someting like this be added as comment or is it cleare enough how it works?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function isn't long; so long as the method comment is descriptive enough, I think it's fine.

{
var dic = new Dictionary<int, bool>(list.Count);
// It works as follows:
// 1. a scratch buffer of the same size as the list is passed in
// 2. exponents are written / copied to that scratch buffer
// 3. scratch buffer is sorted, thus the exponents are in order
// 4. for each item in the scratch buffer (= ordered exponents) it's compared w/ the previous one
// * if equal, then increment a counter
// * else check if the counter is $>0$ and if so write the exponent to the result
//
// For writing the result the same scratch buffer is used, as by definition the index to write the result
// is `<=` the iteration index, so no overlap, etc. can occur.

Debug.Assert(list.Count == buffer.Length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you plan to leave the Debug.Assert code in here? Just wondering; it will get removed from release builds anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to have some Debug.Asserts

  • they check some invariants in debug builds and while running tests
  • are a bit of self-documenting the intention (so why write a comment, when the assert can also be done?)

Especially here the buffer is passed in as argument and must have the correct size.
If the size doesn't match, then the tests (under debug) will fail and one knows why. Otherwise it may be hard to track down the bug.

So I'd leave them in the code.

As you said: for !DEBUG these asserts won't have any effect.


int idx = 0;
foreach (var row in list)
{
#if NETCOREAPP
if (dic.TryAdd(row.Exponent, false))
dic[row.Exponent] = true;
buffer[idx++] = row.Exponent;
}

buffer.Sort();

idx = 0;
int expCount = 0;
int last = buffer[0];

for (int i = 1; i < buffer.Length; ++i)
{
if (buffer[i] == last)
{
expCount++;
}
else
{
if (expCount > 0)
{
Debug.Assert(idx <= i - 1);

buffer[idx++] = last;
expCount = 0;
}
}

last = buffer[i];
}

return buffer.Slice(0, idx);
}
#else
static int[] GetNotUniqueExponents(Polynom list)
{
var dic = new Dictionary<int, bool>(list.Count);
foreach (var row in list)
{
if (!dic.ContainsKey(row.Exponent))
dic.Add(row.Exponent, false);
else
dic[row.Exponent] = true;
#endif
}

// Collect all exponents that appeared more than once.
Expand All @@ -1080,6 +1152,7 @@ int[] GetNotUniqueExponents(Polynom list)

return result;
}
#endif
}

/// <inheritdoc cref="IDisposable.Dispose"/>
Expand Down
31 changes: 25 additions & 6 deletions QRCoder/QRCodeGenerator/CapacityTables.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -36,7 +37,17 @@ private static class CapacityTables
/// block group details, and other parameters required for encoding error correction data.
/// </returns>
public static ECCInfo GetEccInfo(int version, ECCLevel eccLevel)
=> _capacityECCTable.Single(x => x.Version == version && x.ErrorCorrectionLevel == eccLevel);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The downside of Linq is that no (generic) state can be passed in, thus a closure is needed, and this one showed up in a memory-profile.

{
foreach (var item in _capacityECCTable)
{
if (item.Version == version && item.ErrorCorrectionLevel == eccLevel)
{
return item;
}
}

throw new InvalidOperationException("No item found"); // same exception type as Linq would throw
}

/// <summary>
/// Retrieves the capacity information for a specific QR code version.
Expand Down Expand Up @@ -92,11 +103,19 @@ public static int CalculateMinimumVersion(int length, EncodingMode encMode, ECCL
}

// if no version was found, throw an exception
var maxSizeByte = _capacityTable.Where(
x => x.Details.Any(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This closure also showed up, and unfortunately the closure (display-class) gets created at method entry, and not when actually needed, so we have to work around this.

y => (y.ErrorCorrectionLevel == eccLevel))
).Max(x => x.Details.Single(y => y.ErrorCorrectionLevel == eccLevel).CapacityDict[encMode]);
throw new QRCoder.Exceptions.DataTooLongException(eccLevel.ToString(), encMode.ToString(), maxSizeByte);
// In order to get the maxSizeByte we use a throw-helper method to avoid the allocation of a closure
Throw(encMode, eccLevel);
throw null!; // this is needed to make the compiler happy

static void Throw(EncodingMode encMode, ECCLevel eccLevel)
{
var maxSizeByte = _capacityTable.Where(
x => x.Details.Any(
y => (y.ErrorCorrectionLevel == eccLevel))
).Max(x => x.Details.Single(y => y.ErrorCorrectionLevel == eccLevel).CapacityDict[encMode]);

throw new Exceptions.DataTooLongException(eccLevel.ToString(), encMode.ToString(), maxSizeByte);
}
}

/// <summary>
Expand Down
31 changes: 28 additions & 3 deletions QRCoder/QRCodeGenerator/CodewordBlock.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
using System;
using System.Collections.Generic;
using System.Threading;

#if NETCOREAPP
using System.Buffers;
#endif

namespace QRCoder;

public partial class QRCodeGenerator
Expand All @@ -6,15 +14,15 @@ public partial class QRCodeGenerator
/// Represents a block of codewords in a QR code. QR codes are divided into several blocks for error correction purposes.
/// Each block contains a series of data codewords followed by error correction codewords.
/// </summary>
private struct CodewordBlock
private readonly struct CodewordBlock
{
/// <summary>
/// Initializes a new instance of the CodewordBlock struct with specified arrays of code words and error correction (ECC) words.
/// </summary>
/// <param name="codeWordsOffset">The offset of the data codewords within the main BitArray. Data codewords carry the actual information.</param>
/// <param name="codeWordsLength">The length in bits of the data codewords within the main BitArray.</param>
/// <param name="eccWords">The array of error correction codewords for this block. These codewords help recover the data if the QR code is damaged.</param>
public CodewordBlock(int codeWordsOffset, int codeWordsLength, byte[] eccWords)
public CodewordBlock(int codeWordsOffset, int codeWordsLength, ArraySegment<byte> eccWords)
{
CodeWordsOffset = codeWordsOffset;
CodeWordsLength = codeWordsLength;
Expand All @@ -34,6 +42,23 @@ public CodewordBlock(int codeWordsOffset, int codeWordsLength, byte[] eccWords)
/// <summary>
/// Gets the error correction codewords associated with this block.
/// </summary>
public byte[] ECCWords { get; }
public ArraySegment<byte> ECCWords { get; }

private static List<CodewordBlock>? _codewordBlocks;

public static List<CodewordBlock> GetList(int capacity)
=> Interlocked.Exchange(ref _codewordBlocks, null) ?? new List<CodewordBlock>(capacity);

public static void ReturnList(List<CodewordBlock> list)
{
#if NETCOREAPP
foreach (var item in list)
{
ArrayPool<byte>.Shared.Return(item.ECCWords.Array!);
}
#endif
list.Clear();
Interlocked.CompareExchange(ref _codewordBlocks, list, null);
}
}
}
6 changes: 5 additions & 1 deletion QRCoder/QRCodeGenerator/GaloisField.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;

namespace QRCoder;

Expand Down Expand Up @@ -43,6 +44,9 @@ public static int GetAlphaExpFromIntVal(int intVal)
/// This is particularly necessary when performing multiplications in the field which can result in exponents exceeding the field's maximum.
/// </summary>
public static int ShrinkAlphaExp(int alphaExp)
=> (alphaExp % 256) + (alphaExp / 256);
{
Debug.Assert(alphaExp >= 0);
return (int)((uint)alphaExp % 256 + (uint)alphaExp / 256);
}
}
}
Loading