Jump to content

Returning @error from internal function calls


 Share

Recommended Posts

Okay, after getting smacked around by Valik in Feature-Requests :P, I found out that here's the place to post to find out the information I'd like to know.

Basically - I was surprised when 'Return _myfunc()' wouldn't return the @error & @extended codes, which is the point of some of my wrapper functions. So I had to come up with a workaround

I've used something like this to workaround it:

$tempVal=_MyFunction()
If @error Then SetError(@error)
Return $tempVal

and Valik suggested something like this instead:

$tempVal=_MyFunction()
Return SetError(@error,@extended,$tempVal)

However - I was assuming from my other experience with languages that there would be an excessive amount of moving around of storage - especially if there were big variables (Gigantic arrays, or Strings, or DLLStructs, etc)

I was told by Valik that arrays use a 'copy-on-write' mechanism for arrays, but I'm not familiar with the concept - does that mean that the array isn't actually 'copied' until the variable is used in some manner? Would that mean its more of a 'reference' until used?

Also - what about the other things mentioned - strings, DLLStructs - are they too subject to a similar 'copy-on-write' mechanism?

And would passing the parameter to SetError() as Valik suggested not 'copy' (definition here to be determined!) the variable again?

I've always found using a direct 'Return FunctionName()' in other languages to be the most efficient, and doing the above alternatives would cause unnecessary hits in performance (especially when using large objects).

If any dev's could shed some light on this, it would be much appreciated.

Thank you!

Ascend4nt

Link to comment
Share on other sites

I don't get why you have to ask this. I told you that all variables use copy-on-write. I don't know what's not clear about that given that everything is stored in a variable. As for what copy-on-write is, well, I bet Wikipedia - or Google - know.

Link to comment
Share on other sites

Okay, you're right - I misread what you wrote in the Feature Requests response - I only got the part where you said arrays were copy-on-write, missed the 2nd half of that statement. my bad. And honestly I thought copy-on-write was a term coined by you or the programmers, that's why I hadn't thought to look for it.

For anyone reading this that doesn't know, Valik is right: Wikipedia has an article on Copy-on-write.

Thanks Valik!

Link to comment
Share on other sites

Okay, so I decided I might as well write a quick test to convince myself.

Below is code using multiple versions of 'Return' using a large variable returned from a Test function. It is meant to test the difference between Returning a variable directly, indirectly (storing it in a temporary variable), using SetError() before/at Return (and if *at* Return, passing SetError() the large variable), and testing whether the reported copy-on-write functionality makes a difference.

As seen from the below results, the reported 'copy-on-write' functionality doesn't really seem to be working correctly? This would explain why it is comparable in speed to a function that actually modifies the array before returning it. Anyway, I'd be interested if others get the same results - or maybe have suggestions on how to run this test better.

The way it appears now, though, is that direct Returns are faster and it would be nice to have a 'ReturnWithSubError' type statement :D

Test Results:
_Test() by itself time elapsed:          3421 ms
_TestWrapDirectRet() time elapsed:        3512 ms
_TestWrapDirectSetErrAtRet() time elapsed:  3506 ms
_TestWrapIndirectRet() time elapsed:        3559 ms
_TestWrapIndirectSetErrAtRet() time elapsed:3566 ms
_TestWrapModifyRet() time elapsed:        3568 ms
_TestWrapModifySetErrAtRet() time elapsed:  3586 ms

Here's the code:

; -------------------- FUNCTIONS ---------------------

Func _Test()
    Local $aArray[50000]
    ; Random Assignments
    $aArray[9998]=5
    $aArray[4]=3
    SetError(1,1)
    Return $aArray
EndFunc

Func _TestWrapDirectRet()
    SetError(@error,@extended)
    Return _Test()
EndFunc

Func _TestWrapDirectSetErrAtRet()
    Return SetError(@error,@extended,_Test())
EndFunc

Func _TestWrapIndirectRet()
    Local $aArray=_Test()
    SetError(@error,@extended)
    Return $aArray
EndFunc

Func _TestWrapIndirectSetErrAtRet()
    Local $aArray=_Test()
    Return SetError(@error,@extended,$aArray)
EndFunc

Func _TestWrapModifyRet()
    Local $aArray=_Test()
    $aArray[555]=4
    SetError(@error,@extended)
    Return $aArray
EndFunc

Func _TestWrapModifySetErrAtRet()
    Local $aArray=_Test()
    $aArray[555]=4
    Return SetError(@error,@extended,$aArray)
EndFunc

; -----------------------  TESTS --------------------------------

Local $sFinalResults=""

$iTimer=TimerInit()
For $i=0 To 20000
    $aArray=_Test()
    ;$aArray[$i]=$i
Next
$sFinalResults&="_Test() by itself time elapsed:            "&Round(TimerDiff($iTimer))&" ms"&@CRLF

$iTimer=TimerInit()
For $i=0 To 20000
    $aArray=_TestWrapDirectRet()
    ;$aArray[$i]=$i
Next
$sFinalResults&="_TestWrapDirectRet() time elapsed:     "&Round(TimerDiff($iTimer))&" ms"&@CRLF

