Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive

Comments

Add core.experimental.rc#2760

Closed
lesderid wants to merge 8 commits intodlang:masterfrom
lesderid:corex-rc
Closed

Add core.experimental.rc#2760
lesderid wants to merge 8 commits intodlang:masterfrom
lesderid:corex-rc

Conversation

@lesderid
Copy link
Contributor

This PR adds four @safe @nogc @nothrow reference counted data structures to core.experimental:

  • a bare-bones slice type with reference semantics,
  • a proper dynamic array type using the slice type,
  • a singly linked list type, and
  • a hash map type.

The slice in this PR differs from __rcptr!T (#2690) by using intrusive reference counting instead of the refcount being external to the object, increasing performance. This also means we can safely call free on the pointer because we always allocate memory ourselves with malloc.

Sadly, we're still stuck with using atomics for the reference count (see discussion here: #2679 (comment)). I believe this could only reasonably be solved by changing the immutable => shared conversion rules. This would probably break a lot of code (and obviously requires a DIP) and I'm not sure what the deprecation process would look like. I'll start a thread on the NG soon to discuss this issue further.

(The hash map does not yet support (non-empty) creating immutable instances. For now, one should be able to use the same idiom that can be used for the built-in ones: https://dlang.org/spec/hash-map.html#runtime_initialization.)

@lesderid
Copy link
Contributor Author

lesderid commented Sep 2, 2019

With the last commit (57b38e1) and using non-atomic ref counting, rcarray is faster than std.container.array for most mixed benchmarks and nearly as fast as std::vector (with a wrapper):

Benchmark plots

Mixed 1
Mixed 2
Mixed 3

cc @wilzbach

@lesderid lesderid marked this pull request as ready for review September 2, 2019 16:48
@aG0aep6G
Copy link
Contributor

aG0aep6G commented Sep 3, 2019

As far as I can tell, these containers are not @safe.

They allow obtaining references to their elements, and they don't manage to do it @safely. I'm not sure where we stand with DIP 1000, etc. Maybe it just can't be done, yet.

Examples where references to elements outlive their containers (i.e. they become dangling):

ref int slice() @safe
{
    auto s = __rcslice!int(1);
    s[0] = 42;
    return s[0];
}

ref int array() @safe
{
    rcarray!int a;
    a ~= 42;
    return a[0];
}

ref int slist() @safe
{
    rcslist!int s;
    s.insertFront(42);
    return s.front;
}

ref int map() @safe
{
    rcmap!(int, int) m;
    m[0] = 42;
    return *(0 in m);
}

@dlang-bot dlang-bot added Needs Rebase needs a `git rebase` performed stalled labels May 27, 2021
@RazvanN7
Copy link
Contributor

RC requires __mutable to be implemented. There is a reference to this PR here: https://issues.dlang.org/show_bug.cgi?id=23071

@RazvanN7 RazvanN7 closed this Apr 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Needs Rebase needs a `git rebase` performed stalled

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants