Hi,
Having realised the benefits of functional programming, I’ve been quite annoyed by the rumour of how expensive function calls are in Pike. I decided to look into f_map and could see how much seemingly unnecessary work it does when it calls apply_svalue once for each entry in the array – it should be possible to reuse the pike_frame if it’s about to be thrown away after the function call (if it has other refs on the other hand, it can’t be reused – it’s probably used as a scope in some …
[View More]other frame).
I’ve pushed my optimised variant in marty/optimised_map – it seems to work quite well and provides a major speedup. In fact, it’s a bit faster than the corresponding foreach variant. I haven’t verified correctness in various corner cases, and some input on whether it’s correct to do the things init_frame_reuse_context does only once before multiple function calls would be nice too. The *_reuse_context stuff in interpret.c should be applicable wherever the same svalue is applied repeatedly with the same number of arguments (I haven’t looked for it outside of f_map really).
What do you all think? Good idea or did I overlook something?
Without optimisation:
map: 1.660797
array index: 1.335115
array append: 1.17917
With optimisation:
map: 0.877659
array index: 1.351158
array append: 1.189812
Test program:
int main()
{
array base = allocate(10000000, 1);
float gmap = gauge {
array res = map (base, lambda(int i) { return i + 2; });
};
float garrayindex = gauge {
array res = allocate(sizeof(base));
foreach (base; int idx; int i) {
res[i] = i + 2;
}
};
float garrayappend = gauge {
array res = ({});
foreach (base, int i) {
res += ({ i + 2 });
}
};
werror ("map: %O\n", gmap);
werror ("array index: %O\n", garrayindex);
werror ("array append: %O\n", garrayappend);
}
/Marty
[View Less]
I have spent some time looking at the new stuff in Concurrent.pmod
that Grubba has added to 8.1. Mostly it is code relating to the
concept of Promises and Futures which are very popular amongst
JavaScript people these days.
I have noted that there seems to be some rather striking differences
between how JavaScript treats promises and how Pike does it. I'm not
sure this is a good thing(tm) and therefore I'd like to bring this up
before 8.1 stabilizes.
In Pike, we have two methods that register …
[View More]callback functions,
Future.on_success() and Future.on_failure() as opposed to JS where
only one method, then(), is used to register both callbacks. I don't
mind having two different methods, but for easier transition between
the two languages (i.e. for people writing both client-side and
server-side code) having the same API would probably help.
A bigger problem imho is the difference in how promises are actually
resolved. In JavaScript, the return value of then() is always a *new*
promise which then allows for chaining. In Pike, we return the same
promise object. This means that code like
my_promise->on_success(foo)->on_success(bar)
in Pike would result only in a call to bar() once my_promise is
resolved whereas in JavaScript, foo() would be called and it's return
value would be the input to bar in a new promise.
The whole creation of promises is also somewhat awkward in Pike. In
JS, you simply pass a callback that will do all the work and that will
recieve a success callback and a failure callback as arguments and off
you go. In Pike, I'm not sure what the best way to do it is.
I really like the idea of having promises in Pike, but would it not be
much better if we tried to implement an API more similar to what the
JS folks are used to? At least I think so...
Here are three links to good resources I've found regarding promises
in JS. I'm sure there are a ton of others as well.
https://promisesaplus.com/http://www.mattgreer.org/articles/promises-in-wicked-detail/http://bluebirdjs.com/docs/api-reference.html
[View Less]
Something I would recommend, from having written heaps of asynchronous
object based systems, with or without remote computers involved, is to
treat all references to the promise object going away as an error, and
avoid un-needed circular references.
This makes it easy to detect when you accidentally forget the object.
Also, having the object be callable is rather convenient, it can then
be put as a normal callback in all the various function based API:s we
already have.
>From the MiniRPC …
[View More]module, used extensively in the mini codebase:
// This is an object representing a function to be called when
// whatever task is being requested has been completed.
//
// In the RPC case the 'call_id' and 'par' are used to send the return
// value, and function_name is for debug purposes.
//
class ReturnFromCall(protected int call_id,
protected object par,
protected string function_name)
{
protected void destroy()
{
// this should check the done function callback instead..
if( call_id )
{
werror("Return was never called for "+function_name+"\n");
if( par )
this.error("Return was never called\n");
}
}
protected string _sprintf( int flag, mapping opts )
{
// It's rather nice to have a _sprintf for backtraces. But
// without a function_name it's harder :)
return sprintf("ReturnCall(from %O)", function_name );
}
protected void error(string err)
{
// .. this is sort of special in the RPC code ..
call_id = 0;
destruct(this);
}
void `()(mixed ... x )
{
// Wanted addition #2: Callable promises?
call_id = 0;
destruct(this);
}
mixed try( function x )
//! Call @[x], if the call fails report an error message to
//! the remote side.
//
// syntax of the day:
// foo( foo->try() { code to run..; return res;});
//
// It might be useful to have a functon that also returns the
// value calculated
{
if( mixed err = catch{return x();})
{
// code goes here to log etc..
this.error(err);
}
}
}
Also, I personally belong to the "future/promises don't really add much"
camp, but feel free to add the API. :)
(In my opinion it's just another way to complicate asynchronous
programming)
[View Less]
On Tue, Mar 10, 2015 at 10:05 PM, Mirar @ Pike developers forum
<10353(a)lyskom.lysator.liu.se> wrote:
> Wait, I said REUSEPORT? What's the difference to REUSEADDR? Mysteries
> of TCP sockets...
Here's a decent explanation, I think:
http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and…
ChrisA