Does Flex/Actionsctipt/Flash implements a mechanism for reusing Sprites? - apache-flex

I'm creating a game and I create sprites(enemies). I keep creating and destroying sprites. Flash/Flex has a garbage collector which handles the destruction of unused resources. Should I create an object pool to reuse them, or should I leave flash/flex to handle the creation/destruction of objects?
Which option is better from the performance point of view?

Leave it to the Flash player to handle and only worry about optimisation if there turns out to be a performance problem. Its all hypothetical until then anyway.

Object Pooling is an excellent idea if you need to create/destroy lots of items. Flex 4 is doing this with their DataGroup for managing creating destroying itemRenderers while you scroll through huge lists, and it dramatically improves rendering performance.
And all of the particle frameworks HAVE to use this to be competitive.
Check out these Object Pooling Demos and Presentations for Actionscript:
Object Pooling in AS3 by Lost in Actionscript.
Polygonal Labs' ObjectPool class and some examples.
Object Pools prevent "stuttering" example.
Grant Skinner describes some bits about Object Pooling in his Presentation, among other neat things.
I'd recommend using an object pool if you're creating/destroying lots of objects. Performance optimizations are very important early on if you're building a complex project and you won't have time to go back to it for a while. Object Pooling goes a long way and is very easy to implement.
Hope that helps,
Lance

Related

Whether n tier architecture will slow down application performance

When we are creating separate classes means (tiers) will take more time to traverse between pages.Whether this is the case?
I know there is many advantages for using the tiered architecture.Like easy to maintain,easy to understand.
What is actual scenario ?
To answer your question, class will take performance hit as they will be destroyed by GC which in itself is a heavy operation.
Architecture is something which helps you achieve the business requirements with maximum capacity to scale. Now adding a tier in your application would definitely bring in a class, the object of which will be created and again after use, it will be destroyed by garbage collector. So there is a very thin line on what to use. You have to decide it according to your business and scalability.
Creating and destroying classes takes time, but in an OO language this is unavoidable.
As far as .NET is concerned it makes no difference, performance wise whether these classes are in the code behind files of ASP pages or in a separate library.

what techniques should be used to handle objects in asp.net Cache to prevent problems from multiple asp.net worker threads access?

since asp.net contains multiple threads that are executing at the same time.
so if 2 threads access an object (simple or complex) that i got from the the asp.net httpcontext Cache.
can't this lead to state problems on that object if these 2 theads tried to modify/read it at the same time?
so what kind of precautions should i implement?
for example i am thinking maybe locking the object while working with it? (wont this cause performance problems?)
or maybe when i retrieve some object from the cache i should create a copy from it?
or maybe i dont need to worry about this issue at all?
thanks
You need to decide this based on the context of your problem, a one size fits all solution won't work here. If you are only reading data, then you will have no threading issues. If you are writing data to this frequently, its pointless using the cache. If its a bit of a mixture and caching does help with performance etc, then you either need to resort to normal thread synchronisation techniques (e.g. reader writer locks) or perhaps make your object immutable where changes to your object always create a new object. That choice leads to threading problems of its own as the new object has to then replace the old object in cache.
In 5+ years of ASP.NET development projects, I've never come across a situation where this was a worry.
That being said... you're certainly not going to have a problem reading any items. If you have an object that you want to modify often, why is it in the cache to begin with? If you don't need to modify it often, then locking the object won't be a performance problem.

Best Practices for large Flex apps?

