Delegate caching. Is it safe to use or some dirty hack to avoid? (code below)
Here's some delegate caching into struct...
19 Replies
what on earth?
is this supposed to somehow.... what?
okay, so
you're trying to pre-allocate a delegate for each method on
example
which consumers of example
can use
what exactly is wrong with...
what does the 7 layers of indirection accomplish?
or better yet
on a fundamental note, I'm not convinced this would even optimize anything
do you have any actual data to back up a need for something like this?It should cache delegates, which called somewhere...
More real world code...
okay, but again
what purpose does any of the indirection serve
the ENTIRETY of the
action<n>
struct and the M
type parameter on the base class accomplishes one thing: you get to move _methods = new M()
from the subclass into the base class
it doesn't save you from writing lines of code to define WHAT the methods are, on each base class
it doesn't allow someone who only has the base class, without knowing the subtype to use any of the methods
you're trying to enforce a coding paradigm with the compiler
don't do thatAs defaul field initializers can't reference anything except static, so all should be done in class initializer
To keep inherited objectects initializer clear i want to make fields somehow
Also need to cache Actions to prevent allocations inside OnAfterDisable.
especially since this structure doesn't actually really FORCE the designer to do what you want
this accomplishes all requirements
except "keep inherited objectects initializer clear"
which your solution doesn't accomplish either
not to mention
even if this WAS sensible
you can still get rid of the entire
action<N>
struct with...
ah, nevermind on that last bitalso required not supported by my language version
??
what're you using some long-outdated version of Unity?
Unity's last supported C# is 9.0.
required is C# 11.0 feature
okay
so don't use that
oh, okay, that's what you meant
"required" meaning the keyword
really,
required
in that context doesn't accomplish anything anywaybut it's too new
what is?
"required" keyword. Too new for usage in Unity. They're still can't update to .net 6 or above
yes, I know
cause you just said that
to which I responded "don't use that"
then I elaborated that it doesn't accomplish anything in this scenario anyway, so I shouldn't have put it there
Problem could be solved with propety with baking field, or using constructor. First requires adding a baking field and null check every time. Second adds more constructor-related code (but there's already enough code as SetContext) and divides methods and it's creation onto two code parts which is not very convenient
you want #2
put initialization code in your constructor
hell, move everything in
SetContext
there if possibleContext could be changed.
then keep
SetContext()
and add it as a constructor parameter
if it's necessary for a context to be provided before the object can function, it belongs in the constructor
if it can be changed later, that's a separate requirementIt can be changed. The whole this hack is supposed to cache delegates and reuse them when context changed and pooling is needed.
yes, I understand
you do not need a monstrous base class to accomplish this