Smart Mobile Studio
  • News
  • Forums
  • Download
  • Store
  • Showcases
    • Featured demos
    • The Smart Contest 2013, Round 1 – Graphics
  • Documentation
    • Get the book
    • System requirements
    • Prerequisites
    • Getting started
      • Introduction
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • Themes and styles
    • Project types
      • Visual project
      • Game project
      • Console project
    • Layout manager
    • Networking
      • TW3HttpRequest
      • TW3JSONP
      • Loading files
  • About

Monthly Archives: September 2011

Focus, callstack and recursive processing

Posted on 29.09.2011 by Jon Lennart Posted in Documentation 6 Comments
Call stack limitations

Call stack limitations

The world of mobile computing is getting better and better, but there are some limitations when dealing with smart gadgets running iOS or Android. The first thing is obviously the speed, which doesn’t even come close to what an average home computer can muster. Even my 5 year old test machine can outrun any mobile device without making so much as a dent in the cpu-o-meter.

The second limitation people are going to discover, depending of-course on the line of work you do, is that mobile devices is the proverbial banker when it comes to memory – implying that your call stack is but a microbial-fraction of what we are used to. What does this mean? Well every time you call a method under Javascript, you eat up one call-slot. If this procedure in turns calls another, and another (and so on) it quickly gobbles up the meager call stack limit. The size of the call stack is varied and depends on the vendor, as of writing iOS is sticking to it’s near obsolete range of 100 recursive calls while Android is said (I couldn’t find any official information on this) to have a call stack of about 800.

To kiss or not to kiss, that is the question

Kiss (keep it simple stupid) is one way to go when it comes to mobile development. But there are times when you must resort to classical speed mechanisms to reach the full potential of a system. As such I am in the process of optimizing the base-classes for speed and simplicity (some form of balance preferably). The method is really simple and it’s known as loop expansion (or just “unrolling”). Let’s for sake of argument say you have a control with a lot of child objects, like a grid or a list. Instead of doing a simple for/next loop, like this:

[sourcecode language=”delphi”]
Procedure TMyObject.DoSomething;
var
x:Integer;
Begin
for x:=0 to FChildren.Count-1 do
Begin
process(FChildren[x]);
end;
end;
[/sourcecode]

The above example is perfectly valid, but it’s quite slow depending on the data-load you expect. A list control could be expected to handle hundreds if not thousands of elements – so we want to speed things up and minimize loop time:

[sourcecode language=”delphi”]
Procedure TMyObject.DoSomething;
var
mLongs:Integer;
mShort:Integer;
mCount:Integer;
mIndex:Integer;
begin
mCount:=FChildren.Count;
mLongs:=mCount shr 3;
mShort:=mCount mod 8;

while mLongs>0 do
Begin
process(FChildren[mIndex]);inc(mIndex);
process(FChildren[mIndex]);inc(mIndex);
process(FChildren[mIndex]);inc(mIndex);
process(FChildren[mIndex]);inc(mIndex);
process(FChildren[mIndex]);inc(mIndex);
process(FChildren[mIndex]);inc(mIndex);
process(FChildren[mIndex]);inc(mIndex);
process(FChildren[mIndex]);inc(mIndex);
dec(mLongs);
end;

while mShort>0 do
Begin
process(FChildren[mIndex]);inc(mIndex);
dec(mShort);
end;
end;
[/sourcecode]

Speed-wise, at least when it comes to crunching fixed data like pixels or typed JS arrays, is nearly incomparable to the first approach. It obviously produce more code – but the benefits are just to good to ignore (roughly 4 times as fast).

As long as your final build does not exceed 10 megabytes, which is Apple’s limit on single javascript files, it’s not a problem (and that would be a huge, huge project if you ask me).

Men first, women and children last

When doing recursive calls that affect an entire object model you have to deal with the men first, meaning of-course those elements that does not have children attach to them (oh this is gonna get me into so much trouble if my wife reads it). Why? Because we want to minimize the number of recursive calls (or steps taken) a control has to make. Let’s use the simple formula to demonstrate (this would also be loop expanded in the final release):

