Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The question is asked regarding an environment with garbage collection.(Java, AS3)

I am writing a level editor using MVC and the view needs to have read only access to the data in the model for rendering purposes. This can be done in AS3 in several ways:

  1. With a function that retrieves the contents of each specific object by index.
  2. Updating with all the recently modified objects(complicated), this requires keeping the id of the view and the last update date. Keeping the objects sorted by the last change date and then pushing in all the newly changed objects.
  3. Returning a copy of the whole array every time.

What is the most effective way to handle a read only array(not implemented in the language)? Does anyone have performance statistics that shows the performance boost gained for using method 2? And when is method 1 severely inferior to method 3.

share|improve this question

closed as not constructive by Byte56, doppelgreener, Josh Petrie, Trevor Powell, Nate Nov 14 '12 at 17:52

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

Perhaps you could limit your objects to getters only. That way you can't change the value of your object. – Sidar Oct 19 '12 at 11:15
The answer to this question depends entirely on your specific use case. "What is the most effective" is a very broad question. – Justin Skiles Oct 19 '12 at 11:48
@JustinSkiles I do not agree, I added an explanation why. – zehelvion Oct 21 '12 at 21:01
I really do not understand where you are going with this question. Where did you come up with the 30% number? What function calls are you talking about when comparing to indexing into an array? What is your specific use case that would cause you to ask this question? There's a reason your question has -4 votes and addressing a comment by editing your question didn't help much. You'll also notice that the highest ranked answer to this question includes language such as, " depends...". The reason is because your question is very generic, broad, and non-specific. Explain your scenario. – Justin Skiles Oct 21 '12 at 22:33
@JustinSkiles It is a specific data handling question. I added detail about the situation. I do not think it is important. It is just a performance issue of identifying when the passing of read only data becomes a bottleneck and should be adjusted. – zehelvion Oct 22 '12 at 20:35
up vote 3 down vote accepted

Well typically, garbage collection speed and efficiency depends on the object count and size. Returning a full array deep copy is requires both more memory and and time, and creates more new objects. To me, creating a single object copy is more efficient.

However there are most probably exceptions for some particular situations (accessing same index multiple times for example).

share|improve this answer
What if you end up accessing all the object either way? – zehelvion Oct 19 '12 at 10:50
I think that single object copy will be also more efficient since 1) it doesn't as costly as full array copy on creation, and 2) some early accessed objects can be reclaimed by GC in the meantime. – Petr Abdulin Oct 19 '12 at 10:55
I agree too but it also depends on the type of values stored inside the array. For example, a copy of an array of String will take as much memory as the original one because Strings are always copied by value and not by reference. – Antoine Lassauzay Oct 19 '12 at 12:19

Not a hint on performance but a suggestion for good practice and improvement.

You can have a look at the iterator design pattern which basically only provides an interface to loop over values.

share|improve this answer
Also, you could implement an immutable list (a set) your self by wrapping an Array inside another class, and use flash.util.Proxy as a parent class to define list behaviors. But I guess this will be slower for all array operations. – Antoine Lassauzay Oct 19 '12 at 12:17
The problem still remains, right? It returns the object in the array unless it's a primitive( like ints etc.). Then you just copy the value. – Sidar Oct 19 '12 at 12:18
Well, to me this is an possible implementation of a read-only list. That's your first question. Copying is another matter that as already been answered. – Antoine Lassauzay Oct 19 '12 at 12:23
Ah yeah well, the array will be read only. But the objects are still open for change ( It's not very clear though to me what he exactly wants ). He could just then wrap the array and make a getter without a setter. In AS3 this disallows for any change of the array. – Sidar Oct 19 '12 at 12:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.