Whenever you use [DllImport], the analyzer will nudge you to auto-fix it to [LibraryImport] which source-generates a marshalling stub (if any is needed) that then calls an inner [DllImport] that does not require runtime marshalling. This is very cheap since function address gets cached into a readonly static which then gets baked into the machine code once the JIT produces Tier-1 compilation for your method.
On NativeAOT, you can instead use "DirectPInvoke" which links against specified binary and relies on system loader just like C/C++ code would. Then, you can also statically link and embed the dependency into your binary (if .lib/.a is available) instead which will turn pinvokes into direct calls (marshalling if applicable and GC frame transition remain, on that read below).
Lastly, it is beneficial to annotate short-lived PInvoke calls with [SuppressGCTransition] which avoids some deoptimizations and GC frame transition calls around interop and makes the calls as cheap as direct calls in C + GC poll (a single usually not-taken branch). With this the cost of interop effectively evaporates which is one of the features that makes .NET as a relatively high-level runtime so good at systems programming.
Unmanaged function pointers have similar overhead, and identical if you apply [SuppressGCTransition] to them in the same way.
* LibraryImport is not needed if pinvoke signature only has primitives, structs that satisfy 'unmanaged' constraint or raw pointers since no marshalling is required for these.
Lost my wife about 1.5 years ago. It was expected and unexpected at the same time. Long metastatic cancer treatment that ended all of the sudden, in a few weeks of unconsciousness ("coma") with an auto immune brain disease, likely caused by chemo.
As the partner left behind, I nothing but empathy to Bess. As an avid, ultra pragmatic, HN reader though, I've gathered resources so I'll list them here:
Forums / chats:
https://www.reddit.com/r/widowers/ - This one I used immediately after. Yelling into the void. Crying. Having other people cry with me. Make sure I'm heard.
https://discord.gg/CFQfCdby - /r/widowers discord. This one is "good" for the first few days / weeks / months, when the pain is great and the sense of lost is overcoming and you just need someone to talk with, someone who's been through this, right now. Everyone is friendly, rules to keep things sane and not triggering are in effect.
Facebook groups - I know, ugh. But it helps to see other people in the same boat. Somehow. A little. For me it was "Young and Widowed With Children" (well, me) and some of the black humor groups e.g. "Widow(er) Humor". Find your tribe. It really does help.
Books:
It's ok you're not ok - https://www.amazon.com/Its-That-Youre-Not-Understand/dp/1622... - This is "the book". Everyone recommends it and it's justified. If you can't bring yourself to read, get the audible version. I did, it was easier to lie in bed with eyes closed.
Irreverent Grief Guide - https://www.amazon.com/gp/product/B08L5RRJ9D - this one is a "how to" guide. I mean a real "how to", emotionally. I, and possibly many on /r/widowers/ found it priceless.
- The invisible string - https://www.amazon.com/gp/product/031648623X
- Fix-it man - https://www.amazon.com/gp/product/1925335348
- Missing mummy - https://www.amazon.com/gp/product/0230749518
- The sad dragon - https://www.amazon.com/gp/product/1948040999
- Something very sad happened - https://www.amazon.com/gp/product/1433822660
Read once or twice:
- Love is forever - https://www.amazon.com/gp/product/0615884059
- I'll See You In The Moon - https://www.amazon.com/gp/product/1989123309
- My heart will stay - https://www.amazon.com/gp/product/0578794578
- The heart and the bottle - https://www.amazon.com/gp/product/0399254528
- Always remember - https://www.amazon.com/gp/product/0399168095
- The garden of lost balls - https://www.amazon.com/gp/product/B0BLQW27XX
- Gone but never forgotten - https://www.amazon.com/gp/product/B09SNY9VF3
Therapy and meds:
Actually, therapy and meds before, if not already. Anticipatory grief is a thing and processing it can make later days a bit easier. Anti anxiety meds (NDRI) can create "inoculation" effect to some extent. SSRIs probably as well. Understand depression, the symptoms, the issues. Educate family and friends. Establish rapport with a therapist.
Friends and community:
Expect loss of friends. It's terrible but it happens a lot. Extremely common that friends will silently disappear after a few days or weeks. Not even just joint friends. People are awkward around grief. Community, however, does seem to work well. Rely on them. Don't say no to food offers, it helps. Doordash! Don't be shy about it, it's fine to eat junk food. Don't drink though and don't get high, it deepens and prolongs the grief symptoms.
Calls:
Don't forget your family or close friends. I've had daily calls with my sister. It helped a ton. Scheduled daily calls.
On NativeAOT, you can instead use "DirectPInvoke" which links against specified binary and relies on system loader just like C/C++ code would. Then, you can also statically link and embed the dependency into your binary (if .lib/.a is available) instead which will turn pinvokes into direct calls (marshalling if applicable and GC frame transition remain, on that read below).
Lastly, it is beneficial to annotate short-lived PInvoke calls with [SuppressGCTransition] which avoids some deoptimizations and GC frame transition calls around interop and makes the calls as cheap as direct calls in C + GC poll (a single usually not-taken branch). With this the cost of interop effectively evaporates which is one of the features that makes .NET as a relatively high-level runtime so good at systems programming.
Unmanaged function pointers have similar overhead, and identical if you apply [SuppressGCTransition] to them in the same way.
* LibraryImport is not needed if pinvoke signature only has primitives, structs that satisfy 'unmanaged' constraint or raw pointers since no marshalling is required for these.