[fpc-other] PROLOG written in Pascal

Mark Morgan Lloyd markMLl.fpc-other at telemetry.co.uk
Sun Sep 21 13:57:40 CEST 2014


Marco van de Voort wrote:

>>> Basically you only need an own heapmanager that allocates all allocations
>>> in-order from a 16MB block, and mark and release procedures that call that
>>> heapmanager and are declared in some unit preloaded wiht -Fa?
>> The main issue is that dynamic memory is used by the Prolog 
>> implementation in several different ways. The first way is that as rules 
>> are being entered, they go into memory and usually stay there. The 
>> second way is that as queries are evaluated a lot of temporary stuff 
>> (variable bindings etc.) is put into memory, this is all thrown away on 
>> completion. The third way is that during query evaluation, rules could 
>> potentially be added/deleted/changed which shouldn't be thrown away.
> 
> How does this work in TP? Mark/release would remove all variables, not just
> the temprary ones. Or are the persistent and temporary orders not mixed, and
> is only the temporary part under mark/release?

In the current code, it doesn't attempt to change the rules. So 
according to discussion with the author a few years ago, provided that 
there was enough memory to evaluate the query everything could be thrown 
away once the results were returned. However it's been written with 
garbage collection which kicks in when available heap drops below a 
certain amount (this in itself is problematic on the general case), and 
which is intimately aware of TP's heap structure.

> To work around it, in theory one could allocate a new heap on every mark (if free mem< a
> certain threshold), and maintain a linked list of blocks that are scanned
> through for mark/release operations, but that is (1) costly, so you would
> need to pace your mark/releases (2) you still need an upper limit.
>  
> If address space is large enough (read: 64-bit) you might only reserve
> relatively large blocks, regardless of much you use.

In practical terms, having a private heap for evaluations would probably 
be OK. Or redoing the entire thing as OO, in which case it becomes 
comparatively easy to work out what needs to be thrown away on 
completion: AIUI you're walking a tree and also growing/shrinking an 
environment of temporary bindings as you move away and towards the root.

> My point is more that if the prolog interpreter can move its allocations so
> that the program remains working
> 
> IOW if I do
> 
> mark
>   new(persist);
>   new (temp);
> <---
> release
> 
> can I run a routine at the <--- point that moves all persistent allocations
> from the block of mark..release to a new or existing,  persistent heap?

Yes, I think the current code could be fairly easily modified for that 
sort of thing.

> Does it keep track of what is temp and what is persistent?

The current code doesn't, it just GCs the whole thing.

-- 
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]


More information about the fpc-other mailing list