Hi, I need some help/advice for a plugin I'm writing. I'm already downloaded VMMaker and managed to compile a new VM in OS/X, and I already write and compile and test a simple plugin (and I have to say the process is very simple, and all my problems were because I never used Xcode before), but now, with the plugin running, comes the "optimization" phase, and there I have many doubts:
1) An external plugin blocks the VM when primitive is called? if so, how do I manage to avoid this? 2) what is the right way to use strings? now, I'm obtaining them as parameters, but the string comes without final zero, so C does not works fine... because of that, I'm copying them into a new variable (memory allocated with malloc and freed after use), but it does not seems to be very performant... is there a better way? 3) in general, what is the best way to return Strings into Squeak? I have to create an instance with interpreterProxy>>instantiateClass: each time?
Well, I have many doubts about how to optimize my code... where can I look to see some example code?
Thanks, Esteban
2008/9/14 Esteban Lorenzano estebanlm@gmail.com:
Hi, I need some help/advice for a plugin I'm writing. I'm already downloaded VMMaker and managed to compile a new VM in OS/X, and I already write and compile and test a simple plugin (and I have to say the process is very simple, and all my problems were because I never used Xcode before), but now, with the plugin running, comes the "optimization" phase, and there I have many doubts:
- An external plugin blocks the VM when primitive is called? if so, how do
I manage to avoid this?
No way. Primitive implements the behavior of your method. You can't run interpreter before it will return. Of course you can use pthreads: create own native thread and pass all you need to this thread using own synchronization scheme. Then you can avoid blocking. But this comes handy if your task is parallelisable. Some are simply can't be parallelised.
- what is the right way to use strings? now, I'm obtaining them as
parameters, but the string comes without final zero, so C does not works fine... because of that, I'm copying them into a new variable (memory allocated with malloc and freed after use), but it does not seems to be very performant... is there a better way?
you have to copy string into buffer and terminate it with 0 character.
- in general, what is the best way to return Strings into Squeak? I have to
create an instance with interpreterProxy>>instantiateClass: each time?
yes
Well, I have many doubts about how to optimize my code... where can I look to see some example code?
you can look at plugins code, which you already having in VM.
Thanks, Esteban
Igor Stasenko wrote:
2008/9/14 Esteban Lorenzano estebanlm@gmail.com:
Hi, I need some help/advice for a plugin I'm writing. I'm already downloaded VMMaker and managed to compile a new VM in OS/X, and I already write and compile and test a simple plugin (and I have to say the process is very simple, and all my problems were because I never used Xcode before), but now, with the plugin running, comes the "optimization" phase, and there I have many doubts:
- An external plugin blocks the VM when primitive is called? if so, how do
I manage to avoid this?
No way. Primitive implements the behavior of your method. You can't run interpreter before it will return. Of course you can use pthreads: create own native thread and pass all you need to this thread using own synchronization scheme. Then you can avoid blocking. But this comes handy if your task is parallelisable. Some are simply can't be parallelised.
To elaborate on Igor's response, Squeak has a way to register a Semaphore object with the VM. If you want native code to work asynchronously in another thread, you would write a primitive that would queue the task up for the worker thread, then returns immediately (so that bytecode interpretation can resume). Then, when your worker thread is finished, it signals the semaphore. Typically, you will have a Squeak Process looping and blocking on that semaphore; when the semaphore is signalled you know that there is data available to read back from the plugin (you would use a different primitive to read the data back).
As Igor says, you will often just want to wait for the primitive to return. A good rule-of-thumb is to ask yourself, if your whole program was written in C, would this be a place that you would use a separate worker thread. If not, then you probably don't want to do it for a Squeak primitive either.
Cheers, Josh
- what is the right way to use strings? now, I'm obtaining them as
parameters, but the string comes without final zero, so C does not works fine... because of that, I'm copying them into a new variable (memory allocated with malloc and freed after use), but it does not seems to be very performant... is there a better way?
you have to copy string into buffer and terminate it with 0 character.
Yes.
But if possible, avoid using malloc() and free(). Instead, use the memory in Squeak's object memory. That way you can avoid fragmentation. Just right before the call to primitive (or in the primitive), copy the content of the orignal string into another String whose size is one bigger than original (by #replaceFrom:to:with:), and do basicAt: 0 at the end. You could possibly keep the buffer around and reuse it, as long as the size of the String fits in the buffer.
- in general, what is the best way to return Strings into Squeak? I have to
create an instance with interpreterProxy>>instantiateClass: each time?
yes
If you know the size of string that is returned, again you can pass an instance of String from image to the primitive and fill it in. Primitive code looks cleaner in that way.
Well, I have many doubts about how to optimize my code... where can I look to see some example code?
you can look at plugins code, which you already having in VM.
Or, you can just show the code to us!
-- Yoshiki
vm-dev@lists.squeakfoundation.org