[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: numerical Lua?
- From: Diego Nehab <diego@...>
- Date: Sun, 7 May 2006 18:26:57 -0400 (EDT)
I always wanted to replace Matlab's scripting language with
Lua, but I have to admit it has some strenghts when it comes
Anyways, Matlab appart, it would be great to have some of
that functionality available to Lua scripts and I really
appreciate your effort. I thought about doing it myself, but
I never had the guts/time. :)
Yes, a new 100x100 matrix is being generated/collected at
each iteration. I also think that Matlab should be very
sensitive to l- and r-values and should feature a very
specialized parser, as you suggested. I have thought about
this memory reallocation issue before, and took the
shortest lazy path of assuming the user would be smart
enough to find this bottleneck in his code and transfer it
to C using the API, that is, I didn't want to hack the
parser at that time (and still don't) :).
A not-so-lazy approach would be to provide an "in-place"
optional parameter to most of the functions in the
library. I'm postponing this kind of change until a have a
more stable version of NumericLua and more suggestions :).
But you're right: there's a great overhead due to memory
allocation, copying, and collection.
I didn't mean you should hack the parser. I was just giving
my opinion on why the code is slower than Matlab.
Yes, one approach would be to provide functions such as
and so on, that operated in-place. Although I hate the
syntax, it seems like this could be offered as an
alternative to the "performance critical" applications.
Matrices are treated as similar to tables as possible, and
so y would be a reference to x in your example. If the
user wants a copy, that should be explicitly stated with y
= x:copy() or y = x() (matrix calls are treated as
slicing; more on that below).
What do you mean by copy-on-write? Making a copy of x only
when y is written? Wouldn't it be better to just let the
user decide if he wants to copy or just reference? Isn't
worse to copy when not needed?
I was talking about replicating the semantics of Matlab in
Lua. I don't know if you care about it or not. But I am sure
users will compare the two things.
This isn't possible with the current Lua API. Variables can
only hold references to userdata. There is no way to tell
how many references exist to a given matrix. There is not
even a way for you to always copy on assignments, if you
wanted to be conservative.
On the other hand, take for example code like this
m = ones(100, 100)
It doesn't make sense to allocate a 100x100 matrix since you
know all its values are 1s. So you could instead create an
implicit matrix, that always returns 1s.
If the user later does something like
m = 3
you could then explode the matrix. This is one flavour of
lazy evaluation. You could also play tricks with an
associated table that could hold the changes, until there
were so many changes that it was better to create a
Another example is the (in my opinion wonderful) matlab
syntax for "ranges".
m(1:2, 1:2:10) = m(4:5, 21:2:30)
This is not valid syntax in Lua for several reasons. Not
only because of the ':'. A function call can't be an
l-value, and there is no "assignment" metamethod.
It would be very nice if you could provide a similar
operation in NumericLua. I am not sure what it would look
like, but this is one of the strenghts of Matlab. :/
Regardless of the syntax, all these "ranges" could be
represented implicitly. That would be much more efficient
than allocating memory for all of them.