trealla-prolog / trealla

A compact, efficient Prolog interpreter written in plain-old C.
MIT License
252 stars 11 forks source link

`phrase_to_stream/2` (consequently: `phrase_to_file/2`) total memory consumption with a mega-string #525

Closed haijinSk closed 2 months ago

haijinSk commented 2 months ago

Using v2.50.26, for the record; not saying that my mega-long string (cca 2_000_000 and more chars) example ever worked on my computer in Trealla.

With dcgs and pio library loaded, for example this:

length(L, 3_000_000), maplist(=(a), L), phrase_to_stream(seq(L), user_output).

will, slowly, but gigabyte by gigabyte, eat all the RAM, and if I will be lucky, so to speak, my OS will kill the process with the message: Killed. The same with phrase_to_file/2.

My 16 GB RAM computer can handle the phrase_to_stream (and phrase_to_file) example with a string/list 1_000_000 chars long without the freezing/crashing, but it's the quantitative, not qualitative difference, so to speak.


Yes, besides simply using format/2 and the Linux redirection to a file, I can save the 3-mega-string constructed using DCGs/in the "list-way", without (from a naive user's point of view) the unexpected "RAM aggressivity":

length(L,3_000_000),
maplist(=(a),L),
open("long-string.txt",write,Stream),
format(Stream,"~s~n",[L]),
close(Stream).

Or, accepting the list vs string dichotomy, I can use Trealla's savefile/2:

length(L,3_000_000),
maplist(=(a),L),
with_output_to(string(Str), format("~s~n",[L])),
savefile("long-string.txt",Str).

Thank you.

haijinSk commented 2 months ago

Now, I would say, the qualitative difference. A quantum leap towards the pio library as I use it, as I use everything in Prolog; as a naive user. Now I can handle even several-mega-long-string-list with the pio library in this context and no crashing/freezing, no the slow and total memory consumption, at least on my computer.

So I think, I have to close this issue. Thank you!!!