Skip to main content
deleted 1 character in body
Source Link
aepot
  • 2.1k
  • 9
  • 20

First of all, use array instead of list. Every time you know the data size, use array instead of list.

Stop instantiating lot of new arrays, it's very slow. Use offset instead.

This one arr.Skip(12).Take(1).First(); looks really killing. Why not arr[13]?

Also Int16 is short not int. Use matching types.

Let's assume that _recieveData.packet is byte[].

private void UdpFrameHandle()
{
    int unitCount = (_recieveData.packet.Length - 2) / 15;
    
    for (int i = 0; i < unitCount; i++)
    {
        var unitInfo = ByteToUnitNetwork(_recieveData.packet, i * 15 + 2);
        FrameManager.SendDataToView(unitInfo.ViewId, unitInfo);
    }
}

protected UnitNetworkDto ByteToUnitNetwork(byte[] arr, int offset)
{
    short viewId = BitConverter.ToInt16(arr, offset);
    float x = BitConverter.ToSingle(arr, offset + 2);
    float z = BitConverter.ToSingle(arr, offset + 6);
    short health = BitConverter.ToInt16(arr, offset + 10);
    byte teamType = arr[13];
    short targetViewId = BitConverter.ToInt16(arr, offset + 14);

    return new UnitNetworkDto
    {
        ViewId = viewId,
        X = x,
        Z = z,
        Health = health,
        TeamType = teamType,
        TargetViewId = targetViewId
    };
}

Also BitConverter isn't safe to use because its numbers firmatformat depends on CPU architecture. Check BitConverter.IsLittleEndian before use. If available, use BinaryPrimitives instead.

Also always keep in mind when you're thinking of performance: "Linq is slow".

First of all, use array instead of list. Every time you know the data size, use array instead of list.

Stop instantiating lot of new arrays, it's very slow. Use offset instead.

This one arr.Skip(12).Take(1).First(); looks really killing. Why not arr[13]?

Also Int16 is short not int. Use matching types.

Let's assume that _recieveData.packet is byte[].

private void UdpFrameHandle()
{
    int unitCount = (_recieveData.packet.Length - 2) / 15;
    
    for (int i = 0; i < unitCount; i++)
    {
        var unitInfo = ByteToUnitNetwork(_recieveData.packet, i * 15 + 2);
        FrameManager.SendDataToView(unitInfo.ViewId, unitInfo);
    }
}

protected UnitNetworkDto ByteToUnitNetwork(byte[] arr, int offset)
{
    short viewId = BitConverter.ToInt16(arr, offset);
    float x = BitConverter.ToSingle(arr, offset + 2);
    float z = BitConverter.ToSingle(arr, offset + 6);
    short health = BitConverter.ToInt16(arr, offset + 10);
    byte teamType = arr[13];
    short targetViewId = BitConverter.ToInt16(arr, offset + 14);

    return new UnitNetworkDto
    {
        ViewId = viewId,
        X = x,
        Z = z,
        Health = health,
        TeamType = teamType,
        TargetViewId = targetViewId
    };
}

Also BitConverter isn't safe to use because its numbers firmat depends on CPU architecture. Check BitConverter.IsLittleEndian before use. If available, use BinaryPrimitives instead.

Also always keep in mind when you're thinking of performance: "Linq is slow".

First of all, use array instead of list. Every time you know the data size, use array instead of list.

Stop instantiating lot of new arrays, it's very slow. Use offset instead.

This one arr.Skip(12).Take(1).First() looks really killing. Why not arr[13]?

Also Int16 is short not int. Use matching types.

Let's assume that _recieveData.packet is byte[].

private void UdpFrameHandle()
{
    int unitCount = (_recieveData.packet.Length - 2) / 15;
    
    for (int i = 0; i < unitCount; i++)
    {
        var unitInfo = ByteToUnitNetwork(_recieveData.packet, i * 15 + 2);
        FrameManager.SendDataToView(unitInfo.ViewId, unitInfo);
    }
}

protected UnitNetworkDto ByteToUnitNetwork(byte[] arr, int offset)
{
    short viewId = BitConverter.ToInt16(arr, offset);
    float x = BitConverter.ToSingle(arr, offset + 2);
    float z = BitConverter.ToSingle(arr, offset + 6);
    short health = BitConverter.ToInt16(arr, offset + 10);
    byte teamType = arr[13];
    short targetViewId = BitConverter.ToInt16(arr, offset + 14);

    return new UnitNetworkDto
    {
        ViewId = viewId,
        X = x,
        Z = z,
        Health = health,
        TeamType = teamType,
        TargetViewId = targetViewId
    };
}

Also BitConverter isn't safe to use because its numbers format depends on CPU architecture. Check BitConverter.IsLittleEndian before use. If available, use BinaryPrimitives instead.

