Skip to content

Instantly share code, notes, and snippets.

@rikkimax
Last active December 31, 2021 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikkimax/95994607813d6665b6adbfb36d180c42 to your computer and use it in GitHub Desktop.
Save rikkimax/95994607813d6665b6adbfb36d180c42 to your computer and use it in GitHub Desktop.

Abstract

Reference counting allows for a constant cost with predictable lifetime ownership patterns. This is quite often used with system resources or in times when neither scope ownership nor GC are accecptable memory allocation or lifetime schemes. By adding it into the language, further improvements may be possible including for read only types.

Motivation

Atomic mutation of counters is quite expensive as the mutation and access invalidates the cpu cache. This has been shown in real world scenarios to have significant impact on a programs performance https://pkolaczk.github.io/server-slower-than-a-laptop/

Due to this, D needs to solidfy the existing pattern of reference counting operations into the language so that they may be seperated out from construction and destruction of a given type. Seperating out from construction/destruction allows for future optimizations and memory rules that are specific to reference counting while maintaining correctness.

Description

Adds two new operator overloads opRefAdd and opRefSub. These two methods are used as a replacement for copy constructors/postblit and destructors on a struct for handling of reference counting.

Adding a reference occurs when a copy or a move of a struct or a class reference occurs.

Removing a reference occurs when a scope ends for a given variable or a setting of a variable.

If a pair of reference adding and subtraction occur in any order on a given object, the compiler may elide both the calls if it can prove it is the same object. This has the benefit of making reference counting potentially very cheap and future contributions of const based reference counting being possible.

Abstract

Resources managed by the language may be thread specific, rather than purely scope based. For this where you want to guarantee that an owning reference is locked to a single thread scope can be kept but an escape mechanism that allows for a reference to that thing to escape is needed. This DIP introduces an operator overload that is called when scope would be escapped and limit the memory from actually escaping.

Description

Adds a new operator overload that is called when escaping of scope occurs.

In @safe code, it is forbidden to return any memory that was acquired from the this pointer. This means that with DIP1000 it is not allowed for the operator to be return nor may the value returned be scope.

Example of what is not allowed:

@safe:

void main() {
    scope Foo foo = new Foo;
    // Error: scope variable `foo` assigned to non-scope parameter `this` calling onlineapp.Foo.opScopeEscape
    auto got = foo.opScopeEscape();
}

class Foo {
@safe:
    
    auto opScopeEscape() {
        return this;
    }
}

And:

class Foo {
@safe:
    
    auto opScopeEscape() return {
        return this;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment