Enum extender update

Added a little editor pane so you can quickly add values to an enum then get the whole mess written to the clipboard so you can paste that into the IDE

Hunting

Not with a gun – I’ve never shot a living thing in my life although I have fired guns of various kinds.

Right now I’m on the hunt for a “ridiculously fast string”

Xojo’s are OK but things like split, left, right, mid can really bog down if you use enormous strings. I’ve looked at a couple alternatives but so far haven’t found what I need ๐Ÿ™

And right now I’m working on a text editor (more or less) and stress testing it with big strings. Think “code editor” with gigantic swaths of code. My test string is 15Mb and while that might be crazy big when compared to the amount of code you might put in any single method in the IDE – if I can make this really responsive with that big string then it will fly with any thing smaller.

I’ll keep you posted on progress.

Optimization

Code optimization is often described as hand waving and magic.

But mostly its science.

Compilers implement transformations on code that results in provably identical semantic operation but that can result in faster performance, smaller code, or in some cases both (depending on what settings the particular compiler in use exposes)

LLVM uses a form known as SSA (single static assignment). This particular form makes reasoning about and proving the results of transformations are equivalent to the original code since once a value is assigned its never mutated.

SSA makes things like constant propagation & dead code elimination easier to do. Not that it could not be done in other forms compilers use juts that SSA happens to make these easier because of how SSA works.

Xojo’s use of LLVM enables some optimizations – but not all optimizations that LLVM can perform (there are literally hundreds)

So sometimes you need to perform some yourself.

  1. invariant code motion – these are statements, or portions of statements, that can be moved outside a loop and computed once because they do not change in the course of a loop. Some care needs to be taken with this optimization when performed manually as noted in the Wikipedia article. In Xojo constantly referencing picture.graphics can often be lifted outside the loop into a local and cutting down the number of calls to picture.graphics and using the cached local variable.
  2. loop unrolling – some times you can achieve a significant speed up by repeating the body of the loop operating on multiple different items in each loop pass. For instance instead of traversing an array in reverse order and deleting one element on each pass it may be faster to use a step size in the loop of 5 and remove 5 elements on each pass. Again there are some hazards with this. Often its useful to know the number of iterations of the loop at compile time in order to most effectively unroll the loop.
  3. common subexpression elimination – sometimes a portion of a calculation is repeated more than once and lifting the repeated portion out and doing it once and the reusing the pre-computed value can speed things up (it may also improve code clarity). In something like the following
    a = b * c + g
    a = b * c * e

    you might lift out the computation of b * c into a local and then just reuse the local
    bc = b * c
    a = bc + g
    a = bc * e
  4. constant folding and propagation – when known at compile time instead of generating code to compute a constant value, typically from literals, the result will be inserted instead.
    So instead of inserting code to do the computation of 24 * 60 * 60 the compiler will simply insert 86400. This optimization may also be applied to strings and other literals.

These are just 4 of many many possible optimizations. You can implement these by hand in your own code as well and some, like common subexpression elimination, may make long lines of code simpler by replace repeated calculations with a single local variable.

I’d encourage everyone to read the Wikipedia pages about these optimizations implement them in your own code.

Have fun !

Code posting

One of Xojo’s forum guidelines I dislike is

Code should be provided in your reply directly, rather than links to code on other websites or blogs, in order to maintain the postsโ€™ viability for a longer period.

I’ve come to dislike it because sometimes code examples require more than a handful of lines of code and posting a complete example application requires it be hosted somewhere else in order to link to it. Xojo’s forums have no way to post a long sample inline.

Longer samples posted with links in the forums post remains no more, or less, viable than it ever has when you do need to post more than a few lines of code.

Images and other content has the exact same issue. It has to be hosted elsewhere anyway.

Xojo edits get disabled quite rapidly after posting. It seems its about an hour and then the post is no longer editable. If there’s a correction to be made after this you cannot and then have to post all new code. For anyone who may not read the entire thread this can be a problem as they may see the first chunk of code and use it then complain it doesnt work or compile or whatever. And the auto, hopefully, notices this and posts new code.

So, unless its just a small amount of code I’m likely to post more substantial items on my blog or IfNotNil or linked to from there (since they also dont host the content for more substantial examples).

Full samples are on my servers – same as always.

And the other upside to if not nil is edits are enabled forever (so far). And should you ever want to remove the post you can. Its yours to do with as long as you want.

Enum extender

Tired of writing methods to convert enums to strings and back ?

