Memory management (continued :)

Suppose one uses ATS in a purely functional way, to write library functions
or language extensions for another language. If I call an ATS function and
eventually it returns – all side effects are done through CFFI so that no
ATS data structures are needed in the end – then I should be able to call
free() from the caller to clean up ATS heap, correct? Assuming a) the
returned result is a value not a pointer b) I write a heap allocator for
this use. This is very much like a thread execution model where a short
lived thread does not do any GC but let the runtime/OS clean up after it
exits (a popular technique for Erlang?) and side effects are like modifying
the shared memory. And this would require no modification to ATS I think.

Moving ahead one step, it would then be nice to distinguish linear and
non-linear allocations so we can have two heaps, one for the linear
structures and one for the nonlinear ones. Before type erasure ATS has
information on what allocations are linear, correct? Is there also a way to
guarantee that linear structures don’t contain pointers to the nonlinear
heap? This way we can also use linear types to return structures without
copying and/or store side effects, safer than using only CFFI? Not sure how
useful or feasible this is.

Moving ahead one more step we could also provide a frame count to the
allocator so we can manage the heap like a stack which allows us to
interleave calls between ATS and the other language – but that is probably
an unnecessary complication.

Haitao

Linear values are often not freed; instead, they turn into non-linear
values.On Thursday, March 13, 2014 10:51:32 PM UTC-4, H Zhang wrote:

Suppose one uses ATS in a purely functional way, to write library
functions or language extensions for another language. If I call an ATS
function and eventually it returns – all side effects are done through
CFFI so that no ATS data structures are needed in the end – then I should
be able to call free() from the caller to clean up ATS heap, correct?
Assuming a) the returned result is a value not a pointer b) I write a heap
allocator for this use. This is very much like a thread execution model
where a short lived thread does not do any GC but let the runtime/OS clean
up after it exits (a popular technique for Erlang?) and side effects are
like modifying the shared memory. And this would require no modification to
ATS I think.

Moving ahead one step, it would then be nice to distinguish linear and
non-linear allocations so we can have two heaps, one for the linear
structures and one for the nonlinear ones. Before type erasure ATS has
information on what allocations are linear, correct? Is there also a way to
guarantee that linear structures don’t contain pointers to the nonlinear
heap? This way we can also use linear types to return structures without
copying and/or store side effects, safer than using only CFFI? Not sure how
useful or feasible this is.

Moving ahead one more step we could also provide a frame count to the
allocator so we can manage the heap like a stack which allows us to
interleave calls between ATS and the other language – but that is probably
an unnecessary complication.

Haitao

My impression is that ATS does a good job of not gratuitously heap
allocating structures as a naive ML implementation would.

Using flat data (e.g. @(1, 2)) instead of boxed data (e,g, '(1, 2)) can
eliminate a lot of heap-allocation. Also, using call-by-reference
can eliminate a lot of needs for copying flat data.On Friday, March 14, 2014 1:11:11 AM UTC-4, H Zhang wrote:

I see. The first model, which is the “thread”/separate heap model, should
still work so long as ATS heap does not grow too much. My impression is
that ATS does a good job of not gratuitously heap allocating structures as
a naive ML implementation would. If this works it is actually a good model
with likely better performance (trade-off is some latency in reclaiming
memory, which is not important in my use cases). And as you said before one
can always use linear types if necessary.

I will be looking for a library for management of multiple heaps.

Haitao

On Thursday, March 13, 2014 8:24:54 PM UTC-7, gmhwxi wrote:

Linear values are often not freed; instead, they turn into non-linear
values.

On Thursday, March 13, 2014 10:51:32 PM UTC-4, H Zhang wrote:

Suppose one uses ATS in a purely functional way, to write library
functions or language extensions for another language. If I call an ATS
function and eventually it returns – all side effects are done through
CFFI so that no ATS data structures are needed in the end – then I should
be able to call free() from the caller to clean up ATS heap, correct?
Assuming a) the returned result is a value not a pointer b) I write a heap
allocator for this use. This is very much like a thread execution model
where a short lived thread does not do any GC but let the runtime/OS clean
up after it exits (a popular technique for Erlang?) and side effects are
like modifying the shared memory. And this would require no modification to
ATS I think.

Moving ahead one step, it would then be nice to distinguish linear and
non-linear allocations so we can have two heaps, one for the linear
structures and one for the nonlinear ones. Before type erasure ATS has
information on what allocations are linear, correct? Is there also a way to
guarantee that linear structures don’t contain pointers to the nonlinear
heap? This way we can also use linear types to return structures without
copying and/or store side effects, safer than using only CFFI? Not sure how
useful or feasible this is.

