I have a project where I publish and subscribe some data packages. Mostly those data packages are just one package, but sometimes (1 in 100) there could be more packages at one time (a lot more, like 100.000 at the same time) I have a class PacketTransport
which only has a IDataPacket
property in it. I do this so I can serialize and deserialize it with JSON.net and the JsonSerializerSettings
with TypeNameHandling = TypeNameHandling.Auto
so the deserializer will know which kind of type it is.
I think about making IDataPacket
to an IEnumerable<IDataPacket>
so I will always enumerate eventhough there is just one data packet inside because when sometimes 100.000 Data Packages at a time are coming, my MQTT is overloaded with 100.000 PacketTransport
packages and for the next minutes nothing else can pass. But as those 100.000 packets are mostly very small, I would like to pack it in one PacketTransport
message.
So my question is: Is it bad to use an IEnumerable where most of the time only one item is inside it? Is the overhead that much or can I ignore it?
public class PacketTransport
{
//Serialize with:
//JsonConvert.SerializeObject(packetTransport, new() { TypeNameHandling = TypeNameHandling.Auto });
public IEnumerable<IDataPacket> DataPackets { get; }
public PacketTransport(IDataPacket dataPacket)
{
if (dataPacket is null) throw new ArgumentNullException(nameof(dataPacket));
DataPackets = new[] { dataPacket }; //Is this the most efficient way?
}
public PacketTransport(IEnumerable<IDataPacket> dataPackets)
{
if (dataPackets is null) throw new ArgumentNullException(nameof(dataPackets));
DataPackets = dataPackets.ToArray(); //Copy the original dataPackets
}
}
public interface IDataPacket
{
int Id { get; }
}
p.s.: I know, that my main bottleneck is probably the JSON serialization, I will go with protobuf in the future