This little tool lets you drag enum(s) from the IDE onto it and then select the text and paste it back in to the IDE

I will write two styles of conversions – one pair as extends and one as ToString/FromString

To paste the code back into the IDE see my previous post

You’ll find it here

Pasting plain text code

If, like me, you sometime munge text into code and then want to paste it into the IDE that can sometimes be a real chore

Unless you happen to know this little trick

If you have plain text lke

Public Function ToString(extends enumValue as Untitled) as String
  select case enumValue
    case Untitled.valueName
        return "valueName"
    case Untitled.valueName
        return "valueName"
    case Untitled.valueName
        return "valueName"
Else
 raise new UnsupportedOperationException
  end select
End Function


Public Function ToUntitled(extends stringValue as String) as Untitled
  select case stringValue
    case "valueName"
        return Untitled.valueName
    case "valueName"
        return Untitled.valueName
    case "valueName"
        return Untitled.valueName
  end select
Else
 raise new UnsupportedOperationException
End Function

Public Function Untitled_ToString( enumValue as Untitled) as String
  select case enumValue
    case Untitled.valueName
        return "valueName"
    case Untitled.valueName
        return "valueName"
    case Untitled.valueName
        return "valueName"
Else
 raise new UnsupportedOperationException
  end select
End Function


Public Function Untitled_FromString(stringValue as String) as Untitled
  select case stringValue
    case "valueName"
        return Untitled.valueName
    case "valueName"
        return Untitled.valueName
    case "valueName"
        return Untitled.valueName
Else
 raise new UnsupportedOperationException
  end select
End Function

and select it all and copy then, in the IDE select the MODULE, WINDOW, CLASS (not the methods group in any of those) and Paste

The downside here is that only the FIRST method gets pasted – but it does get pasted

So you have to do them one by one

But its better than nothing

Works well with a little helper app I’ll post later than writes methods to extend enums so its easy to convert them to/from strings

Who set that (and who wants to read it) ?

Sometimes you’d like to use what in other languages is referred to as a watchpoint. Some hardware support exists for this in Intel CPU’s but it is limited (there are only a handful or hardware watchpoints available.)

Or it could be done in software; but as noted on Wikipedia this is a ton slower.

Xojo really doesn’t support either type.

But, you can fake it by using computed properties. And since Xojo makes it easy to switch between a public property and a computed one WITHOUT having to update your code (this is a wonderful feature) you can quickly and easily put whatever break points and conditional watches you want IN your code and have much of the benefit of watchpoints.

If you have a public property that you want to see who is setting it simply

  1. select the property
  2. right click and select “Convert to computed property”

And now you can put a break point in the SET, or GET, code for that computed property.

With some creativity you could even get to being able to modify whether the break point is reached while watching your program execute.

Assigns

Xojo has this great feature. You can write sets of methods that can act like LValues. That is – ones that can have a value assigned to them. Or ones that occur on the LEFT of an assignment operator.

dim i as integer
i = 9 // <<< i is an LValue

What can I do with assigns ?

Well one of the things you can do is make methods behave like properties – regular or computed ones. So we can do something simple like

Class myClass
      Sub foo(assigns value as integer)
        // now do whatever we want with value
      End Sub
End Class

dim c as new myClass
c.foo = 9

This is nice but you can get the exact same effect with a public property or a computed property. So no real big deal here. You could have

Class myClass
     Public foo as integer
End Class

dim c as new myClass
c.foo = 9

OR something like

Class myClass
      ComputedProperty foo
        // now do whatever we want with value
      End ComputedProperty
End Class

dim c as new myClass
c.foo = 9

You really cant tell which form was used. And thats actually a nice trick that not many other languages have.

But, since we can write ANY kind of method that uses Assigns as the last parameter we can do things that are not possible with public properties or computed properties.

So we could write a method that, given an integer and string key gets assigned a value that gets stored in a database.

Sub newPrice(partID as integer, partName as string, assigns newPrice as double)

and then use this elsewhere like

newPrice(123, "washer") = 49.95

Public properties and computed properties are usually where I start when writing code, and I’ll transition from a public property to a computed one and then to a get / set pair of methods as needs are revised.

Fortunately Xojo makes this transition easy because each of those can be swapped for the other without any impact on the rest of your code when they have the same signature. FOr instance, without examining the code you have no idea whether the following uses a public property, computed property, or setter method with assigns

dim c as new myClass
c.someProperty = 100