Pass by address
- Posted by "J. Kenneth Riviere" <kriviere at MINDSPRING.COM> Aug 16, 2000
- 539 views
This is the first of a short series of msgs I intend to post to the mailing list hoping to generate some discussion about features I'd like to see added to Euphoria. The first item I'd like to see added is parameter passing by address for subroutines. Currently all subroutines accept passed parameters by value. Any changes made to the data passed as arguments are not reflected in the original variables unless the updated variables are returned from a function or assigned to some other global variable. Robert has noted in the documentation for Euphoria that in actuallity he has only passed a reference which points to the variables being passed into the subroutine so that there is very little overhead in subroutine calls. Furthermore, since the same logic should apply to returning results, it should be highly efficient to return updated variables. This is a good thing and I'm generally grateful that I don't have to worry about this. However, in those same notes Robert mentions that if a complex structure is updated which is currently pointed to by two (or more, presumably) variables, then a copy of that complex structure will be made. As I understand it, this copy would only be of the logical "top level" of the sequence structure, as the addresses of the complex sequences which comprise each element of the top-level sequence would still point to the individual instances of those sub-sequences and would be shared by both copies (the original structure and the new, updated copy). I've been working on a series of functions and procedures for manipulating key-value sequences (kvseq) which are designed to be as general purpose as I can make them. Each kvseq would consist of a sequence where each element of the sequence is a two element sequence, the first being the key and the second being the value. Each of these elements, the key and the value, can be any object in Euphoria. I use the compare function to keep them sorted and do binary searches of the structure so that I can check for the existence of a key, retrieve the value associated with a key, insert or update key/value pairs, append to the value associated with a key, prepend to the value associated with a key, or delete a k/v pair. Each function which modifies the kvseq is passed the kvseq which is to be updated and returns the updated version. The passing in of the argument and the return of the result are probably quite effecient operations per the earlier discussion. However, in the course of updating the sequence, especially when inserting or deleting a k/v pair, the code could wind up causing a complete copy of the kvseq's top level to be made. If these kvseq's grow to any significant length then this could be a significant overhead. If I could indicate that this parameter is being passed "by address" then the update process of the interpreter could recognize that there really is only one variable that identifies this sequence and thus it would not have to make a copy of that potentially large sequence. I would convert the current functions into procedures which would also save the (minimal) time of returning the updated value. To my mind this enhancement to the language would be similar to having the ability to use the "x+=1" assignment instead of having to use "x=x+1". This would only require a minimal adjustment to the syntax of declaring subroutines to allow parameters to have a property of "by address", how ever Robert wanted to implement it. I hope that this suggestion will generate some discussion of other enhancements Euphoria users might like to see. -J. Kenneth Riviere (JoKeR)