Single Static Assignment

Or “write once”

This is a form that LLVM uses for its IR when compiling. It makes certain kinds of analysis much easier to perform as its simpler to determine how variables are affected long term and what their life times are.

And Rust uses it for EVERYTHING – you literally have to tell Rust that a variable should be “mut” or mutable.

Now you might think “OMG this makes things SO difficult”.

But in reality it actually makes things easier especially in multi threaded code because you cannot from one thread modify something in a way that could cause side effects in other threads.

And the really nice thing is that Rust actually can tell at compile time that your code is thread safe – this is really pretty cool and the more I learn about Rust the more I like some of the design decisions they made when creating the language.

This style is much harder to use in Xojo. You’d have to make every property that is exposed a computed property that you can assign once. And this is a bit of work to do. But it is possible. Again doing this could help eliminate certain kinds of bugs. Those that come from side effects because in one place you alter a property and yet it shouldn’t be or wasn’t expected to be.

Certainly computed properties can help as it makes it possible to put code in the setter so you can see when a property is modified. This is handy as heck and something I use a lot to try & make sure I don’t have weird side effects. Or when I do I can convert the simple property to a computed one and then put break points in the setter & getter.*

While SSA form has certain qualities that work well in languages & tools designed around this form, using it in Xojo _might_ be more pain than its worth.

Why ?

Suppose you create a custom subclass of a control and want to use SSA. If you have added properties on your subclass they may get initialized when the window is opened. Should your SSA property be accepting that value and no others ? Or should it only accept the first value it is assigned from code ?

Determining which case you’re dealing with is possible (see https://www.great-white-software.com/blog/2019/05/31/making-a-constructor-sometimes-illegal-to-call/) but now you have a lot of extra code in every property you want to be this kind.

In total I’m not sure that th effort is worth it for every property. But for a handful it might be.

  • That you can switch a simple property to a computed on or method get / set pair and NOT have to change other code is actually one of the things I appreciate about Xojo.

Whatever you think of him

Joel Spolksy has some great blog posts

Several I re-read from time to time just to refresh my recollection of them or to glean something new from them I may have missed on one of the tens of rereads I did.

I really like his blog posts about organization style, functional specs and other aspects of software development. Mostly because he’s done a lot of this more than once.

Some of my favourites

How to be a program manager It’s a great read and talks about WHAT a program manager does and who should, and who shouldn’t be the program manager and why. And it has some excellent lins to other posts about functional specs. Perhaps the BETS bit of advice in this one is

To make sure that the debate happens respectfully and on a rational basis of facts, it’s absolutely critical that the program managers and developers be peers

https://www.joelonsoftware.com/2009/03/09/how-to-be-a-program-manager/

Painless Functional Specifications is another great one and has sound advice for anyone who writes software. Starting WITHOUT a spec is masochistic. Or as Joel writes

It’s as stupid as setting off to cross the Mojave desert with just the clothes on your back, hoping to “wing it.” 

https://www.joelonsoftware.com/2000/10/02/painless-functional-specifications-part-1-why-bother/

Setting off without knowing where you’re going, how you plan to get there, or even where “there” is is simply crazy. And a waste of time and effort

If you haven read his blog I would – especially the archives as there is a lot of really good advice on it.

Enum extender updated

Yeah again 🙂

Fixed a bug where it would write the wrong value into the data on the clipboard and so it would create an incorrect enum to be pasted.

Thats been fixed

More things on the way !

On Propertiness

I’m not sure thats a word – propertiness

I’m using it to describe something that acts or behaves as a “property”

What defines something being a “property” ?

C# uses

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties

There’s no specific requirement that a property be a simple type or a computed property – just that it has the ability to be read, written or possibly can access a private member.

A property could be implemented in Xojo using :

  1. a simple type like an Integer, Color, etc
  2. a computed property
  3. a pair of methods

Each has pro’s an cons

A simple property allows you to quickly and easily add a some setting for whatever you are adding this to. But, you cant easily make such a property read only or write only. And your code wont notice when this is set since there’s no event or anything to tell you it has been set. Subclasses cannot “override” this kind of property but they can “shadow” it.

A computed property is a simple set of get / set methods that are associated with the property. The setter has an implied parameter, value, that is the same type as your computed property. The getter has no such parameter and returns a value that is the same type as the computed property. The upside to this kind of property is now you have a place to put code to react to changes in the value, or to know when something accesses the value by putting code in the getter. Subclasses cannot “override” this kind of property but they can “shadow” it. This often has a protected or private backing variable that actually gets set or read – but not always as it may be derived from some other data held internally like the ubound of an array, number of rows in a listbox, etc.

A pair of methods, one define to return the value of whatever type and one to set the value, using assigns, are almost identical to a computed property. The biggest difference here is that, because methods ARE virtual this kind of property can be overridden by subclasses if they simply implement one or both of the methods that define the property.

Perhaps the most unique and useful aspect of these different types is that, in code that uses the property, you dont have to care what implementation is used. And, in code that creates & defines the property you can safely switch from one type to another as long as you maintain the same API (ie/ if a property is initially readable & writable then you have to maintain that when you switch to a different style otherwise code that expects to be able to write that property will no longer compile)

The upside to the last style is that the means you can “define properties” on an interface. Since a property isnt a specific implementation but a behaviour an interface can define the getter & setter and then any implementors of that interface now can expose that property with whatever custom implementation they need to implement.

Have fun !

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