www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Cannot make my shared PureMallocator callable in pure functions

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
I'm struggling with making

https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d

callable in pure functions such as here

https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d#L84

Shouldn't a shared

     static shared PureMallocator instance;

make it possible to call

     PureMallocator.instance.allocate(16);

in pure functions?
Feb 17 2018
next sibling parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Saturday, 17 February 2018 at 12:33:25 UTC, Nordlöw wrote:
     PureMallocator.instance.allocate(16);
currently errors as pure_mallocator.d(84,16): Error: pure function 'pure_mallocator.__unittest_pure_mallocator_82_0' cannot access mutable static data 'instance'
Feb 17 2018
prev sibling next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 17/02/2018 12:33 PM, Nordlöw wrote:
 I'm struggling with making
 
 https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d
 
 callable in pure functions such as here
 
 https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d#L84 
 
 
 Shouldn't a shared
 
      static shared PureMallocator instance;
 
 make it possible to call
 
      PureMallocator.instance.allocate(16);
 
 in pure functions?
pure means no globals. As in none :)
Feb 17 2018
next sibling parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Saturday, 17 February 2018 at 12:35:00 UTC, rikki cattermole 
wrote:
 in pure functions?
pure means no globals. As in none :)
I don't understand. I thought std.experimental.allocators API was designed to be able express these needs, andralex?
Feb 17 2018
parent rikki cattermole <rikki cattermole.co.nz> writes:
On 17/02/2018 12:48 PM, Nordlöw wrote:
 On Saturday, 17 February 2018 at 12:35:00 UTC, rikki cattermole wrote:
 in pure functions?
pure means no globals. As in none :)
I don't understand. I thought std.experimental.allocators API was designed to be able express these needs, andralex?
Library code cannot change the language. pure functions cannot access globals period, it does not matter what you write. Its a hard limit. You simply don't want pure here.
Feb 17 2018
prev sibling next sibling parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Saturday, 17 February 2018 at 12:35:00 UTC, rikki cattermole 
wrote:
 in pure functions?
pure means no globals. As in none :)
I guess one solution is to make the member functions in PureMallocator static and change how the template argument `Allocator` for a container is used to call these member functions directly instead of via `instance`, right?
Feb 17 2018
prev sibling parent reply ag0aep6g <anonymous example.com> writes:
On 02/17/2018 01:35 PM, rikki cattermole wrote:
 pure means no globals. As in none :)
... except immutable ones. And since PureMallocator has no fields, `instance` can be made immutable, and all the methods can be made static or const. Then they can be used in `pure` code.
Feb 17 2018
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/17/18 8:32 AM, ag0aep6g wrote:
 On 02/17/2018 01:35 PM, rikki cattermole wrote:
 pure means no globals. As in none :)
... except immutable ones. And since PureMallocator has no fields, `instance` can be made immutable, and all the methods can be made static or const. Then they can be used in `pure` code.
You have to be a bit careful here. pure functions can assume nothing is happening and simply not call the function. -Steve
Feb 17 2018
parent reply ag0aep6g <anonymous example.com> writes:
On 02/17/2018 03:04 PM, Steven Schveighoffer wrote:
 You have to be a bit careful here. pure functions can assume nothing is 
 happening and simply not call the function.
That's only a problem when the called function is strongly pure, right? Nordlöw's methods are only weakly pure. They have mutable indirections either in the return type or in a parameter type. So calls to them should not be optimized away.
Feb 17 2018
next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/17/18 9:54 AM, ag0aep6g wrote:
 On 02/17/2018 03:04 PM, Steven Schveighoffer wrote:
 You have to be a bit careful here. pure functions can assume nothing 
 is happening and simply not call the function.
That's only a problem when the called function is strongly pure, right? Nordlöw's methods are only weakly pure. They have mutable indirections either in the return type or in a parameter type. So calls to them should not be optimized away.
That's fine as long as you aren't going to use the allocator to make any immutable or const data ;) -Steve
Feb 17 2018
prev sibling parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Saturday, 17 February 2018 at 14:54:37 UTC, ag0aep6g wrote:
 Nordlöw's methods are only weakly pure. They have mutable 
 indirections either in the return type or in a parameter type. 
 So calls to them should not be optimized away.
I found a solution at https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d currently successfully used at https://github.com/nordlow/phobos-next/blob/master/src/open_hashmap_or_hashset.d Works for me. Thanks!
Feb 17 2018
prev sibling next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/17/18 7:33 AM, Nordlöw wrote:
 I'm struggling with making
 
 https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d
 
 callable in pure functions such as here
 
 https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d#L84 
 
 
 Shouldn't a shared
 
      static shared PureMallocator instance;
 
 make it possible to call
 
      PureMallocator.instance.allocate(16);
 
 in pure functions?
Pure functions cannot access shared or thread-local data. They are not supposed to have any side effects. Keep in mind, allocators have side effects, we just pretend they don't. You need to fool the compiler into thinking you aren't doing anything to global data. The design of allocators makes this difficult. I suggested at one point in the past that such allocators make all functions static, which solves other problems. Oh, I even made a PR: https://github.com/dlang/phobos/pull/4288 But it wasn't to allow purity, it was to allow storage of an "instance" anywhere. -Steve
Feb 17 2018
prev sibling parent Eduard Staniloiu <edi33416 gmail.com> writes:
On Saturday, 17 February 2018 at 12:33:25 UTC, Nordlöw wrote:
 I'm struggling with making

 https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d

 callable in pure functions such as here

 https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d#L84

 Shouldn't a shared

     static shared PureMallocator instance;

 make it possible to call

     PureMallocator.instance.allocate(16);

 in pure functions?
As the folks before me have pointed out, the language doesn't allow you to use globals inside pure code, excepting global immutables; this makes sense as once an immutable object was constructed it will never change. As Steven pointed out, we are just trying to fool the compiler into thinking that allocators don't have side effects; in the case of Mallocator, we are just forwarding calls to libc's mallocator. With this in mind, it looks to me that you just need to decide what is the best/easiest way for you to forward the calls. You could: 1) make all you methods static (after all, the allocator is stateless) 2) make `instance` immutable and make all the methods const
Feb 17 2018