Repository navigation
Removed reflection from composite read and write - #2402
Conversation
roji
left a comment
There was a problem hiding this comment.
Great to see this @YohDeadfall - but where are the benchmark results that show how much faster it is? :) Anyway, here's a first review - this isn't trivial code so it may take more than one back and forth... Hopefully I've understood everything correctly and haven't written anything silly.
|
There are no benchmarks because I had no time, but am planning to write them and tests too. |
austindrenski
left a comment
There was a problem hiding this comment.
This is some exciting work. Looking forward to seeing the benchmarks too.
A few questions and suggestions, but otherwise looking good.
There was a problem hiding this comment.
This isn't my area of expertise, so please forgive what may actually be a silly question:
Is there a perf reason to avoid checking pgFields[pgFieldIndex].Name == name directly? Is repeatedly reassigning the local cheaper than the one additional index access?
For example, assuming that we're dealing with a composite of 4 fields, (a, b, c, d) and we want to get field a:
The current version (pgField.Name == name):
- indexes
pgFields4 times - accesses
.Name4 times - writes
pgField4 times
But if it checked pgFields[pgFieldIndex].Name == name directly, it would need to:
- indexes
pgFields5 times - accesses
.Name4 times - writes
pgField1 time.
There was a problem hiding this comment.
Don't know and will check.
There was a problem hiding this comment.
Checked and verified that a local variable could improve perf when an iteration goes over an array. Probably it's the right behavior because there could be side effects and the JIT takes that into account. The last two tests (without direct return) demonstrate the current scenario when the result is used after a cycle.
One small note before you will see the test results. This isn't applicable to the commented code since pgFields isn't an array but an IReadOnlyList<T>. Therefore, the JIT must call the indexer each time.
BenchmarkDotNet=v0.11.5, OS=Windows 10.0.16299.1029 (1709/FallCreatorsUpdate/Redstone3)
Intel Core i7-8650U CPU 1.90GHz (Kaby Lake R), 1 CPU, 8 logical and 4 physical cores
Frequency=2062499 Hz, Resolution=484.8487 ns, Timer=TSC
.NET Core SDK=2.2.104
[Host] : .NET Core 2.2.2 (CoreCLR 4.6.27317.07, CoreFX 4.6.27318.02), 64bit RyuJIT
DefaultJob : .NET Core 2.2.2 (CoreCLR 4.6.27317.07, CoreFX 4.6.27318.02), 64bit RyuJIT
| Method | Mean | Error | StdDev |
|---|---|---|---|
| Iteration_WithLocal_DirectReturn | 1.972 ns | 0.1022 ns | 0.1177 ns |
| Iteration_WithoutLocal_DirectReturn | 2.138 ns | 0.1012 ns | 0.1039 ns |
| Iteration_WithLocal_NoDirectReturn | 1.713 ns | 0.0952 ns | 0.0978 ns |
| Iteration_WithoutLocal_NoDirectReturn | 1.956 ns | 0.1051 ns | 0.1438 ns |
private readonly Field[] pgFields = new[]
{
new Field("one"),
new Field("two"),
new Field("three"),
};
[Benchmark]
public Field Iteration_WithLocal_DirectReturn()
{
for (var pgFieldIndex = pgFields.Length - 1; pgFieldIndex >= 0; --pgFieldIndex)
{
var pgField = pgFields[pgFieldIndex];
if (pgField.Name == "three")
{
return pgField;
}
}
return null;
}
[Benchmark]
public Field Iteration_WithoutLocal_DirectReturn()
{
for (var pgFieldIndex = pgFields.Length - 1; pgFieldIndex >= 0; --pgFieldIndex)
{
if (pgFields[pgFieldIndex].Name == "three")
{
return pgFields[pgFieldIndex];
}
}
return null;
}
[Benchmark]
public Field Iteration_WithLocal_NoDirectReturn()
{
Field pgField = null;
for (var pgFieldIndex = pgFields.Length - 1; pgFieldIndex >= 0; --pgFieldIndex)
{
pgField = pgFields[pgFieldIndex];
if (pgField.Name == "three")
{
break;
}
}
return pgField;
}
[Benchmark]
public Field Iteration_WithoutLocal_NoDirectReturn()
{
Field pgField = null;
for (var pgFieldIndex = pgFields.Length - 1; pgFieldIndex >= 0; --pgFieldIndex)
{
if (pgFields[pgFieldIndex].Name == "three")
{
pgField = pgFields[pgFieldIndex];
break;
}
}
return pgField;
}There was a problem hiding this comment.
Very interesting, thanks for taking a look at it!
roji
left a comment
There was a problem hiding this comment.
See the nits and suggestions but otherwise look pretty good to me!
@YohDeadfall I'd like to see what things would look like without boxing structs in the sync path, if you feel this is too much we can merge and then maybe I'll give it a try (or not, depending if I have time...).
Mapped composites
Unmapped composites
|
|
Really nice to see the perf improvement @YohDeadfall! Will try to review this and approve ASAP. |
|
Changed composite materialization to avoid Mapped class
Mapped struct
Unmapped class
Unmapped struct
|
roji
left a comment
There was a problem hiding this comment.
LGTM modulu the comments below, thanks @YohDeadfall!
|
Feel free to merge @YohDeadfall (don't forget to squash all those commits...). |
|
Will merge when answer on #2402 (comment). |
|
🍾 |
Fixes #2377.