USE GetClass()->CheckEqual()
If you can, use the above, rather than Cast, especially on a BP graph.
GetClass() returns a pointer (4 bytes) to a UClass, and CheckEqual() will get the other UClass pointer, compare equality, and return a bool (1 byte). It’s incredibly cheap - like getting and comparing 2 floats. It runs only a few lines of code, from files that are likely already in memory - GameplayStatics & KimetMathLibrary & UObjectBase.
The BP Cast node creates an object - all member variables have to be initialized, all hard references need to be loaded, etc. This is almost certainly more expensive for memory, and I expect it’s more expensive on processing, even with the cost of 3 nodes versus 1, and especially so if using Blueprint Nativization.
Casting primitives (float to int, int to bool, etc.) in C++ is cheap. Casting to an object, though, requires trying to construct that object, so how cheap it is may depend on the objects involved.
BTW, the script we see when copying and pasting a node into Notepad is not relevant for memory or processing - this script does have the name of the Engine function being called (useful to know), but otherwise, I don’t believe it’s related to runtime. These scripts are mostly for the Editor to know how to display the nodes in the graph. It’s like the “Collapse Nodes” option - for human readability, without a runtime effect.
Also from above - to kill a hard references on the graph - use node “Set By-Ref Var” with Target == reference to kill, Value == None. The GC will see a reference to nullptr and dump it. Read about this on a blog, did a quick test, and it seems to work.
Another way to prevent storing the hard object reference of a cast, I imagine, is to cast within a function, so that after it runs, it’ll be destroyed. From the conversation above, though, it sounds like all references are loaded on BP construction, and I don’t know (and can’t easily test) if this keeps references within functions alive.
Would be interested to hear more.