Skip to content

Commit 4ba71e2

Browse files
committed
Tensor.Explicit.cs: perf-ops
1 parent 533f8fd commit 4ba71e2

1 file changed

Lines changed: 57 additions & 22 deletions

File tree

src/TensorFlowNET.Core/Tensors/Tensor.Explicit.cs

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,110 @@
11
using System;
2+
using System.Runtime.CompilerServices;
23

34
namespace Tensorflow
45
{
56
public partial class Tensor
67
{
78
public static explicit operator bool(Tensor tensor)
89
{
9-
EnsureScalar(tensor);
10-
return tensor.Data<bool>()[0];
10+
unsafe
11+
{
12+
EnsureScalar(tensor);
13+
return *(bool*) tensor.buffer;
14+
}
1115
}
1216

1317
public static explicit operator sbyte(Tensor tensor)
1418
{
15-
EnsureScalar(tensor);
16-
return tensor.Data<sbyte>()[0];
19+
unsafe
20+
{
21+
EnsureScalar(tensor);
22+
return *(sbyte*) tensor.buffer;
23+
}
1724
}
1825

1926
public static explicit operator byte(Tensor tensor)
2027
{
21-
EnsureScalar(tensor);
22-
return tensor.Data<byte>()[0];
28+
unsafe
29+
{
30+
EnsureScalar(tensor);
31+
return *(byte*) tensor.buffer;
32+
}
2333
}
2434

2535
public static explicit operator ushort(Tensor tensor)
2636
{
27-
EnsureScalar(tensor);
28-
return tensor.Data<ushort>()[0];
37+
unsafe
38+
{
39+
EnsureScalar(tensor);
40+
return *(ushort*) tensor.buffer;
41+
}
2942
}
3043

3144
public static explicit operator short(Tensor tensor)
3245
{
33-
EnsureScalar(tensor);
34-
return tensor.Data<short>()[0];
46+
unsafe
47+
{
48+
EnsureScalar(tensor);
49+
return *(short*) tensor.buffer;
50+
}
3551
}
3652

3753
public static explicit operator int(Tensor tensor)
3854
{
39-
EnsureScalar(tensor);
40-
return tensor.Data<int>()[0];
55+
unsafe
56+
{
57+
EnsureScalar(tensor);
58+
return *(int*) tensor.buffer;
59+
}
4160
}
4261

4362
public static explicit operator uint(Tensor tensor)
4463
{
45-
EnsureScalar(tensor);
46-
return tensor.Data<uint>()[0];
64+
unsafe
65+
{
66+
EnsureScalar(tensor);
67+
return *(uint*) tensor.buffer;
68+
}
4769
}
4870

4971
public static explicit operator long(Tensor tensor)
5072
{
51-
EnsureScalar(tensor);
52-
return tensor.Data<long>()[0];
73+
unsafe
74+
{
75+
EnsureScalar(tensor);
76+
return *(long*) tensor.buffer;
77+
}
5378
}
5479

5580
public static explicit operator ulong(Tensor tensor)
5681
{
57-
EnsureScalar(tensor);
58-
return tensor.Data<ulong>()[0];
82+
unsafe
83+
{
84+
EnsureScalar(tensor);
85+
return *(ulong*) tensor.buffer;
86+
}
5987
}
6088

6189
public static explicit operator float(Tensor tensor)
6290
{
63-
EnsureScalar(tensor);
64-
return tensor.Data<float>()[0];
91+
unsafe
92+
{
93+
EnsureScalar(tensor);
94+
return *(float*) tensor.buffer;
95+
}
6596
}
6697

6798
public static explicit operator double(Tensor tensor)
6899
{
69-
EnsureScalar(tensor);
70-
return tensor.Data<double>()[0];
100+
unsafe
101+
{
102+
EnsureScalar(tensor);
103+
return *(double*) tensor.buffer;
104+
}
71105
}
72106

107+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
73108
private static void EnsureScalar(Tensor tensor)
74109
{
75110
if (tensor == null)

0 commit comments

Comments
 (0)