Shop OBEX P1 Docs P2 Docs Learn Events
looping until a lock is set — Parallax Forums

looping until a lock is set

agsags Posts: 386
edited 2013-04-24 21:22 in Propeller 1
I think this is just a matter of style and/or personal taste. In the Propeller Manual (and many other places) I see this idiom frequently used to wait until a lock can be obtained:
repeat until not lockset(lockID)

This is "backwards" from my way of thinking (not wrong, just reverse-logic for me). Am I missing some subtlety of why one wouldn't use:
repeat while lockset(lockID)

? I don't think I've ever seen the latter form, always the former, which makes me wonder if I'm missing something.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-24 16:00
    Nope, you're not missing anything. Either form will work. The "repeat while" is a little more efficient since you don't need the "not" then.
  • agsags Posts: 386
    edited 2013-04-24 19:14
    Mike Green wrote: »
    Nope, you're not missing anything. Either form will work. The "repeat while" is a little more efficient since you don't need the "not" then.

    ...and that's precisely why I favor it in general (I guess I'm a stingy nanosecond spendthrift)

    Thanks for the reply, Mike.
  • kuronekokuroneko Posts: 3,623
    edited 2013-04-24 19:27
    ags wrote: »
    ...and that's precisely why I favor it in general (I guess I'm a stingy nanosecond spendthrift)
    ... in which case you should actually use
    repeat
      while lockset(lockID)
    
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-04-24 20:27
    @kuroneko, what is the difference? I would think that one line would be better than two, but being new to Spin, I have no clue!!!!
  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-24 20:33
    The Propeller Tool (and maybe the other Prop compilers) uses a different interpretive code for the one line version than for the two line version and the latter is faster and smaller by a little.
  • agsags Posts: 386
    edited 2013-04-24 20:35
    kuroneko wrote: »
    ... in which case you should actually use
    repeat
      while lockset(lockID)
    

    Yes, that is the fastest repeat loop available. Unfortunately, I think it fails at least one other design requirement... :smile:

    Edit: I thought this was a joke. Isn't repeat alone on a line an infinite loop? (if there is no indent on the 2nd line)
  • kuronekokuroneko Posts: 3,623
    edited 2013-04-24 20:38
    NWCCTV wrote: »
    @kuroneko, what is the difference? I would think that one line would be better than two, but being new to Spin, I have no clue!!!!
    The difference is that the one liner does the exit check at the beginning of the loop (empty body) the two liner does it at the end (again empty body). The latter requires less byte codes which generally speeds up the interpreter (less hub access).

    That said, a shorter loop time isn't a guarantee for faster overall time. It all comes down to when the lock becomes available in relation to the actual check. I've seen cases where the one liner was actually faster (waiting for a PASM command ACK) despite taking up more space.
  • kuronekokuroneko Posts: 3,623
    edited 2013-04-24 20:41
    ags wrote: »
    Edit: I thought this was a joke. Isn't repeat alone on a line an infinite loop? (if there is no indent on the 2nd line)
    Check with PropTool and indicators enabled. When you put an empty line between them you'll see it's like C's do/while style.
  • agsags Posts: 386
    edited 2013-04-24 20:53
    kuroneko wrote: »
    Check with PropTool and indicators enabled. When you put an empty line between them you'll see it's like C's do/while style.

    Yes, (now that you point it out) of course. I still find myself thinking (and coding) as if "while" can be the first token of a statement. I tend to leave the "repeat" part out. The empty body assisted in my confusion. Same for a repeat/until loop.

    Notwithstanding your explanation about byte codes, it's still not obvious (or clear) to me why the former (one line) is slower than the latter (two lines). I'm confident you are correct, but intuition would have led me to expect that the one line format would be faster (the entire line could in theory be handled by one byte code - since there is no place for any interceding statements (the body of the loop)) while the two-line style would result in two separate steps (meaning two byte codes to interpret). I have never poked into the byte codes so clearly my intuition is misinformed...
  • kuronekokuroneko Posts: 3,623
    edited 2013-04-24 21:22
    For reference the bst compiler listing:
    |===========================================================================|
    Objects : -
    Untitled1
    
    Object Address : 0010 : Object Name : Untitled1
    
    Binary Image Information :
    PBASE : 0010
    VBASE : 0028
    DBASE : 0030
    PCURR : 001C
    DCURR : 0034
    |===========================================================================|
    |===========================================================================|
    Object Untitled1
    Object Base is 0010
    |===========================================================================|
    Object Constants
    |===========================================================================|
    |===========================================================================|
    Object DAT Blocks
    |===========================================================================|
    |===========================================================================|
    |===========================================================================|
    Spin Block null with 0 Parameters and 0 Extra Stack Longs. Method 1
    PUB null
    
    Local Parameter DBASE:0000 - Result
    |===========================================================================|
    3                        repeat while lockset(0)
    Addr : 001C: Label0002
    Addr : 001C:             35  : Constant 1 $00000000
    Addr : 001D:             2A  : LockSet(Id) Push
    Addr : 001E: JZ Label0004
    Addr : 001E:          0A 02  : jz Address = 0022 2
    Addr : 0020: Label0003
    Addr : 0020: JMP Label0002
    Addr : 0020:          04 7A  : Jmp 001C -6   
    Addr : 0022: Label0004
    Addr : 0022:             32  : Return        
    |===========================================================================|
    Spin Block eins with 0 Parameters and 0 Extra Stack Longs. Method 2
    PUB eins
    
    Local Parameter DBASE:0000 - Result
    |===========================================================================|
    7                        repeat
    Addr : 0023: Label0002
    Addr : 0023: Label0003
    8                        while lockset(0)
    Addr : 0023:             35  : Constant 1 $00000000
    Addr : 0024:             2A  : LockSet(Id) Push
    Addr : 0025: JNZ Label0002
    Addr : 0025:          0B 7C  : jnz Address = 0023 -4
    Addr : 0027: Label0004
    Addr : 0027:             32  : Return        
    
Sign In or Register to comment.