Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

grandM

macrumors 68000
Original poster
Oct 14, 2013
1,551
309
So I was wondering if you want to use an array. Say this array would hold String instances. You could use optionals or you could just initialize it with an empty array. What is the memory impact of using the empty array?

I could use optionals but sometimes I just get fed up with all these question and exclamation marks. Or are optionals truly recommended?
 
This seems like a question best answered by writing a test program.

Start with a nil variable. How much memory does it take?
Create an empty array. How much memory does it take now?
Fill the array with 10,000 separate empty arrays. How much memory does it take now?
Add another 10,000 empty arrays. How much memory does it take now?

The raw numbers should give you an idea of the cost for 10k empty arrays.
Divide the numbers by 10k to get the per-array cost.
 
  • Like
Reactions: grandM
If you're concerned about memory usage my guess would be a fixed sized array non-mutable (both for size and content) would be the least memory.

There's also the issue of using memory to process things. So you have the memory foot print of the array and the memory used for processing things (unwrapping, etc).

Part of this depends on how the internals are setup for arrays that are allowed to change in content and size. You might not be able to find out if the memory pool gets chopped up because of different types of arrays. The chopped up part could happen when applying methods to arrays that would need more memory.

I think you'd have to create huge array and run some tests. Most advanced languages have people that have insight into these things, like C/C++, so you might look at using C arrays instead of objects and run tests on that.
 
  • Like
Reactions: grandM
So I was wondering if you want to use an array. Say this array would hold String instances. You could use optionals or you could just initialize it with an empty array. What is the memory impact of using the empty array?

I could use optionals but sometimes I just get fed up with all these question and exclamation marks. Or are optionals truly recommended?


Optionals are really recommended. They are here for security reasons aka find nil as soon as possible. I belive that usually there is rule that says "don't premature optimise code when there is no need to". And so far, there have not been reason enough to do workaround optionals. I don't belive that in grand scale of memory or performance issue, optionals play any major part. They do however play major part in finding bugs as soon as posible.

I do agree that they are ugly as hell.
 
Optionals are really recommended. They are here for security reasons aka find nil as soon as possible. I belive that usually there is rule that says "don't premature optimise code when there is no need to". And so far, there have not been reason enough to do workaround optionals. I don't belive that in grand scale of memory or performance issue, optionals play any major part. They do however play major part in finding bugs as soon as posible.

I do agree that they are ugly as hell.
I'm not an expert on optionals yet, but the part in bold would explain why Apple would put them in. In advanced programs, chasing bugs can be a real problem. Most can write simple programs and debug them, there's a special skill in debugging very large projects. I've seen many programmers that don't have that skill set.
 
  • Like
Reactions: grandM
If you want to avoid optionals, learn Java or something.

If you are using Java...every project I've ever worked on used the Guava Library and Optionals are a main foundation of the library: http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Optional.html

In point, Optionals are a great tool, you should absolutely learn how to use them effectively. By using the if let and guard let syntax, you can rid your code of nearly all question marks. The most naive way to deal with optionals is to implicitly unwrap them (!).
 
Rather than memory impact, I'd be concerned with reliability impact.

Optionals aren't just a feature tacked onto Swift, but are a foundational component (talking more about usage, but as it is essentially part of the typing system, true in the code sense). What you should be concerned with, as other have said, is the integrity of your code. Optionals were introduced to improve that. Essentially, in the case you gave, you are attempting to write your own null to save memory and keystrokes, which you most likely wouldn't in the long run. Swift is already doing its best to manage memory and provides you with an entire class of functionality best for that, so telling it theres an absence of a value allows it to work in the fashion that was intended and prevents logic that you'd otherwise have to account for or lose.

If you're fed up with using ?, then there are probably situations in which you can safely use ! to sidestep the safety - which I find much more manageable.
 
... Essentially, in the case you gave, you are attempting to write your own null to save memory and keystrokes, ...
For the description in post #1, I don't think that's so.

As I understand the description, he's basically comparing two cases:
1. A variable that can hold null or an array.
2. A variable that always holds an array, but the array can be empty (0 elements).

I further assume that a null in the variable is equivalent to an empty array, i.e. they both represent the "0 elements" state.

To be honest, I think #2 is simpler and clearer. That's the one he wants to use, too.

The code in #2 never has to check whether the variable is null, and never has to create an array on the fly to assign to the variable. It only has to deal with an empty array (0 elements). Adding null handling costs extra, since it means the same thing as a non-null but empty array. Why have two states that mean the same thing, unless there's a significant benefit?

There may be a memory cost for maintaining a 0-element array, but we don't know what that cost is (yet). Indeed, that's the question to consider. That cost may differ depending on how the array got to 0 elements. That is, a newly created array of 0 length may have a different memory footprint than an array that previously contained 10,000 elements and was then emptied (new vs. residual).

Maybe I'm missing something about how the array variable is expected to be used, but I'd rather maintain code where there are fewer states, with no overlap in the meaning (semantics) of those states.
 
Maybe I'm missing something about how the array variable is expected to be used, but I'd rather maintain code where there are fewer states, with no overlap in the meaning (semantics) of those states.

No, you are totally correct - I completely misunderstood the intention and it went over my head - I completely agree with what you've said. I'd prefer to maintain an empty collection over marking the collection as absent when empty mainly because I consider an array that has zero items and an nil pointer to an array as different situations with different intentions.

I was thinking along the lines of <String?> or <String> and passing nil or "" as values - so disregard what I was rambling about above because I am crazy.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.