Moving ahead one more step we could also provide a frame count to the
allocator so we can manage the heap like a stack which allows us to
interleave calls between ATS and the other language – but that is probably
an unnecessary complication.

Haitao

Update on how to manage multiple heaps:
If you use Windows you can create your own heap by using HeapCreate, which
returns a handle that you can then pass on to HeapAlloc. At the end you can
call HeapDestroy to remove the heap so you don’t have to track the free
list. So this is as simple as it gets in the usage model described below.
It is probably too bothersome to thread the heap handle to all the
malloc/frees called by ATS so one can use a (thread) static variable for
the handle. You set it using a setter function after you call HeapCreate
and you write your own malloc/free functions that call HeapAlloc/HeapFree
on the designated heap.

HaitaoOn Thursday, March 13, 2014 10:11:11 PM UTC-7, H Zhang wrote:

I see. The first model, which is the “thread”/separate heap model, should
still work so long as ATS heap does not grow too much. My impression is
that ATS does a good job of not gratuitously heap allocating structures as
a naive ML implementation would. If this works it is actually a good model
with likely better performance (trade-off is some latency in reclaiming
memory, which is not important in my use cases). And as you said before one
can always use linear types if necessary.

I will be looking for a library for management of multiple heaps.

Haitao

On Thursday, March 13, 2014 8:24:54 PM UTC-7, gmhwxi wrote:

Linear values are often not freed; instead, they turn into non-linear
values.

On Thursday, March 13, 2014 10:51:32 PM UTC-4, H Zhang wrote:

Suppose one uses ATS in a purely functional way, to write library
functions or language extensions for another language. If I call an ATS
function and eventually it returns – all side effects are done through
CFFI so that no ATS data structures are needed in the end – then I should
be able to call free() from the caller to clean up ATS heap, correct?
Assuming a) the returned result is a value not a pointer b) I write a heap
allocator for this use. This is very much like a thread execution model
where a short lived thread does not do any GC but let the runtime/OS clean
up after it exits (a popular technique for Erlang?) and side effects are
like modifying the shared memory. And this would require no modification to
ATS I think.

Moving ahead one step, it would then be nice to distinguish linear and
non-linear allocations so we can have two heaps, one for the linear
structures and one for the nonlinear ones. Before type erasure ATS has
information on what allocations are linear, correct? Is there also a way to
guarantee that linear structures don’t contain pointers to the nonlinear
heap? This way we can also use linear types to return structures without
copying and/or store side effects, safer than using only CFFI? Not sure how
useful or feasible this is.

Moving ahead one more step we could also provide a frame count to the
allocator so we can manage the heap like a stack which allows us to
interleave calls between ATS and the other language – but that is probably
an unnecessary complication.

Haitao

I see. The first model, which is the “thread”/separate heap model, should
still work so long as ATS heap does not grow too much. My impression is
that ATS does a good job of not gratuitously heap allocating structures as
a naive ML implementation would. If this works it is actually a good model
with likely better performance (trade-off is some latency in reclaiming
memory, which is not important in my use cases). And as you said before one
can always use linear types if necessary.

I will be looking for a library for management of multiple heaps.

HaitaoOn Thursday, March 13, 2014 8:24:54 PM UTC-7, gmhwxi wrote:

Linear values are often not freed; instead, they turn into non-linear
values.

On Thursday, March 13, 2014 10:51:32 PM UTC-4, H Zhang wrote:

Suppose one uses ATS in a purely functional way, to write library
functions or language extensions for another language. If I call an ATS
function and eventually it returns – all side effects are done through
CFFI so that no ATS data structures are needed in the end – then I should
be able to call free() from the caller to clean up ATS heap, correct?
Assuming a) the returned result is a value not a pointer b) I write a heap
allocator for this use. This is very much like a thread execution model
where a short lived thread does not do any GC but let the runtime/OS clean
up after it exits (a popular technique for Erlang?) and side effects are
like modifying the shared memory. And this would require no modification to
ATS I think.

Moving ahead one step, it would then be nice to distinguish linear and
non-linear allocations so we can have two heaps, one for the linear
structures and one for the nonlinear ones. Before type erasure ATS has
information on what allocations are linear, correct? Is there also a way to
guarantee that linear structures don’t contain pointers to the nonlinear
heap? This way we can also use linear types to return structures without
copying and/or store side effects, safer than using only CFFI? Not sure how
useful or feasible this is.

Moving ahead one more step we could also provide a frame count to the
allocator so we can manage the heap like a stack which allows us to
interleave calls between ATS and the other language – but that is probably
an unnecessary complication.

Haitao