Browse Source

[Exif] Eliminate invalidTag list allocation when there are no invalid tags

pull/747/head
Jason Nelson 8 years ago
parent
commit
de0956295a
  1. 12
      src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs
  2. 20
      src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs

12
src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs

@ -50,7 +50,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
{
this.Parts = ExifParts.All;
this.data = data;
this.InvalidTags = new List<ExifTag>();
this.InvalidTags = Array.Empty<ExifTag>();
}
/// <summary>
@ -289,7 +289,15 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
this.values = reader.ReadValues();
this.InvalidTags = new List<ExifTag>(reader.InvalidTags);
if (reader.InvalidTags.Count > 0)
{
this.InvalidTags = new List<ExifTag>(reader.InvalidTags);
}
else
{
this.InvalidTags = Array.Empty<ExifTag>();
}
this.thumbnailOffset = (int)reader.ThumbnailOffset;
this.thumbnailLength = (int)reader.ThumbnailLength;
}

20
src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs

@ -7,7 +7,6 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.Primitives;
@ -19,7 +18,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
/// </summary>
internal sealed class ExifReader
{
private readonly List<ExifTag> invalidTags = new List<ExifTag>();
private List<ExifTag> invalidTags;
private readonly byte[] exifData;
private int position;
private Endianness endianness = Endianness.BigEndian;
@ -38,7 +37,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
/// <summary>
/// Gets the invalid tags.
/// </summary>
public IReadOnlyList<ExifTag> InvalidTags => this.invalidTags;
public IReadOnlyList<ExifTag> InvalidTags => this.invalidTags ?? (IReadOnlyList<ExifTag>)Array.Empty<ExifTag>();
/// <summary>
/// Gets the thumbnail length in the byte stream
@ -338,7 +337,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
// Ensure that the new index does not overrun the data
if (newIndex > int.MaxValue)
{
this.invalidTags.Add(tag);
this.AddInvalidTag(tag);
exifValue = default;
@ -349,7 +348,8 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
if (this.RemainingLength < size)
{
this.invalidTags.Add(tag);
this.AddInvalidTag(tag);
this.position = oldIndex;
exifValue = default;
@ -372,6 +372,16 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
return true;
}
private void AddInvalidTag(ExifTag tag)
{
if (this.invalidTags == null)
{
this.invalidTags = new List<ExifTag>();
}
this.invalidTags.Add(tag);
}
private TEnum ToEnum<TEnum>(int value, TEnum defaultValue)
where TEnum : struct
{

Loading…
Cancel
Save