-
Notifications
You must be signed in to change notification settings - Fork 25
Patch conditional compilation #9
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
A few questions:
|
|
That's probably not a matter of how NuGet works, but how .NET assemblies work. As far as I understand, .NET assemblies are essentially CPU-neutral (setting compiler target for x64, x86 or arm doesn't seem to affect anything?), and CIL-to-machine code compilation is done during installation / loading of the DLL. Thus probably we can't do that in compile time. |
We can adjust the code so that it has 3 branches, little big and default (current code). NuGet would use default but the user could take advantage of this feature if he adjusts his .csproj. |
I have very vague idea of how .NET assemblies work, so it is really up to you guys to decide. If I'm right about the fact that they are essentially architecture-indepedent, then I'd vote to stick with current run-time detection routines, and, indeed, put alternative versions in the branches. |
I have a very vague idea how assemblies compile as well, but as far as I understand it, even platform-independant code does not keep multiple versions per compile symbols. Ergo, it prunes the code during (pre)compilation. I will fix it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of conditional compilation if we cache the bool locally on the KaitaiStream class:
static readonly bool IsLittleEndian = BitConverter.IsLittleEndian;
Then use that in ReadBytesNormalisedLittle/BigEndian
then the JIT compiler optimizes the Array.Reverse out when it doesn't apply. That way we get one nuget dll but optimal performance on all platforms.
Here is ReadBytesNormalisedLittleEndian()
on my machine, no if
check or Array.Reverse()
:
byte[] bytes = ReadBytes(count);
038A0F50 mov eax,edx
038A0F52 sar eax,1Fh
038A0F55 push eax
038A0F56 push edx
038A0F57 call 038A0D90
038A0F5C ret
021331a
to
632bde8
Compare
@Arlorean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks perfect!
Resolve "Add CancellationToken support." Closes kaitai-io#9 See merge request marta/kaitai_struct_csharp_runtime!19
Most important change: in project settings, a flag similiar to DEBUG and TRACE gets computed for IsLittleEndian. The code actually compiles conditionally on it. Better performance.
Someone needs to run test suite on it, I dont have it set up.