$iTimer=TimerInit()
For $i=0 To 20000
    $aArray=_TestWrapDirectSetErrAtRet()
    ;$aArray[$i]=$i
Next
$sFinalResults&="_TestWrapDirectSetErrAtRet() time elapsed: "&Round(TimerDiff($iTimer))&" ms"&@CRLF

$iTimer=TimerInit()
For $i=0 To 20000
    $aArray=_TestWrapIndirectRet()
    ;$aArray[$i]=$i
Next
$sFinalResults&="_TestWrapIndirectRet() time elapsed:       "&Round(TimerDiff($iTimer))&" ms"&@CRLF

$iTimer=TimerInit()
For $i=0 To 20000
    $aArray=_TestWrapIndirectSetErrAtRet()
    ;$aArray[$i]=$i
Next
$sFinalResults&="_TestWrapIndirectSetErrAtRet() time elapsed:   "&Round(TimerDiff($iTimer))&" ms"&@CRLF

$iTimer=TimerInit()
For $i=0 To 20000
    $aArray=_TestWrapModifyRet()
    ;$aArray[$i]=$i
Next
$sFinalResults&="_TestWrapModifyRet() time elapsed:     "&Round(TimerDiff($iTimer))&" ms"&@CRLF

$iTimer=TimerInit()
For $i=0 To 20000
    $aArray=_TestWrapModifySetErrAtRet()
    ;$aArray[$i]=$i
Next
$sFinalResults&="_TestWrapModifySetErrAtRet() time elapsed: "&Round(TimerDiff($iTimer))&" ms"&@CRLF

ConsoleWrite("Test Results:"&@CRLF&$sFinalResults)
MsgBox(0,"Test results",$sFinalResults)

*edit, mistyped something, changed output string to add 'At' where it should be

Edited by ascendant
Link to comment
Share on other sites

Asking for the new Return keyword is going to stop right now. I'm sick of the idea. It's stupid, so drop it.

Now, as for copy-on-write... Hmm. It does appear to be working incorrectly or at least not how I expected which is about as good as working incorrectly.

Link to comment
Share on other sites

Asking for the new Return keyword is going to stop right now. I'm sick of the idea. It's stupid, so drop it.

:D I knew that'd be your reaction. You didn't get that I was kidding (and didn't actually *ask* for it). C'mon, Valik, lighten up :o

Link to comment
Share on other sites

:D I knew that'd be your reaction. You didn't get that I was kidding (and didn't actually *ask* for it). C'mon, Valik, lighten up :o

Maybe I didn't care that you were kidding. Maybe I'm just sick of seeing people continue to mention stupid ideas - in jest or not - after they have been rejected. It's annoying when a dead horse is beaten repeatedly.
Link to comment
Share on other sites

So I'm curious, is there a bug in AutoIT (in regards to copy-on-write), or has this been looked into yet? I didn't see anything listed in Bug reports, so thought I'd post to see what the situation is - and if I should try to 'code around' it.

thanks!

Now, as for copy-on-write... Hmm. It does appear to be working incorrectly or at least not how I expected which is about as good as working incorrectly.

Link to comment
Share on other sites

I haven't had time to look.

Are you trying to suggest that the AutoIt team gets to have a life outside of AutoIt? You actually get time away from developing? Jon must be getting generous when he starts handing out bonuses like that.

On the other hand I have a hunch he's been slacking off a bit himself lately.

Q: Can you fix problem XYZ?

A: First I'll see if it's broken

Q: Can you fix it this week?

A: If it's broken then if we don't get it fixed this week we might get it fixed sometime in the future.

Comment: But I need it now.

Reply: Since we missed "Now", it might be sometime in the future.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Are you trying to suggest that the AutoIt team gets to have a life outside of AutoIt? You actually get time away from developing? Jon must be getting generous when he starts handing out bonuses like that.

Indeed. I said fuck-it to AutoIt today and re-wrote a Visual Studio add-in! First time in quite awhile I actually wrote C++ for something other than AutoIt.

On the other hand I have a hunch he's been slacking off a bit himself lately.

Jon is perpetually slacking off and/or drunk. :D

Q: Can you fix problem XYZ?

A: First I'll see if it's broken

Q: Can you fix it this week?

A: If it's broken then if we don't get it fixed this week we might get it fixed sometime in the future.

Comment: But I need it now.

Reply: Since we missed "Now", it might be sometime in the future.

Is that pseudo-exchange yours? Where did you obtain it? I like it and wish to re-use it with proper attribution. It's somewhat more polite than "fuck off you dirty sod, I'm not here to serve you".
Link to comment
Share on other sites

I just wrote the psuedo-exchange on the fly and have not gotten around to copyrighting it yet so do what you want with it.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

What kind of add-in were you writing?

I write code using variable-width fonts but I often need to switch to a fixed-width font in order to ensure comments don't run way off the right of the screen for others. I have long had an extension with a menu-item that allowed me to quickly toggle to Courier New and back again to whatever I was using before. However, the way I wrote it did not allow for it to support key bindings. I rewrote it so that it can be bound to a key using Visual Studio's normal key binding interface.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...