Skip to content

Commit

Permalink
Optimize cross reference object offset validation by avoiding nested …
Browse files Browse the repository at this point in the history
…loops
  • Loading branch information
madelson committed Nov 12, 2024
1 parent 132ada7 commit 67a6401
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
16 changes: 8 additions & 8 deletions src/UglyToad.PdfPig.Core/IndirectReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// <summary>
/// Used to uniquely identify and refer to objects in the PDF file.
/// </summary>
public readonly struct IndirectReference
public readonly struct IndirectReference : IEquatable<IndirectReference>
{
/// <summary>
/// A positive integer object number.
Expand All @@ -31,15 +31,15 @@ public IndirectReference(long objectNumber, int generation)
}

/// <inheritdoc />
public override bool Equals(object obj)
public bool Equals(IndirectReference other)
{
if (obj is IndirectReference reference)
{
return reference.ObjectNumber == ObjectNumber
&& reference.Generation == Generation;
}
return other.ObjectNumber == ObjectNumber && other.Generation == Generation;
}

return false;
/// <inheritdoc />
public override bool Equals(object obj)
{
return obj is IndirectReference other && Equals(other);
}

/// <inheritdoc />
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/CrossReference/CrossReferenceTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal CrossReferenceTable(CrossReferenceType type, IReadOnlyDictionary<Indire
Trailer = trailer ?? throw new ArgumentNullException(nameof(trailer));
CrossReferenceOffsets = crossReferenceOffsets ?? throw new ArgumentNullException(nameof(crossReferenceOffsets));

var result = new Dictionary<IndirectReference, long>();
var result = new Dictionary<IndirectReference, long>(capacity: objectOffsets.Count);
foreach (var objectOffset in objectOffsets)
{
result[objectOffset.Key] = objectOffset.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public static bool ValidateCrossReferenceOffsets(IInputBytes bytes, CrossReferen
return true;
}

var builderOffsets = new Dictionary<IndirectReference, long>();

var bruteForceOffsets = BruteForceSearcher.GetObjectLocations(bytes);
if (bruteForceOffsets.Count > 0)
{
var builderOffsets = new Dictionary<IndirectReference, long>();

// find all object streams
foreach (var entry in crossReferenceTable.ObjectOffsets)
{
Expand All @@ -39,11 +39,11 @@ public static bool ValidateCrossReferenceOffsets(IInputBytes bytes, CrossReferen
// TODO: more validation of streams.
builderOffsets[entry.Key] = entry.Value;
}
}

foreach (var item in bruteForceOffsets)
{
builderOffsets[item.Key] = item.Value;
}
foreach (var item in bruteForceOffsets)
{
builderOffsets[item.Key] = item.Value;
}

actualOffsets = builderOffsets;
Expand Down

0 comments on commit 67a6401

Please sign in to comment.