O'Reilly logo

Swift High Performance by Kostiantyn Koval

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Value objects and reference objects

In the previous chapter, you learned the benefits of using immutable value objects. Value objects not only make code safer and clearer, they also make it faster. Value objects have better speed performance than reference objects and here is why. As an example of a value object, we will use structures in this chapter.

Memory allocation

Value objects can be allocated on the stack memory instead of the heap memory. Reference objects need to be allocated on the heap memory because they can be shared between many owners. Because value objects have only one owner they can be safely allocated on the stack. Stack memory is way faster that heap memory.

The second advantage is that value objects don't need reference counting ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required