[sourcecode language=”delphi”]
Procedure TMyObject.DoSomething;
var
x:Integer;
mCount:Integer;
mStack: TObjectList;
mObj: TObject;
Begin
//avoid multiple calls to getCount in FChildren array
mCount:=FChildren.Count-1;
for x:=0 to mCount do
Begin
//minimize call to getItem in FChildren array
mObj:=FChildren[x];
if mObj.count=0 then
// No kids? Do singles first
process(mObj) else
Begin
//kids? Ok, we’ll handle that later
if mStack=NIL then
mStack:=TobjectList.Create;
mStack.add(mObj);
end;
end;

if mStack<>NIL then
Begin
mCount:=mStack.Count;
while mCount>0 do
Begin
mObj:=mStack[mCount-1];
process(mObj);
dec(mCount);
end;
mStack.free;
end;
end;
[/sourcecode]

And last but not least, the “process” method should be inlined if possible, thats 50% of the point here. If you can avoid a secondary call – then avoid it, that’s how you cut down on call-stack overload and as a nice benefit you get a speed boost.

Notes

#1 [29.09.11, 18:31]: Eric Grange pointed out (as did two readers on this page) that the same effect will happen if we apply FChildren.Count-1 and start from zero. That would be slightly faster to since we dont execute a continous subtraction of the index. A clear error on my part.

#2 [29.09.11, 18:31]: Interestingly, the -1 trick was originally used for string processing (hence starting at 1) and has nothing to do with speed per-see, except that you save an “if FChildren.Count>0 then” in some languages. But this tiny advantage is indeed lost by the use of [-1] in the loop itself. Some basic compilers will execute the code even if FChildren.count =0. This is an old habit from the days of Amiga BlitzBasic programming.

#3 [29.09.11, 18:35]: Also the “process()” call should only be concidered pseudo code. It’s the loop expansion that’s being demonstrated. Preferably you would work with the data directly at this point rather than call another proc, since the whole point is to limit the number of calls while speeding up a process. It’s not meant as a literal example, but as the bare-bones of a technique.

General programming Smart Mobile Studio tutorial

The elegance of Javascript

Posted on 24.09.2011 by Jon Lennart Posted in News and articles

Two year ago I used to laugh at Javascript, but the more I play with it these days, I find myself enjoying the experience. It cant be compared to Delphi on any level, but there are some features that just blows your mind. I find myself spending a lot of time in Besen, a javascript interpreter and bytecode compiler written in Delphi – just for the sheer fun of it.I like the way dynamic parameters (what we would call array of const) are implemented and that they remain hidden to the entrypoint. This allows you to do do a form of undercover overloading. Unless the method is aware of _the calling interface_ (not the other way around like we are used to) then it could support only a sub-set of the intended functionality. By patching the original function and chaining to it you basically can “emulate” overloading based on condition rather than explicit call. This opens up for some clever API designs.I love the ability to pick values based on conditions, but compacted into a single line without having to invoke a equating entity:[sourcecode language=”javascript”]someValue = (backwards ? (100-percent) : (percent-100)) + "%";[/sourcecode]Having said that there are some aspects of javascript that makes me ponder the collective wisdom of W3C. As a fail-safe i usually add an “if” section to thoughts like this, because otherwise mother nature will go out of her way to disprove me. I can only imagine the work of cleaning up after Netscape and Microsoft, trying to play ball with Opera while Webkit came sneaking in the back door.

Media mayhem

The factors are many, yet the organization of the latest HTML5 multimedia functions is neither fish nor foul. It’s not truly object(ive) in the C++/Delphi sort of way, nor is it linear like basic or old school pascal. It’s both, and none of them at the same time . which makes it difficult to set up generic rules that would apply everywhere in our object model. The only solution is to let go of what the W3C has marked as “legacy features”. Which is hard to do when they produce results so easily 🙂

Threading, security and overkill

