January 28, 2019
On 1/28/19 7:39 AM, FeepingCreature wrote:
> On Friday, 25 January 2019 at 14:33:16 UTC, Steven Schveighoffer wrote:
>> On 1/25/19 3:20 AM, FeepingCreature wrote:
>>> On Thursday, 24 January 2019 at 17:49:34 UTC, Ali Çehreli wrote:
>>> Aren't the semantics of .clear that it's invalid to access references to .data after calling .clear, period? And if not, then shouldn't they be? Consider if Appender managed its own memory and held on to previously-allocated arrays while you were appending, only to free them on .clear. That seems like something Appender should be allowed to do. If you don't want it, just reinitialize Appender instead of calling .clear.
>>
>> You are advised not to. But it's not unsafe, as the memory is still there.
>>
> 
> That's stupid. Why would you advise me not to, if there's no problem with it? Either it should be accepted or it should be forbidden, just like warnings.

Just like this:

int[] arr = [1,2,3,4,5];
auto slice = arr[0 .. 2];
slice.assumeSafeAppend;

Now, you can use arr still, it's got elements in it beyond what slice has. You might have things change underneath you without expecting it. That's why it's advised not to do that.

But if arr is immutable(int)[], then you are running into undefined behavior, it's a completely different problem.

>> Without good reasons to change, I don't see why it would be accepted.
>>
>> Maybe you can describe your use case?
>>
> 
> My use case is simply the standard usecase of Appender: I want to build up an array in a way that reduces GC churn. Maybe it's an array of structs that contain const members that I'll serialize to JSON and send on a socket. In that case, I know for a fact that no references will hang around past the serialization. That's what clear _is for._ I don't see why this would be different with const or immutable data; if you hold references past .clear being called you're in trouble *anyways.*

Right, this does seem like a big limitation. Keeping with the spirit of how slices don't own the memory in question, Appender is being conservative with what it doesn't know.

I wonder if it may be more appropriate to instead of preventing clear() on immutable/const arrays, to make it @system. Or maybe call it something different "dangerousClear" or something ;)

There definitely should be some way to fail if clear is called on an array that was passed into the constructor.

But I'm still not sure we should allow overwriting immutable memory without a cast, even in @system code.

> I consider initializing Appender with an array referencing immutable data a borderline error anyways. The entire point of Appender is that it caches capacity data of GC managed memory, which is never immutable. On the first append to an immutable-memory array, it has to reallocate *anyways*. There is no benefit to initializing an Appender with immutable memory over just appending it first thing, unless you never plan to append to it ever.

It will inspect the allocated length from the GC if the array is appendable from the beginning. So it's not always going to reallocate.

e.g.:

string x = "abc".idup;

auto app = x.appender;

app ~= "xyz"; // does not reallocate.

-Steve
January 30, 2019
On Monday, 28 January 2019 at 15:16:54 UTC, Steven Schveighoffer wrote:
> It will inspect the allocated length from the GC if the array is appendable from the beginning. So it's not always going to reallocate.
>
> e.g.:
>
> string x = "abc".idup;
>
> auto app = x.appender;
>
> app ~= "xyz"; // does not reallocate.
>
> -Steve

Fair enough.

>> My use case is simply the standard usecase of Appender: I want to build up an array in a way that reduces GC churn. Maybe it's an array of structs that contain const members that I'll serialize to JSON and send on a socket. In that case, I know for a fact that no references will hang around past the serialization. That's what clear _is for._ I don't see why this would be different with const or immutable data; if you hold references past .clear being called you're in trouble *anyways.*
>
> Right, this does seem like a big limitation. Keeping with the spirit of how slices don't own the memory in question, Appender is being conservative with what it doesn't know.
>
> I wonder if it may be more appropriate to instead of preventing clear() on immutable/const arrays, to make it @system. Or maybe call it something different "dangerousClear" or something ;)
>
> There definitely should be some way to fail if clear is called on an array that was passed into the constructor.
>
> But I'm still not sure we should allow overwriting immutable memory without a cast, even in @system code.
>

My problem is this.

const and immutable are *not* well supported in the standard library at the moment. What I want is that I can use the idioms of the stdlib, semantically, in a way that lets me *not care* about const or immutable, that lets me express the patterns I want without having to worry about whether some type deep inside my code, a dub library, or phobos itself decided to declare a field immutable.

