www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9265] New: Nullable fixed-sized array wrapper

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9265

           Summary: Nullable fixed-sized array wrapper
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2013-01-03 05:25:28 PST ---
When I use fixed-sized arrays a lot, sometimes I need a nullable version of
them (fixed-sized arrays are allocated in-place, so they often avoid heap
allocations, avoiding the creation of some garbage, reducing the GC pressure
and generally speeding up the code). A "ref" can't be used because it can't be
null.

So I think the normal way to use them in D is this, but the "(*arr)[1]" syntax
is bug-prone and not elegant:


alias TA = immutable(int[5]);
bool foo(TA* arr) {
    if (arr)
        return (*arr)[1] == 20;
    else
        return false;
}
void main() {
    TA a;
    foo(&a);
}



std.typecons.Nullable and std.typecons.NullableRef contain enforce() that
throws and kills inlining.


This simple struct inspired by NullableRef is light, avoids the problem of
NullableRef and allows for a natural syntax for nullable fixed sized arrays:



// Nullable fixed-sized array --------
struct Ptr(T) {
    private T* ptr;

    this(T* ptr_) pure nothrow {
        this.ptr = ptr_;
    }

    bool opCast(T)() const pure nothrow if (is(T == bool)) {
        return ptr !is null;
    }

     property ref inout(T) get()() inout pure nothrow
    in {
        assert(ptr);
    } body {
        return *ptr;
    }

    alias get this;
}

Ptr!T ptr(T)(ref T x) {
    return typeof(return)(&x);
}

// Example usage ----------

alias TA = immutable(int[5]);

bool foo(Ptr!TA arr=Ptr!TA.init) nothrow {
    if (arr)
        return arr[1] == 20;
    else
        return false;
}

bool foo(typeof(null) _) nothrow {
    return false;
}

void main() {
    assert(!foo());
    assert(!foo(null));
    TA items = [10, 20, 30, 40, 50];
    assert(foo(ptr(items)));
}
//---------------------------------



Unfortunately my benchmarks show that with the current DMD 2.061 the code that
uses such wrapped array is not as efficient as the first program that uses the
pointer to the fixed sized array.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 03 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9265


monarchdodra gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra gmail.com


--- Comment #1 from monarchdodra gmail.com 2013-02-04 22:57:48 PST ---
(In reply to comment #0)
 std.typecons.Nullable and std.typecons.NullableRef contain enforce() that
 throws and kills inlining.

FYI: I submitted a pull that changes those enforces to logic errors (asserts): https://github.com/D-Programming-Language/phobos/pull/1103 This should allow the code to inline. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 04 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9265



--- Comment #2 from github-bugzilla puremagic.com 2013-03-02 10:06:08 PST ---
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/e2831a5f2a7f3a8b0df4e475ff4f79fea2ff5c0f
Fixes Issue 9265 - assertNotThrown should emit msg from thrown exception if
available.

https://github.com/D-Programming-Language/phobos/commit/ba4095de5f2c6c62dbec45b71ec3e4ba3d802f90
Merge pull request #1185 from AndrejMitrovic/Fix9625

Issue 9265 - assertNotThrown should emit msg from thrown exception if
available.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 02 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9265



--- Comment #3 from bearophile_hugs eml.cc 2013-03-02 12:48:12 PST ---
(In reply to comment #2)
 Commits pushed to master at https://github.com/D-Programming-Language/phobos
 
 https://github.com/D-Programming-Language/phobos/commit/e2831a5f2a7f3a8b0df4e475ff4f79fea2ff5c0f
 Fixes Issue 9265 - assertNotThrown should emit msg from thrown exception if
 available.
 
 https://github.com/D-Programming-Language/phobos/commit/ba4095de5f2c6c62dbec45b71ec3e4ba3d802f90
 Merge pull request #1185 from AndrejMitrovic/Fix9625
 
 Issue 9265 - assertNotThrown should emit msg from thrown exception if
 available.

Is this related? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 02 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9265


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com


--- Comment #4 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-03-02
13:39:58 PST ---
(In reply to comment #3)
 (In reply to comment #2)
 Commits pushed to master at https://github.com/D-Programming-Language/phobos
 
 https://github.com/D-Programming-Language/phobos/commit/e2831a5f2a7f3a8b0df4e475ff4f79fea2ff5c0f
 Fixes Issue 9265 - assertNotThrown should emit msg from thrown exception if
 available.
 
 https://github.com/D-Programming-Language/phobos/commit/ba4095de5f2c6c62dbec45b71ec3e4ba3d802f90
 Merge pull request #1185 from AndrejMitrovic/Fix9625
 
 Issue 9265 - assertNotThrown should emit msg from thrown exception if
 available.

Is this related?

No it was a typo in the commit message, sorry. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 02 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9265


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #5 from bearophile_hugs eml.cc 2013-03-02 13:42:02 PST ---
Using the updated NullableRef in my code I've seen that it is good enough for
the purposes explained here, so I close down this issue.

I'd like a more handy way to create a nullable, like:

Ptr!T ptr(T)(ref T x) {
    return typeof(return)(&x);
}


So I've created the Issue 9637

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 02 2013