Another feature that seems like overkill, is the javascript notion of threads (read: blue threads), referred to as web-workers in the W3C specs for HTML5 (actually better info on Apple’s docs for safari). Im not sure what i was expecting, but i was sort of hoping to use threads to crunch pixel data. You can do that, but you are unable to hook the reference since it runs in a different context.

  • All communication between a worker and the DOM is through a message pipe
  • The only datatype you can pass to and from is a blob
  • All bindings are late-binding
  • Worker code cannot reside in the main document

Thankfully I found a “loophole”, I’ll document it later – but it wont make it into OP4JS because it’s basically a hack. You can use TW3GlobalStorage (one of our classes) as a mirror cache for the data – and it’s actually faster than creating, populating and moving a blob into the thread context. Although it does require initialization ofcourse. But i suspect that loophole will be closed in the next release.

External references

  • Besen – SourceForge project page 
  • Web workers – W3C documentation
javascript

Snippets and comments, simple but effective

Posted on 23.09.2011 by Jon Lennart Posted in Developers log 2 Comments

The OP4JS IDE (integrated development environment) aims to be simple, neat and clean. There are two reasons for this: First of all there is no point trying to catch up with products that have been in development for nearly two decades – and secondly because we don’t necessarily agree with the route some companies have taken with regards to their IDE’s.

Delphi 7, the most popular IDE ever

Delphi 7, the most popular IDE ever

If you ask a Delphi developer what his all-time favorite IDE is, chances are he is going to put the finger somewhere between Delphi 5 and Delphi 7. The environments back then were responsive, lightweight and easy to work with. The interface was so intuitive that even without formal training it required little effort to learn how to use the it. For me personally Delphi 7 is the undisputed king of IDE’s with one exception, like XCode it was a multi windowed environment which is a feature that messes up your work-space.

So for our IDE we turned the idea on it’s head. Since we are starting from scratch – why not use this opportunity to simplify things again? We have neither the time or manpower to catch up with the likes of Delphi, XCode or MonoDevelop. But what we do have is a keen eye on details and a desire to keep things effective.

Back to basics

What are the basics of a good IDE? What do we need to get our work done quickly and efficiently? What do I need?

  • Tabbed source editing
  • Syntax highlighting
  • Method proposal (press “.”)
  • Parameter hints
  • Project tree-view
  • Unit tree-view
  • Property inspector
  • Form designer
  • A snippet library
  • A proper note system
  • A help system designed for human beings
  • CTRL + mouse to quickly locate a symbol (or equivalent functionality)

Text matters

Use notes as your scrap book

Use notes as your scrap book

If you are anything like me you probably have a desktop littered with notes, books, gadgets and coffey mugs. Once a week I give my desk a run over, clean out the thrash and try to organize my notes in a logical, easy to use fashion. I also have a couple of huge whiteboards with important things kept in check by magnets. Fact is, text matters. It is through words that we formulate and transfer ideas.

Sadly the important of text and keeping our ideas organized havent really been implemented in most development environments. Delphi has a skinny “to do” list which is added to the project. I know a couple of people that use it – but let’s be honest: it’s pretty meager stuff. I mean, if I want to generate some form of documentation from my source-code, wouldn’t it be better to keep my notes in one place and my code in another? I’m not suggesting that we should stop commenting code, far from it, I’m simply saying that code-comments is more than enough for the code part. Adding documentation, ideas, who-did-what, credits and examples in the code is a bit excessive.

In short, OP4JS supports comments for every element belonging to the project. You can even comment on images if you like. But it’s not a simple text field or a memo – it’s a full editor. The view will actually split to make room for your notes and as such you can jot down everything (or just use it as a scrap-book when trying out new ideas). The comment editor has proven instrumental when porting Javascript to Object Pascal, because I can paste the JS code in the comment editor and convert it method by method with both sources in plain view. It’s also very handy when it comes to keeping track of who did what and where you last worked, just in case you forget between sessions.

Snippets as a resource

