Re: st: Mata and memory

Ben Jann <ben.jann@soz.gess.ethz.ch> reports,
> I have a problem with Mata that confuses me. Mata sometimes
> issues an error "cannot create <istmt1>(): insufficient memory"
> although I am not doing anything that really uses a lot of
> memory. Here's an example:
>
> . local list
> . forv i=1/10000 {
> 2. local list "`list' hellobello"
> 3. }
> . something `list'
> cannot create <istmt1>(): insufficient memory
> r(3000);
>
> [where]
>
> === something.ado ===
> program define something, rclass
> version 9.0
> syntax anything
> mata: something(`"`anything'"')
> return local list `"`r(anything)'"'
> end
> version 9.0
> mata:
> function something(string scalar anything)
> {
> st_global("r(anything)", anything)
> }
> end
> ====================
Ben notes that he is using Stata/SE 9.0 and that the string `list'
containing 10000 times "hellobello" is around 110,000 characters long.
I have recreated the problem. The problem Ben describes does not occur
when the length of `list' is reduced.
Source of problem
-----------------
A limit was accidentally imposed in the SE-edition of the Mata code: when
passing strings from Stata to Mata as literals, the strings may not exceed
67,784 characters (which is the maximum length of a macro in Stata non-SE.)
To be clear: strings in Mata can exceed 67,784 (whether using SE or not).
The bug is in the passing of strings from Stata to Mata, and then, only when
passed as literals.
Until we can fix the problem, if Ben needs to pass strings longer than 67,784,
he is going to have to fetch them from the macro. I.e., rather than coding
. mata: something(`"`anything'"')
Ben needs to code
. mata: something()
and then code the Mata routine to include the line
anything = st_local("anything")
For example, I changed Ben's something.ado file to read:
-----------------------------------------------------------
program define something, rclass
version 9.0
syntax anything
mata: something()
return local list `"`r(anything)'"'
end
version 9.0
mata:
function something()
{
st_global("r(anything)", st_local("anything"))
}
end
-----------------------------------------------------------
-- Bill
wgould@stata.com
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/