Also always keep in mind when you're thinking of performance: "Linq is slow".

deleted 74 characters in body
Source Link
aepot
  • 2.1k
  • 9
  • 20

First of all, use array instead of list. Every time you know the data size, use array instead of list.

Stop instantiating lot of new arrays, it's very slow. Use offset instead.

This one looks arr.Skip(12).Take(1).First(); looks really funnykilling. Why not simply arr[13]?

Also Int16 is short not int. Use matching types.

Let's assume that _recieveData.packet is byte[].

private void UdpFrameHandle()
{
    int unitCount = (_recieveData.packet.Length - 2) / 15;
    
    for (int i = 0; i < unitCount; i++)
    {
        var unitInfo = ByteToUnitNetwork(_recieveData.packet, i * 15 + 2);
        FrameManager.SendDataToView(unitInfo.ViewId, unitInfo);
    }
}

protected UnitNetworkDto ByteToUnitNetwork(byte[] arr, int offset)
{
    short viewId = BitConverter.ToInt16(arr, offset);
    float x = BitConverter.ToSingle(arr, offset + 2);
    float z = BitConverter.ToSingle(arr, offset + 6);
    short health = BitConverter.ToInt16(arr, offset + 10);
    byte teamType = arr[13];
    short targetViewId = BitConverter.ToInt16(arr, offset + 14);

    return new UnitNetworkDto
    {
        ViewId = viewId,
        X = x,
        Z = z,
        Health = health,
        TeamType = teamType,
        TargetViewId = targetViewId
    };
}

Also BitConverter isn't safe to use because its numbers firmat depends on CPU architecture. Check BitConverter.IsLittleEndian before use. If available, use BinaryPrimitives instead.

Also always keep in mind when you're thinking of performance: "Linq is slow".

First of all, use array instead of list. Every time you know the data size, use array instead of list.

Stop instantiating lot of new arrays, it's very slow. Use offset instead.

This one looks arr.Skip(12).Take(1).First(); really funny. Why not simply arr[13]?

Also Int16 is short not int. Use matching types.

Let's assume that _recieveData.packet is byte[].

private void UdpFrameHandle()
{
    int unitCount = (_recieveData.packet.Length - 2) / 15;
    
    for (int i = 0; i < unitCount; i++)
    {
        var unitInfo = ByteToUnitNetwork(_recieveData.packet, i * 15 + 2);
        FrameManager.SendDataToView(unitInfo.ViewId, unitInfo);
    }
}

protected UnitNetworkDto ByteToUnitNetwork(byte[] arr, int offset)
{
    short viewId = BitConverter.ToInt16(arr, offset);
    float x = BitConverter.ToSingle(arr, offset + 2);
    float z = BitConverter.ToSingle(arr, offset + 6);
    short health = BitConverter.ToInt16(arr, offset + 10);
    byte teamType = arr[13];
    short targetViewId = BitConverter.ToInt16(arr, offset + 14);

    return new UnitNetworkDto
    {
        ViewId = viewId,
        X = x,
        Z = z,
        Health = health,
        TeamType = teamType,
        TargetViewId = targetViewId
    };
}

Also BitConverter isn't safe to use because its numbers firmat depends on CPU architecture. Check BitConverter.IsLittleEndian before use. If available, use BinaryPrimitives instead.

First of all, use array instead of list. Every time you know the data size, use array instead of list.

Stop instantiating lot of new arrays, it's very slow. Use offset instead.

This one arr.Skip(12).Take(1).First(); looks really killing. Why not arr[13]?

Also Int16 is short not int. Use matching types.

Let's assume that _recieveData.packet is byte[].

private void UdpFrameHandle()
{
    int unitCount = (_recieveData.packet.Length - 2) / 15;
    
    for (int i = 0; i < unitCount; i++)
    {
        var unitInfo = ByteToUnitNetwork(_recieveData.packet, i * 15 + 2);
        FrameManager.SendDataToView(unitInfo.ViewId, unitInfo);
    }
}

protected UnitNetworkDto ByteToUnitNetwork(byte[] arr, int offset)
{
    short viewId = BitConverter.ToInt16(arr, offset);
    float x = BitConverter.ToSingle(arr, offset + 2);
    float z = BitConverter.ToSingle(arr, offset + 6);
    short health = BitConverter.ToInt16(arr, offset + 10);
    byte teamType = arr[13];
    short targetViewId = BitConverter.ToInt16(arr, offset + 14);

    return new UnitNetworkDto
    {
        ViewId = viewId,
        X = x,
        Z = z,
        Health = health,
        TeamType = teamType,
        TargetViewId = targetViewId
    };
}

Also BitConverter isn't safe to use because its numbers firmat depends on CPU architecture. Check BitConverter.IsLittleEndian before use. If available, use BinaryPrimitives instead.

Also always keep in mind when you're thinking of performance: "Linq is slow".