A snippet is just a piece of code that you use often or that you found online. Handy enough to keep but not essential enough to deserve it’s own unit. I haven’t really used snippets before but after implementing it – it’s quite addictive (!) Whenever I go online to search for a solution or study a particular task – the snippet library functions as my extended clipboard, if that makes any sense.

Snippets is an underestimated resource

Snippets is an underestimated resource

borland delphi ide

Inline graphics, orientation and animations

Posted on 20.09.2011 by Jon Lennart Posted in News and articles 5 Comments
CSS based chevrons

CSS based chevrons

This week we are seeing some very cool ideas finally maturing onto the visual canvas. First and foremost: inline graphics. Under the traditional HTML paradigm resources such as images or soundfiles were always losely bound external entities. So before an image could be used it naturally had to download its content from a webserver or read it from a local file. But under HTML5 all of that goes out the window, because now you can embed your graphics as Base64 encoded characters. In short: transform your graphics into text and they can be applied anywhere.

So today our TW3MenuListItem class got automatic support for inline glyphs. The default glyph is of-course the ordinary iPhone chevron picture, but when TImageList is done you will be able to apply just about anything. Since this is HTML5 you can also make use of your styles to add further “bling” to your elements – but we will leave that part up to you. Our job is to deliver a system with the basic iOS widgets ready – but object pascal style, which means easy to use, easy to work with and as little fluff as possible.

Orientation

Angles should be used prudently

Angles should be used prudently

Another cool feature that is now going through the test phase is the Angle property. All components that derive from TCustomControl have this property, and as the name suggests you can use it to rotate the control as you see fit. While it’s not the biggest feature in the world it does open up for a more colorful expression in the user interface. Want a yellow price banner over a product? Put a label on it, set the color and put -30 in the degree property.

Animations

This feature is my favorite, but it’s also the most complex to capture under object pascal. Webkit allows you to define animation sequences, which can then be applied to a html element or a style. You then use a trigger rule to activate the animation. The cool part is of-course that most of these effects have hardware support – so they run smoothly and without any flicker what so ever.

Since Javascript is not the fastest horse in the stable, at least not on devices like iPhone 3GS – it’s very important to use hardware accelerated animations as much as possible. You don’t want to rotate a picture by manipulating the degree property under a timer. It works of-course, but the results cant be compared to the effects produced by hardware accelerated CSS3 animations.

But of-course, under OP4JS you don’t have to worry about that

 External references

  • CSS Backgrounds and Borders Module Level 3 – www.w3.org
  • The document object model – www.w3.org
animation CSS HTML5

Something is coming…

Posted on 01.09.2011 by Jørn E. Angeltveit Posted in News

This website is currently under construction. Over the next days and weeks more and more material will become available here – so stay tuned for a very exciting product 🙂

Pages

  • About
  • Feature Matrix
  • Forums
  • News
  • Release History
  • Download
  • Showcases
    • The Smart Contest 2013, Round 1 – Graphics
  • Store
  • Documentation
    • Creating your own controls
    • Debugging, exceptions and error handling
    • Differences between Delphi and Smart
    • Get the book
    • Getting started
      • Introduction
      • Local storage, session storage and global storage
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • pmSmart Box Model
      • Themes and styles
    • Layout manager
    • Networking
      • Loading files
      • TW3HttpRequest
      • TW3JSONP
    • Prerequisites
    • Real data, talking to sqLite
    • System requirements
    • Project types
      • Visual project
      • Game project
      • Console project

Archives

  • December 2019
  • December 2018
  • November 2018
  • July 2018
  • June 2018
  • February 2018
  • September 2017
  • April 2017
  • November 2016
  • October 2016
  • September 2016
  • April 2016
  • March 2016
  • January 2016
  • October 2015
  • September 2015
  • July 2015
  • April 2015
  • January 2015
  • December 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • November 2011
  • October 2011
  • September 2011

Categories

  • Announcements (25)
  • Developers log (119)
  • Documentation (26)
  • News (104)
  • News and articles (16)

WordPress

  • Register
  • Log in
  • WordPress

Subscribe

  • Entries (RSS)
  • Comments (RSS)
© Optimale Systemer AS