Partial evaluation of format_//2 and related constructs #2364
triska
started this conversation in
Show and tell
Replies: 1 comment 3 replies
-
Upgrading from 0292cb7 to 2fdbb94, I see actually a little (~3%) slowdown, from 17.5 to 18.1 seconds, in the precautionary package code that writes the ?- [scryer]. % using 0292cb7
% Warning: overwriting nth1/3 because the clauses are discontiguous
true.
?- [esc].
true.
?- time(maplist(write_T, [2,3,4,5,6,7,8])).
D = 2 J = 46
D = 3 J = 154
D = 4 J = 442
D = 5 J = 1162
D = 6 J = 2890
D = 7 J = 6922
D = 8 J = 16138
% CPU time: 17.533s, 90_876_135 inferences
true.
?- time(maplist(write_T, [2,3,4,5,6,7,8])).
D = 2 J = 46
D = 3 J = 154
D = 4 J = 442
D = 5 J = 1162
D = 6 J = 2890
D = 7 J = 6922
D = 8 J = 16138
% CPU time: 17.420s, 90_876_158 inferences
true.
?- halt.
% ... upgraded to 2fdbb94
?- [scryer].
% Warning: overwriting nth1/3 because the clauses are discontiguous
true.
?- [esc].
true.
?- time(maplist(write_T, [2,3,4,5,6,7,8])).
D = 2 J = 46
D = 3 J = 154
D = 4 J = 442
D = 5 J = 1162
D = 6 J = 2890
D = 7 J = 6922
D = 8 J = 16138
% CPU time: 18.126s, 95_669_949 inferences
true.
?- time(maplist(write_T, [2,3,4,5,6,7,8])).
D = 2 J = 46
D = 3 J = 154
D = 4 J = 442
D = 5 J = 1162
D = 6 J = 2890
D = 7 J = 6922
D = 8 J = 16138
% CPU time: 18.055s, 95_669_972 inferences
true.
?- |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Dear all,
while trying to improve the speed of
format_//2
for cases such as #2360, it occurred to me that the performance offormat_//2
,format/2
and related predicates can be improved with a technique known as partial evaluation: This means that we take into account everything we already know about these goals at compilation time, and only do what is still necessary at run time.In almost all practical usage scenarios of
format_//2
, the format string (i.e., the first argument) is ground and can thus be compiled into dedicated instructions at compilation time. In this way, it is no longer necessary to parse the format string at run time.I have now done this, and filed it as a draft in #2363.
Good news everyone!
With these changes applied, the example shown in #2360 runs more than twice as fast. In other programs, I measured a speedup of a factor 3 or greater. For example, the
chacha20.pl
implementation now produces its output much faster.To illustrate the effect of these changes, let's consider
word_hex/2
from that program:Note that the format string is fully known at compilation time, a very typical scenario.
With the partial evaluation changes to
library(format)
applied, the above clause automatically becomes:Note how the format string has completely disappeared from the program! Instead, dedicated "instructions" appear that can be processed much faster, by relying on argument indexing and meta-calls.
I would greatly appreciate any feedback and tests, in particular if you have programs that describe a lot of output. For example, I think you @dcnorris described a larger CSV file at one point? If possible, please try these changes and see whether they work and yield performance improvements for your use cases.
Thank you a lot, and enjoy!
Markus
Beta Was this translation helpful? Give feedback.
All reactions