Unnamed functions
Why you shouldn’t use them. What’s more, every function call costs time, think about inlining simple functions code.
Continue Reading | No Comments
Tags: Function, Optimization
Faster Math.ceil()
For those of you who optimize every bit of code.
Continue Reading | No Comments
Tags: Math.ceil(), Optimization
The easiest way to protect your code
I heard on Russian twitter that flash decompilers fail to parse embeded binary data (for example another SWFs). Tested it locally and that rendered to be true — the content of embeded SWF was impossible to extract. That seemed so simple yet effective. Just use something like
[Embed(source="swf.swf", mimeType="application/octet-stream")]
Someone could write a simple command line script to split SWFs into several parts to embed them inside a loader SWF and assemble again. Or it’s possible to add simple encryption to go further.
This idea is indeed very simple so someone might have made a tool like this already. I didn’t find a tool but I did find a post about this technique. And since then I successfully used it in one of my projects.
Tags: Embed, Protection
as Class vs. Class(..)
Some of you might think that
p as Point
and
Point( p )
are equal. They might “mean” the same but they are not. First of all if p can’t be converted to Point result of the first operation is null, but the second one will fail with an error like this:
TypeError: Error #1034: Type Coercion failed: cannot convert 42 to flash.geom.Point.
What’s more, the first one is faster. Here’s the test:
public function typetest()
{
var total: int;
for ( var i: int = 0; i < 10; i++ )
total += execute();
trace( "as", total/10 );
total = 0;
for ( var i: int = 0; i < 10; i++ )
total += execute2();
trace( "cast", total/10 );
}
private function execute(): int
{
var t: int = getTimer();
var i: int = 0;
var p: * = new Point();
for ( i; i < 1000000; i++ )
{
p as Point;
}
return getTimer() - t;
}
private function execute2(): int
{
var t: int = getTimer();
var i: int = 0;
var p: * = new Point();
for ( i; i < 1000000; i++ )
{
Point(p);
}
return getTimer() - t;
}
This code outputs for me the following execution times:
as 157
cast 217.7
Tags: as, Cast, Optimization, Type conversion
How Maple helps
Sometimes it’s good to test some math model before coding it into ActionScript or any other language, draw some cool graphs and play with parameters. Of course you could write a simple flash application to draw graphs for you but why waste time if there are a lot of math software around? In school we used Maple, so from time to time I use it to check stuff before actually coding it. I have to say that I’m not good at it but my knowledge is enough for my own tasks.
Well, right now I need to code a pseudo random movement. I need some kind of a periodic pattern with bursts and decaying.
So, I opened Maple and set several constants I had in mind.
minAmp — minimal value of global amplitude,
maxAmp — maximum value,
minLen — minimal wave length,
deltaLen — minLen + deltaLen = maxLen — maximum wave length,
speed — wave speed (rendered useless).
After that I wrote functions for amplitude.
In the example I used constant wave length, didn’t have time to play more with it. Here everything is simple. We have some x value from which wave length and amplitude is counted. Both are periodical.
Here’s the graph.
Now we can add one more sin which will be limited by this amplitude.
We are getting a periodical movement but it’s too boring. Here goes the magic.
This function generates all the randomness. Simply put, there’s some seed which grows from 0 to 1 (actually a bit more) by dseed and after it reaches 1 it goes back to almost zero and dseed (delta seed — seed change speed) changes to some other value controlled by minRand and ampRand. At the end we get new value in the last line.
That’s how we get such a jerky rhythm, and because of dseed the movement can be be fast and slow. And everything is limited by periodic amplitude.
Let’s check the result.
This is just what I wanted. If you play with parameters a bit ou can get very interesting results.
Here’s the example. You shouldn’t stare it for a long time q: It uses much CPU so I recommend you to close the tab after you finish reading.
Continue Reading | No Comments
Tags: Effect, Maple, Randomness
Prolog for Mac
Let’s assume that you need to set up Prolog for OS X. If you are still here let’s continue.
After reading some book from 80s about Prolog I found that language to be interesting and I decided to write some simple things in it. But I had to install it somehow on my Mac preferably without any stupid virtual machines. So here’s what I ended with.
- All crossplatform solutions are based on Eclipse, so first of all you need Eclipse. There are a lot of versions, but I chose the one with less stuff I don’t need because I won’t use anything related to Java development.
- The first plugin I tested was PDT which is too old and doesn’t work with Eclipse 3.5, I downloaded 3.1 but haven’t tested it because I got another plugin which works — ProDT. This is just what I need. From Downloads you should choose the smallest file.
- For ProDT to work you need SWI-Prolog — some kind of opensource Prolog.
- I’m not sure if SWI-Prolog requires MacPorts, but anyway I recommend you to install it. It’s useful.
- Extract ProDT to eclipse/dropins/prodt/eclipse/plugins, run eclipse which installs it automatically.
- After that in ProDT preferences ( Eclipse -> Preferences -> Prolog -> Compilers -> Swi Compiler ) set compiler path which is at
/opt/local/bin/swipl. - Switch to Prolog view Window -> Show Perspective -> Other… -> Prolog.
That’s it. Now you can test if it actually works:
yes( one ).
yes( two ).
Run and write in console:
yes(X).
x = one ;
x = two.
Tags: Eclipse, Mac, MacPorts, PDT, ProDT, Prolog, SWI-Prolog
Speed vs. OOP
As I found out, AS3 compiler is too stupid to add even trivial optimizations.
I’ve been trying to optimize one of my heavy libraries I use everywhere for some time. It parses a lot of XML using e4x and creates many objects which it has to check using isSomething() function which is just 3 lines of code. So, this function is executed for every single object created.
Let’s check the test now.
var t: int;
var v: int = 5;
var w: int = 6;
for ( var i: int = 0; i < 5; i++ )
{
t = getTimer();
for ( var j: int = 0; j < 1000000; j++ )
{
var k: Boolean = func( v, w );
//var k: Boolean = v > w;
}
trace( getTimer() - t );
}
private function func( v: int, w: int ): Boolean
{
return v > w;
}
This executes on my Macbook Pro for 400ms.
If you uncomment the second line and comment the first one, execution time drops to 90-100ms. Which is 4 times less. But this is for 100000 calls, you can say. Remember that 30fps = 33ms for every frame, so one millisecond here and one millisecond there give noticeable result. And as I said the same short function is executed for every object. And there can be a lot of them. Though I don’t have to do it every frame but I don’t want my application freeze in the beginning either.
Moreover,
var t: int;
for ( var i: int = 0; i < 5; i++ )
{
t = getTimer();
for ( var j: int = 0; j < 1000000; j++ )
{
var k: int = value;
//var k: int = _value;
}
trace( getTimer() - t );
}
public function get value(): int
{
return _value;
}
it’s the same for getters and setters because they are also functions.
So I just replaced all calls of this function for actual code (6 occurrences). Every programmer who knows OOP will say that it’s bad to copy and paste code, if he doesn’t know why I did that. He might also go post on the forums that I am a noob, make a function of these pasted code snippets and will be proud of himself.
Here’s the question. Speed or OOP? While AS3 compiler is so dumb and can’t inline simple functions like this I’ll have to break readability in favor of execution speed.
Tags: Framework, Inline, OOP, Optimization, XML
Flex on Rails is too fast
Ruby on Rails evolves so fast that at version 2.3.2 all the code from a year 2008 book doesn’t work in April 2009.
Well, Flex + Ruby on Rails is great, the speed with which simple applications are made is astonishing. I’m reading right now Flex on Rails: Building Rich Internet Applications with Adobe Flex 3 and Rails 2 and as I said every once and a while I get stuck into compatibility problem.
Right now the problem is the following — in Flex HTTPService class though it says that method = “GET|POST|PUT|DELETE”, but the actual data is sent with GET or POST (for some reason I’m sure that 90% users don’t even know that PUT and DELETE exist). But in case with RoR they are essential, for example GET accounts/1 returns account information, PUT accounts/1 updates account and DELETE accounts/1 deletes an account. The book says just that and as a workaround it suggests to add ?_method=put to URL. But in 2.3.2 it doesn’t work anymore. Trying to do that we get
ActionController::MethodNotAllowed (Only get, put, and delete requests are allowed.)
Google says that I should to send method=put with request itself. Requests are sent in XML but I can’t have 2 roots in AS3 XML, so <_method>put</_method><data /> doesn’t work. Wrapping the whole construct into another root fails.
After googling a bit more I found out that I must set header HTTP_X_HTTP_METHOD_OVERRIDE = PUT. But it didn’t work. I almost gave up but visited this chinese link where our communist friend solved the problem!. It seems that Rails somehow adds to headers another HTTP_ prefix and doesn’t know what to do with things like HTTP_HTTP_X_HTTP_METHOD_OVERRIDE = PUT.
So, here’s the code which works
<mx:HTTPService id=”accountsUpdate”
url=”{CONTEXT_URL}/accounts/{accountsGrid.selectedItem.id}”
method=”POST” resultFormat=”e4x” contentType=”application/xml” headers=”{{X_HTTP_METHOD_OVERRIDE: ‘PUT’}}” >
</mx:HTTPService>
I hope that this text helps people trying to learn Ruby on Rails and how it works with Flex.
Tags: 2.3.2, Flex, Flex on Rails, Headers, HTTP, HTTPService, RoR, Ruby, X_HTTP_METHOD_OVERRIDE






