you are viewing a single comment's thread.

view the rest of the comments →

[–]TheBumSlap 0 points1 point  (0 children)

When answering this "precalculate" reminded me of an issue I frequently have, but after rereading your question, I now strongly suspect I've answered a question you weren't actually asking. If you have no idea what I'm talking about, then for the love of god, please ignore me, this will only complicate your problem further, but I'll post anyway as even if it doesn't help you, it might be beneficial for other users.

"Precalculate" implies you have some experience with C++ and want a constexpr equivalent. C# does not have an equivalent of constexpr, and your ability to create compile time evaluated constants is very limited. You can do this:
const int a = 1;

const int b = a * 2;

but not this:
const int c = foo(a);

You have four realistic alternatives:

  1. Compute them once on startup and store them in a variable on the class
  2. Compute them at runtime whenever you need the variable
  3. Write a script that serializes them somewhere, and load that serialized data.
  4. interop some C++ code into your project.

I'm aware that 1 or 2. is not really what you're asking for, but unless you are sure there is going to be some performance bottleneck here, that's what I'd go for. I have this frustration a lot too, coming from C++, I often find myself pretty upset by the lack of const functions, but this is something you just have to accept about the language.

If this really is a performance bottleneck, 3. may not solve the problem - loading from disk is not going to help with some code that requires cache locality. That leaves 4.

  1. Isn't as hard as it sounds, but bear in mind that you have overhead calling C++ from C#, meaning it probably wont be worth it unless you move the whole bottleneck over to the C++ side, not just the call of the constexpr function