Appender covers two usecases: "capacity caching" and "memory reuse." Both of those usecases have two const variations: "possibly immutable" and "mutable".

  mutable capacity caching | mutable memory reuse
---------------------------+---------------------
immutable capacity caching | immutable memory reuse

But instead of cutting between capacity caching and memory reuse, down the middle, Appender cuts between mutable and immutable, left to right. I think this is a symptom of a broad negligence of constness as a first-class property - constness works sometimes, and it's nice if it does, but it can't be *relied* on to be well supported. This makes using const in D an eternal uphill struggle. Why even go to all the trouble to make a new major version of the language just to introduce constness, if it's not going to be treated as a first-class concern? I don't want to have to sprinkle static if(isAssignable!T) everytime I want to use a library type. If Phobos offers me as generic a data structure as Appender, parameterized on T, it should work for T, regardless of what T is doing, and *certainly* regardless of what fields in T are marked const. Especially considering that if a field is not marked as const, that hardly means it's not going to lead to bugs if its memory is reused while you're referencing it!
January 30, 2019
On 1/30/19 7:32 AM, FeepingCreature wrote:
> On Monday, 28 January 2019 at 15:16:54 UTC, Steven Schveighoffer wrote:
>> It will inspect the allocated length from the GC if the array is appendable from the beginning. So it's not always going to reallocate.
>>
>> e.g.:
>>
>> string x = "abc".idup;
>>
>> auto app = x.appender;
>>
>> app ~= "xyz"; // does not reallocate.
>>
> 
> Fair enough.

Wow, I take this back. I tried it, and it DOES reallocate.

In fact, appender when given data that's not mutable avoids the check for appendability, and extension into the block.

So indeed, using appender on a string that is appendable reallocates on first append. I agree with you now, Appender on an existing string is near useless.

Looks like there were some changes that fix one problem, but may have inadvertently created this problem. And I'm rereading my comments in this PR, finding that I was not exactly correct on my analysis: https://github.com/dlang/phobos/pull/2046

In short, there's a question of purity, which was already on the Appender's functions, causing issue with immutable data. I think this won't be strong pure anyway, because appender's functions are not strong-pure (there's always mutable data inside it).

I think we should avoid the mutability check in that call.

> My problem is this.
> 
> const and immutable are *not* well supported in the standard library at the moment. What I want is that I can use the idioms of the stdlib, semantically, in a way that lets me *not care* about const or immutable, that lets me express the patterns I want without having to worry about whether some type deep inside my code, a dub library, or phobos itself decided to declare a field immutable.
> 
> Appender covers two usecases: "capacity caching" and "memory reuse." Both of those usecases have two const variations: "possibly immutable" and "mutable".
> 
>    mutable capacity caching | mutable memory reuse
> ---------------------------+---------------------
> immutable capacity caching | immutable memory reuse
> 
> But instead of cutting between capacity caching and memory reuse, down the middle, Appender cuts between mutable and immutable, left to right. I think this is a symptom of a broad negligence of constness as a first-class property - constness works sometimes, and it's nice if it does, but it can't be *relied* on to be well supported. This makes using const in D an eternal uphill struggle. Why even go to all the trouble to make a new major version of the language just to introduce constness, if it's not going to be treated as a first-class concern? I don't want to have to sprinkle static if(isAssignable!T) everytime I want to use a library type. If Phobos offers me as generic a data structure as Appender, parameterized on T, it should work for T, regardless of what T is doing, and *certainly* regardless of what fields in T are marked const. Especially considering that if a field is not marked as const, that hardly means it's not going to lead to bugs if its memory is reused while you're referencing it!

Appender covers appending, which works for both mutable and immutable data. The clear function, if called on immutable data, would allow for immutable data to be overwritten, which is undefined behavior. This should be harder to do than just calling a member function.

Note that if we fix the above problem, there is a workaround:

app = app.data[0 .. 0].assumeSafeAppend.appender;

Which includes the greppable assumeSafeAppend property.

The biggest problem I see is not necessarily that immutable arrays should be clearable, but that arrays of types with const items should be clearable. I can't say I agree with immutable pieces, as again, that leads to potential UB. I know it's a PITA, but we have to consider as a library type all possible usages, not just focused ones.

I can see a PR that allows clear for types that are not assignable, but only have const data, being accepted. Const doesn't guarantee no mutability, so it's not UB to modify data that in some other alias has a const reference. I don't know if we have a way to determine this separate from immutability.

-Steve
1 2
Next ›   Last »