kildom / cppUtils

Small C++ utility library inspired by high level languages like Python or JavaScript
0 stars 0 forks source link

Add another kind of dollar reference to reduce reference counter updates #1

Open kildom opened 1 month ago

kildom commented 1 month ago

Add another kind of dollar references (lets call it for now $1 and $2). It would be useful for parameters to reduce reference counter updates.

When passing some reference (one of tree old ones) new object should be created on the stack (type $1) that increments the counter. It is later converted to reference (type $2) that does not update reference counter since we know that the first object ($1) will hold reference as long as it is on the stack. Passing such reference down will not increase the reference counter making its performance the same as raw pointer.

Local variables can be $1. This way passing it to parameters of type $2 would not need the reference counter updates. Using $1 in different cases than local variable is misuse and it is security risk, so maybe $1 should be internal type.

kildom commented 1 month ago

Possible naming:


DOLLAR_CLASS(Foo);
class FooClass;

Foo x; // instance reference
Foo$ x; // non-null reference
Foo$N x; // nullable reference
Foo$$ x; // non-null fast reference
Foo$$N x; // nullable fast reference
// Class $1 from the description above should be hidden, but can be used with the following pattern
auto p = -x;
f(p);
g(p);
// Which is faster version of:
f(-x);
g(-x);
// operator "-" converts Foo, Foo$ or Foo$N to $1 which is implicitly converted to Foo$$ or Foo$$N
kildom commented 1 month ago

Type $2 (Foo$$ or Foo$$N) should be PDO if possible, because this way it will be passed over registers. Other references will be passed over memory since they are to complex.

There should not be an assign operator for the Foo$$ and Foo$$N types to avoid its misuse.