KxSystems / embedPy

Allows the kdb+ interpreter to call Python functions
https://code.kx.com/q/interfaces
Apache License 2.0
87 stars 42 forks source link

x=ja(&y,&z) #24

Closed effbiae closed 5 years ago

effbiae commented 6 years ago

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;}

abalkin commented 6 years ago

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.

abalkin commented 6 years ago

What do you think about

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

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

effbiae commented 6 years ago

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).

abalkin commented 6 years ago

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).