deleted 74 characters in body
Source Link
aepot
  • 2.1k
  • 9
  • 20

First of all, use array instead of list. Every time you know the data size, use array instead of list.

Stop instantiating lot of new arrays, it's very slow. Use offset instead.

This one looks arr.Skip(12).Take(1).First(); really funny. Why not simply arr[13]?

Also Int16 is short not int. Use matching types.

Let's assume that _recieveData.packet is byte[].

private void UdpFrameHandle()
{
    // also this one can be avoided but I don't know what is _recieveData.packet
    byte[]int pureDataunitCount = (_recieveData.packet.Skip(Length - 2).ToArray(); / 15;
    
    for (int i = 0; i < pureData.Length; i +=unitCount; 15i++)
    {
        var unitInfo = ByteToUnitNetwork(pureData_recieveData.packet, i * 15 + 2);
        FrameManager.SendDataToView(unitInfo.ViewId, unitInfo);
    }
}

protected UnitNetworkDto ByteToUnitNetwork(byte[] arr, int offset)
{
    short viewId = BitConverter.ToInt16(arr, offset);
    float x = BitConverter.ToSingle(arr, offset + 2);
    float z = BitConverter.ToSingle(arr, offset + 6);
    short health = BitConverter.ToInt16(arr, offset + 10);
    byte teamType = arr[13];
    short targetViewId = BitConverter.ToInt16(arr, offset + 14);

    return new UnitNetworkDto
    {
        ViewId = viewId,
        X = x,
        Z = z,
        Health = health,
        TeamType = teamType,
        TargetViewId = targetViewId
    };
}

Also BitConverter isn't safe to use because its numbers firmat depends on CPU architecture. Check BitConverter.IsLittleEndian before use. If available, use BinaryPrimitives instead.

First of all, use array instead of list. Every time you know the data size, use array instead of list.

Stop instantiating lot of new arrays, it's very slow. Use offset instead.

Also Int16 is short not int. Use matching types.

private void UdpFrameHandle()
{
    // also this one can be avoided but I don't know what is _recieveData.packet
    byte[] pureData = _recieveData.packet.Skip(2).ToArray();
    
    for (int i = 0; i < pureData.Length; i += 15)
    {
        var unitInfo = ByteToUnitNetwork(pureData, i);
        FrameManager.SendDataToView(unitInfo.ViewId, unitInfo);
    }
}

protected UnitNetworkDto ByteToUnitNetwork(byte[] arr, int offset)
{
    short viewId = BitConverter.ToInt16(arr, offset);
    float x = BitConverter.ToSingle(arr, offset + 2);
    float z = BitConverter.ToSingle(arr, offset + 6);
    short health = BitConverter.ToInt16(arr, offset + 10);
    byte teamType = arr[13];
    short targetViewId = BitConverter.ToInt16(arr, offset + 14);

    return new UnitNetworkDto
    {
        ViewId = viewId,
        X = x,
        Z = z,
        Health = health,
        TeamType = teamType,
        TargetViewId = targetViewId
    };
}

Also BitConverter isn't safe to use because its numbers firmat depends on CPU architecture. Check BitConverter.IsLittleEndian before use. If available, use BinaryPrimitives instead.

First of all, use array instead of list. Every time you know the data size, use array instead of list.

Stop instantiating lot of new arrays, it's very slow. Use offset instead.

This one looks arr.Skip(12).Take(1).First(); really funny. Why not simply arr[13]?

Also Int16 is short not int. Use matching types.

Let's assume that _recieveData.packet is byte[].

private void UdpFrameHandle()
{
    int unitCount = (_recieveData.packet.Length - 2) / 15;
    
    for (int i = 0; i < unitCount; i++)
    {
        var unitInfo = ByteToUnitNetwork(_recieveData.packet, i * 15 + 2);
        FrameManager.SendDataToView(unitInfo.ViewId, unitInfo);
    }
}

protected UnitNetworkDto ByteToUnitNetwork(byte[] arr, int offset)
{
    short viewId = BitConverter.ToInt16(arr, offset);
    float x = BitConverter.ToSingle(arr, offset + 2);
    float z = BitConverter.ToSingle(arr, offset + 6);
    short health = BitConverter.ToInt16(arr, offset + 10);
    byte teamType = arr[13];
    short targetViewId = BitConverter.ToInt16(arr, offset + 14);

    return new UnitNetworkDto
    {
        ViewId = viewId,
        X = x,
        Z = z,
        Health = health,
        TeamType = teamType,
        TargetViewId = targetViewId
    };
}

Also BitConverter isn't safe to use because its numbers firmat depends on CPU architecture. Check BitConverter.IsLittleEndian before use. If available, use BinaryPrimitives instead.

Source Link
aepot
  • 2.1k
  • 9
  • 20
Loading