Object Creation Performance

Help requests on developing Logtalk applications

Moderator: Paulo Moura

Post Reply
prologer
Posts: 2
Joined: Mon Jul 25, 2011 4:40 pm

Object Creation Performance

Post by prologer » Wed Jul 27, 2011 10:39 pm

Hi,

I am testing Logtalk to see if it is suitable for a new project. The first thing I need to do is load a 50,000+ line xml file. I wrote a pure prolog version that models the data with plain old predicates. It is able to load the file in a reasonable 0.72 sec and uses 2,722,448 bytes of the heap. This is in SWI-Prolog.

It would be nice to use an object oriented approach, so I wrote a version that models the data using Logtalk objects. It loads the file in 26.87 sec and uses 34,399,392 bytes. This project will become part of a compiler toolchain, so keeping things to just few seconds is very desirable.

It seems that object creation is the slow part. I wrote a test of a simple object.

:- object(test).
:- public(new/1).
:- mode(new(-object_identifier), one).
new(Test) :-
self(Self),
create_object(Test, [extends(Self)], [], []).
:- end_object.

?- profile( forall(between(1,5000,_), test::new(_)) ).

The time it takes to create 5000 objects is 31.89 sec. Is there a faster way to create new objects in Logtalk, or should I stick to Prolog for this particular project.

Thanks,
Matt

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Object Creation Performance

Post by Paulo Moura » Wed Jul 27, 2011 11:32 pm

If your data (from the XML file) is static (or mostly static), a better approach could be to use parametric objects and parametric object proxies. This allows you the best of both worlds: very compact representation of data as plain Prolog predicates that are interpreted as proxies to parametric objects, which allows you to structure the predicates that handles the data. See the "proxies" example and/or provide us with more details for more specific advise.

Compiling a Logtalk source file with object definitions will always be slower than compiling a plain Prolog file. In a nutshell, the Logtalk compiler performs two passes on the source code and generates an intermediate Prolog file, which is then compiled by the back-end Prolog compiler that you're using. With dynamic objects created at runtime, there's no intermediate Prolog file but you will be dealing with dynamic code, which is always slower than static code.
Paulo Moura
Logtalk developer

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Object Creation Performance

Post by Paulo Moura » Wed Jul 27, 2011 11:41 pm

prologer wrote: I am testing Logtalk to see if it is suitable for a new project. The first thing I need to do is load a 50,000+ line xml file. I wrote a pure prolog version that models the data with plain old predicates. It is able to load the file in a reasonable 0.72 sec and uses 2,722,448 bytes of the heap. This is in SWI-Prolog.

It would be nice to use an object oriented approach, so I wrote a version that models the data using Logtalk objects. It loads the file in 26.87 sec and uses 34,399,392 bytes. This project will become part of a compiler toolchain, so keeping things to just few seconds is very desirable.
Btw, was the test above performed using the current development version (2.43.0) or the current stable version (2.42.4)? If the former, did you turned off the "source_data" flag before compiling the Logtalk file?

prologer
Posts: 2
Joined: Mon Jul 25, 2011 4:40 pm

Re: Object Creation Performance

Post by prologer » Thu Jul 28, 2011 2:30 am

Everything will be static once the file is loaded, so parametric objects look like an excellent option. I am using Logtalk 2.42.3 recently installed by macports.

Thanks,
Matt

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Object Creation Performance

Post by Paulo Moura » Thu Dec 29, 2011 8:17 pm

prologer wrote: It seems that object creation is the slow part. I wrote a test of a simple object.

:- object(test).
:- public(new/1).
:- mode(new(-object_identifier), one).
new(Test) :-
self(Self),
create_object(Test, [extends(Self)], [], []).
:- end_object.

?- profile( forall(between(1,5000,_), test::new(_)) ).

The time it takes to create 5000 objects is 31.89 sec. Is there a faster way to create new objects in Logtalk, or should I stick to Prolog for this particular project.
Actually, the time it takes to create the objects is better. By typing the above profile goal at the Prolog prompt, the ::/2 goal results in a compiler overhead that can be factored out by ensuring static binding for the new/1 message to the "test" object. See e.g. the "benchmarks" example in the current Logtalk distribution. It includes a benchmark for creating AND abolishing simple objects.
Paulo Moura
Logtalk developer

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests