KxSystems/embedPy

x=ja(&y,&z)

effbiae opened this issue · 4 comments

in PR "Use ja to null-terminate code strings. #11"

- ..x=K(",[;10h$0]",r1(x));..
+..C z=0;r1(y);x=ja(&y,&z);x==y?--xn:r0(y);..

reviewing api docs at http://code.kx.com/q/interfaces/capiref/#joins, x==y is always true after x=ja(&y,&z)

a solution
+Z K2(runs){P(xt!=-KJ||y->t!=KC,E(type))r1(y);ja(&y,""),--y->n;PyErr_Clear();O o=PyRun_String(xG,xj?Py_eval_input:Py_file_input,d,d);r0(y);R ko(o)?:PE;}

Hmm, I think you are right. I recall seeing some crashes when I first wrote the code the way you propose, but now it looks right to me. Maybe it was something else.

Note that if you are not renaming y to x, you should change xG to kG(y) and xj to y->j later in the line.

Nice trick using "" to create a pointer to a null character, BTW.

What do you think about

x=ja(&y,"");xr?--xn:0;

There is no need to modify xn if x is not shared.

i'm not sure that the complexity this optimization introduces is worth it. i'd tend toward using k() or doing something threadsafe to copy xC and append "\0" (if "\0" is not already there).

Speaking of thread safety, you probably know that Python API is not thread safe and you want to call it from multiple threads you have to acquire the Global Interpreter Lock (the infamous GIL).