New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Core] Make object id more performant #1957
Conversation
256ec5b
to
4dcdb99
Compare
there would be a faster option but it faces the same problem : netstandard2.0 string.Create doesnt exist in netstandard2.0
|
the new toguid is around 2-4 times faster than the old one, but again its netstandard 2.0 so it cant be used the original intention to half the allocation is achieved, the mini performance gain probably doesnt matter after all and isnt worth the trouble in a 15 min session tostring was in the top memory allocating methods |
964a8d6
to
6fffca6
Compare
It has to be compatible with .NET Framework 4.7.2 which is what VS is targeting. And that framework version only supports netstandard2.0 |
Note you can surround with |
updated help section, is ready for review and extra tests
i will just go with the current version so it makes least amount of confusion the main problem why i looked into it was the allocation as object id to string is often used for referenc a version without unsafe but its slower in tostring but ALOT faster in toguid and the byte operator EDIT: forgot the reference lololo https://paste.mod.gg/gifhzrqjvzfl/0 |
seems like assembly procsoor doestn get build, dont know why i can build it with the changes would be nice if someone can take over the PR and help me with that ( if its not casued iwth the current situation that it cant build ) |
I'll have a look at making the 2 versions (netstandard and net6+) in this PR - thanks for starting it |
Thank you!
this is the fastest one i was able to make ( with help ) in the linked resource its as double as slow as the original one, so dont take that one :D public Guid ToGuid()
{
return MemoryMarshal.Cast<ObjectId, Guid>(
MemoryMarshal.CreateReadOnlySpan(ref this, 1))[0];
} this was 2-4 times faster but requires also netstandard2.1 or net6 this should be also faster public static explicit operator ObjectId(Guid guid)
{
return MemoryMarshal.Cast<Guid, ObjectId>(
MemoryMarshal.CreateReadOnlySpan(ref guid, 1))[0];
} the guy who helped me said that simd helps here, whatever that means, but as always i only tested it with == compare, which is not much for these as i couldnt get stride working with it :D |
@IXLLEGACYIXL This code can actually be simplified to // To ObjectId
Unsafe.As<Guid, ObjectId>(ref guid)
// From ObjectId
Unsafe.As<ObjectId, Guid>(ref this) This is a single IL instruction can't be cheaper than this 😄 no need to use spans here. There is still a lot that can be improved in this struct :) |
I think the PR is strictly for converting them to string, otherwise we already have operators to convert back and forth between guid and objectid. See: |
dotlogix gave me a suggestion on how to improve these too, i will add them |
https://paste.mod.gg/gifhzrqjvzfl/0 |
added the improvements this shouldnt be merged as manio will make the improvements so they work depedent on the target framework |
|
the original attempt was just to hallf the memory allocation, mine currently works with both and achieves the target of halfing the allocation the unsafe as is just a code improvement, same with the gethashcode, making it more readable mine could be merged as is right now in terms of compatibility with the assemblyprocessor ... ofcourse i wish a test from someone else too :D |
should manio make it with preprocessor directives or should we go with what i have now? the current thing can be merged after a maintainer ( or someone else ) tried it, as known now assemblyprocessor recompilation leads to different results so i want to make sure i dont break anything |
This is treated specially by JIT and inlined to a pointer cast effectively. But it's supposed to be safe to use without @IXLLEGACYIXL made a PR against your branch here: NexStandard#1 |
Make ObjectId changes work with AssemblyProcessor
thank you |
i want to run today again the tests and then it should be fine |
I had this error locally as well, but then I rebuilt and it started working again somehow, so unclear what is causing |
i cant view that |
You can see build details as Guest - there's a link beneath login form. |
PR Details
the to string in object id has an array which allocates memory, using span would remove it
Description
im not sure if that is even wanted.
problem is with the new byte[] which could be made a span but assemblyprocessor is netstandard2.0 so it doesnt detect the new string(span) so i have to use cast to readonly span
the benchmark is here
so memory allocation is halfed and object id is frequently called
performance wise it doesnt do too much
would the assemblyprocessor be 2.1 then the "faster" way with new string(span) could be chosen but im not sure why the assemblyprocessor is netstandard2.0 and not netstandard2.1
Motivation and Context
halfing the allocation in a frequently called method
Types of changes
Checklist
help
idk how that works with the assembly processor, as this file is referenced by it so i think it doesnt need a commit?
a test of it would be good, if a bigger project opens it and it works then it should work, i couldnt make projects fail with that
i had to repair in stride.core.targets the assemblyprocessor path as i cant buid else stride anymore, this is not inlcuded in that PR but should be considered that someone with normal stride can build it