built-in array ptrEnd

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Sep 17 10:11:30 PDT 2012


On 9/17/12 12:34 PM, monarch_dodra wrote:
> I love D's concept of arrays (fat pointers).
>
> However, one thing I've found it lacks is a (convenient) way to get the
> end ptr.
>
> Phobos (and druntime) are riddled with "arr.ptr + arr.length". It is
> ugly and inconvenient, and makes something that should be easy to
> understand that much harder.
>
> Then I thought: "std.array" defines all the functions required to
> enhance arrays. Why not just add a "ptrEnd" in there? So I did.
>
> The rational is that now, we can write:
>
> bool isDisjoint = a.ptrEnd <= b.ptr || b.ptrEnd <= a.ptr;
>
> More elegant than:
>
> bool isDisjoint = a.ptr + a.length <= b.ptr ||
> b.ptr + b.length <= a.ptr;
>
> Nothing revolutionary, but it *is* easier on the fingers when typing :D
> . Also, it *does* make a change in some bigger and more complicated cases.
>
> Anyways, pull request:
> https://github.com/D-Programming-Language/phobos/pull/798
>
> I wanted to have some feedback, as this is introducing something new (as
> opposed to fixing something existing).
>
> IMO, this should really be built-in, in particular, since, in my
> understanding, an array is internally represented by the ptr and ptrEnd
> pair anyways. If the compiler has access to it, it might as well
> communicate it (rather than us re-calculating it...)

To be blunt, I think this is a terrible idea for a convenience function. 
Note that I'm only allowing myself to say this because monarch_dodra has 
clearly made other excellent contributions so I assume his ideas can 
take a bit of a destruction.

Normal code isn't supposed to mess with pointers and stuff, particularly 
with pointers past the end of arrays. That's rare. If Phobos uses .ptr 
with any frequency it's because it's low-level code that should optimize 
for performance compulsively.


Andrei


More information about the Digitalmars-d mailing list