I'm in the middle of creating a fairly large flex application, and over time, it's started to edge toward unmaintainability.
I'm using 3 external library projects which are still small enough to remain maintainable and reusable, but the main project seems to be impossible to keep organized.
Part of the problem seems to be that I have about 30 objects inheriting from a single abstract superclass type object. All of child objects have both a logic component and a ui component which are tightly integrated to each other. The superclass object has about 60 shared methods and properties, most of which can be overridden in any of the child classes, a few of which should be overridden in all child classes.
To add to the complexity, these have to communicate between themselves, and this is usually via the container object they reside in. Additionally, the main project has to create value objects out of these so they can be sent to a FlourineFX backend for storage, and additional authentication/authorization logic.
I've created much larger projects in languages from old MS BASIC (pre VB), Ada, VB (3 to .Net 1), C++, and C# without this problem. (well, old VB tended toward this problem because of the same tight integration between UI and logic) So, is there any thing I'm missing, or is there any best practices that I can implement? (even if that means rewriting entire swaths of code)
And yes, this may be an extension to this conversation.
Do you use any framework implementations in this project? A framework would help modularise a lot of this complexity and hopefully remove a lot of the dependencies you seem to have between the application logic and views.
I'm a massive advocate of the RobotLegs framework which implements the mvcs pattern and offers dependency injection for use throughout your project. There are others out there such as pureMvc, Cairngorm, Mate. Have a look around and see which best suits your project.
It sounds to me like you really need to do a big refactor which is a risky process in such a large project. It could be well worth it if you're struggling to maintain it. If you are going to refactor definitely refactor into a framework. It's probably the area that will give you most bang for your buck (pound for the brits ;) )
James Hay's conversation starter is a good one, but for HUGE applications I would take time to test and consider memory management for some of the suggestions in that answer/conversation. RobotLegs is great and all, but I would worry about 'over-singletonization' and potential memory management issues that it would create (though I have to admit that I've never used and avoided robotLegs because of it's use of singletons).
If you were thinking IoC and dependency injection (like that which robotLegs provides), I'd suggest a look at swiz -- I really like the new 'instance-direction' swiz has taken. My only issue with it (in the current beta) is they have some cleanup issues, though these issues are easy enough to remedy (look through their source and any time you completely remove a component from the stage you'll have to play the profiling game and make sure everything is getting cleaned up --- we had to create temp functions to remove the changewatchers and destroy 'display list bean instances' until they get that stuff fixed).
The project I lead had many of the potential issues you must be worried about. Our ERP app has thousands of modules and the thing is running on client machines for hours/days at a time, constantly loading and unloading modules. Garbage collection and memory management were and is THE issues.
As for using mate, the annoying carhorn, or pureMVC, we created our own framework two years ago. It borrowed ideas from cairngorm, but overall my suggestion is to use whatever you can quickly learn, understand and teach while thinking about garbage collection. Our internal Model and View classes now use swiz (for newly developed modules) and this has made maintainability and code readability super smooth.
I hope my blabbing has helped at least a bit.
Best of luck.
It seems like you just need a clean separation of UI and domain components. Look into the component guidelines and the Presentation Patterns discussed by Martin Fowler, especially the Presentation Model.
To bring these pieces together, you might want to use an IoC container like Spring ActionScript. This is a non-intrusive framework that allows you to keep layers separated.
Don't let a framework get in your way. I've seen massive misuse of frameworks like PureMVC and Cairngorm mainly because apply them in an all-or-nothing fashion.

Any UML-like modeling tool for QT signals and slots?

Is it any uml-like modeling tool available that can design (draw) classes and can visually represent QT signals and slots, they connections?
The signal/slog mechanism is essentially a mechanism for registering callbacks. So your question could be paraphrased as: "How do I model callbacks in UML". I'm not sure if there is a good answer since callbacks are not really an object oriented construction. Conceptually the observer pattern would be closest.
You can try Enterprise Architect as it supports UML 2.1 and allow to create user defined diagrams.
These connections are dynamic, so I'm not sure it's even possible to represent them in a static way (as in a diagram).
Also, most often, they are tightly bound in time and code (i.e. you create two objects and then connect them). From the code, it should be pretty obvious what happens and why, making any extra documentation dangerous (since the best it could do was to document the current state and it would always be in danger to be out of date unless it was generated from the source, or rather from data gathered during the runtime of the application).

Should I cache instances of frequently accessed classes

New to .net and was wondering if there is a performance gain to keeping an instance of, for example a DAL object in scope?
Coming from the Coldfusion world I would instanciate a component and store it in the application scope so that every time my code needed to use that component it would not have to be instanciated over and over again effecting performance.
Is there any benefit to doing this in ASP.Net apps?
Unless you are actually experiencing a performance problem, than you need not worry yourself with optimizations like this.
Solve the business problems first, and use good design. As long as you have a decent abstraction layer for your data access code, then you can always implement a caching solution later down the road if it becomes a problem.
Remember that any caching solution increases complexity dramatically.
NO. In the multi-tier world of .asp this would be considered a case of "premature optimization". Once a sites suite of stubs, scripts and programs has scaled up and been running for a few months then you can look at logs and traces to see what might be cached, spawned or rewritten to improve performance. And as the infamous Jeff Atwood says "Most code optimizations for web servers will benifit from money being spent on new and improved hardware rather than tweaking code for hours and hours"
Yes indeed you can and probably should. Oftentimes the storage for this is in the Session; you store data that you want for the user.
If it's a global thing, you may load it in the Application_Start event and place it somewhere, possibly the HttpCache.
And just a note, some people use "Premature Optimisation" to avoid optimising at all; this is nonsense. It is reasonable to cache in this case.
It is very important to do the cost benefit analysis before caching any object, one must consider all the factors like
Performance advantage
Frequency of use
Hardware
Scalability
Maintainability
Time available for delivery (one of the most important factor)
Finally, it is always useful to cache object which are very costly to create or you are using very frequently i.e. Tables's Data (From DB) or xml data
Does the class you are considering this for have state? If not, (and DAL classes often do not have state, or do not need state), then you should make it's methods static, and then you don't need to instantiate it at all. If the only state it holds is a connection string, you can also make that property field a static property field, and avoid the requirement of instantiating it that way.
Otherwise, take a look at the design pattern called Flyweight

Resources