Search by Algolia
What is online retail merchandising? An introduction
e-commerce

What is online retail merchandising? An introduction

Done any shopping on an ecommerce website lately? If so, you know a smooth online shopper experience is not optional ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

5 considerations for Black Friday 2023 readiness
e-commerce

5 considerations for Black Friday 2023 readiness

It’s hard to imagine having to think about Black Friday less than 4 months out from the previous one ...

Piyush Patel

Chief Strategic Business Development Officer

How to increase your sales and ROI with optimized ecommerce merchandising
e-commerce

How to increase your sales and ROI with optimized ecommerce merchandising

What happens if an online shopper arrives on your ecommerce site and: Your navigation provides no obvious or helpful direction ...

Catherine Dee

Search and Discovery writer

Mobile search UX best practices, part 3: Optimizing display of search results
ux

Mobile search UX best practices, part 3: Optimizing display of search results

In part 1 of this blog-post series, we looked at app interface design obstacles in the mobile search experience ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

Mobile search UX best practices, part 2: Streamlining search functionality
ux

Mobile search UX best practices, part 2: Streamlining search functionality

In part 1 of this series on mobile UX design, we talked about how designing a successful search user experience ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

Mobile search UX best practices, part 1: Understanding the challenges
ux

Mobile search UX best practices, part 1: Understanding the challenges

Welcome to our three-part series on creating winning search UX design for your mobile app! This post identifies developer ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

Teaching English with Zapier and Algolia
engineering

Teaching English with Zapier and Algolia

National No Code Day falls on March 11th in the United States to encourage more people to build things online ...

Alita Leite da Silva

How AI search enables ecommerce companies to boost revenue and cut costs
ai

How AI search enables ecommerce companies to boost revenue and cut costs

Consulting powerhouse McKinsey is bullish on AI. Their forecasting estimates that AI could add around 16 percent to global GDP ...

Michelle Adams

Chief Revenue Officer at Algolia

What is digital product merchandising?
e-commerce

What is digital product merchandising?

How do you sell a product when your customers can’t assess it in person: pick it up, feel what ...

Catherine Dee

Search and Discovery writer

Scaling marketplace search with AI
ai

Scaling marketplace search with AI

It is clear that for online businesses and especially for Marketplaces, content discovery can be especially challenging due to the ...

Bharat Guruprakash

Chief Product Officer

The changing face of digital merchandising
e-commerce

The changing face of digital merchandising

This 2-part feature dives into the transformational journey made by digital merchandising to drive positive ecommerce experiences. Part 1 ...

Reshma Iyer

Director of Product Marketing, Ecommerce

What’s a convolutional neural network and how is it used for image recognition in search?
ai

What’s a convolutional neural network and how is it used for image recognition in search?

A social media user is shown snapshots of people he may know based on face-recognition technology and asked if ...

Catherine Dee

Search and Discovery writer

What’s organizational knowledge and how can you make it accessible to the right people?
product

What’s organizational knowledge and how can you make it accessible to the right people?

How’s your company’s organizational knowledge holding up? In other words, if an employee were to leave, would they ...

Catherine Dee

Search and Discovery writer

Adding trending recommendations to your existing e-commerce store
engineering

Adding trending recommendations to your existing e-commerce store

Recommendations can make or break an online shopping experience. In a world full of endless choices and infinite scrolling, recommendations ...

Ashley Huynh

Ecommerce trends for 2023: Personalization
e-commerce

Ecommerce trends for 2023: Personalization

Algolia sponsored the 2023 Ecommerce Site Search Trends report which was produced and written by Coleman Parkes Research. The report ...

Piyush Patel

Chief Strategic Business Development Officer

10 ways to know it’s fake AI search
ai

10 ways to know it’s fake AI search

You think your search engine really is powered by AI? Well maybe it is… or maybe not.  Here’s a ...

Michelle Adams

Chief Revenue Officer at Algolia

Cosine similarity: what is it and how does it enable effective (and profitable) recommendations?
ai

Cosine similarity: what is it and how does it enable effective (and profitable) recommendations?

You looked at this scarf twice; need matching mittens? How about an expensive down vest? You watched this goofy flick ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

What is cognitive search, and what could it mean for your business?
ai

What is cognitive search, and what could it mean for your business?

“I can’t find it.”  Sadly, this conclusion is often still part of the modern enterprise search experience. But ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

Looking for something?

iOS: When Automatic Reference Counting is a Bad Idea
facebookfacebooklinkedinlinkedintwittertwittermailmail

I started developing for iOS in 2009 by learning about the Objective-C language. At that time ARC (Automatic Reference Counting) was not yet available and developers were responsible for alloc/release/autorelease. I found it pretty straightforward as it was very similar to C++ and the resulting code was very self-explanatory.

When ARC was released at the end of 2011 it made a great impression on me and looked like the perfect feature for any programmer coming from the Java world who was not familiar with memory management. I started using ARC and discovered a major flaw that completely changed my mind. Here is an example :

[Edit 28-Jan-2013] This post describes a bug in ARC that was fixed in Xcode 4.4.[/Edit]
Let’s start with an example in C++, the sample contains a constructor that allocates two vectors, a destructor that destroys them and a compute method that just performs a swap between the two vectors. This code is perfectly valid and a swap is the perfect operation when you have two sets of data to maintain because this is very efficient (just two pointers copy, no data copy).

class Example {
public:
  Example() {
    nextPositions = new std::vector<int>();
    currentPositions = new std::vector<int>();
  }
  ~Example() {
    delete nextPositions;
    delete currentPositions;
  }

  void compute() {
    // some code here
    std::swap(nextPositions, currentPositions);
    // some other code here
  }

private:
  std::vector<int>* nextPositions;
  std::vector<int>* currentPositions;
};

Before ARC the objective-C code was very similar and perfectly valid:

// headers
@interface Example : NSObject {
  NSMutableArray* nextPositions;
  NSMutableArray* currentPositions;
}
-(void) compute;
@end

// implementation
@implementation Example
-(id) init {
  self = [super init];
  if (!self)
    return nil;
  nextPositions = [[NSMutableArray alloc] init];
  currentPositions = [[NSMutableArray alloc] init];
  return self;
}

-(void) dealloc {
  [super dealloc];
  [nextPositions release];
  [currentPositions release];
}

-(void) compute
{
  // some processing stuff here
  NSMutableArray* tmp = nextPositions;
  nextPositions = currentPositions;
  currentPositions = tmp;
  // some processing stuff here
}
@end

The semantics are very clear, it is exactly the same as in C++.

So when you start using ARC you tend to do exactly the same thing with less code, and you just delete the dealloc method:

// headers
@interface Example : NSObject {
  NSMutableArray* nextPositions;
  NSMutableArray* currentPositions;
}
-(void) compute;
@end

// implementation
@implementation Example
-(id) init {
  self = [super init];
  if (!self)
    return nil;
  nextPositions = [[NSMutableArray alloc] init];
  currentPositions = [[NSMutableArray alloc] init];
  return self;
}

-(void) compute {
  // some processing stuff here
  NSMutableArray* tmp = nextPositions;
  nextPositions = currentPositions;
  // Wrong, at that step this is too late! nextPositions was deallocated
  currentPositions = tmp;
  // some processing stuff here
}
@end

The error does not come from a missing strong attribute, because instance variables are strong by default. The problem is that ARC does not generate code in the variable assignation. You should explicitely add properties and use “self.variableName” notation like in the next example. I would encourage ARC designers to read this excellent article by Joel Spolsky “Making Wrong Code Look Wrong”. This ARC usage is a perfect example of wrong code that looks perfect but leads me to the conclusion that ARC is not trustworthy.

Here is the correct version:

// headers
@interface Example : NSObject {
  NSMutableArray* nextPositions;
  NSMutableArray* currentPositions;
}
-(void) compute;
@property (strong, nonatomic) NSMutableArray* nextPositions;
@property (strong, nonatomic) NSMutableArray* currentPositions;
@end

// implementation
@implementation Example
-(id) init {
  self = [super init];
  if (!self)
    return nil;
  nextPositions = [[NSMutableArray alloc] init];
  currentPositions = [[NSMutableArray alloc] init];
  return self;
}

-(void) compute {
  // some processing stuff here
  NSMutableArray* tmp = self.nextPositions;
  self.nextPositions = self.currentPositions;
  self.currentPositions = tmp;
  // some processing stuff here
}
@synthesize nextPositions;
@synthesize currentPositions;
@end

I am surprised Apple hasn’t corrected that flaw yet. This is a major issue as ARC does not generate code for variable affectation like it does for properties (if one of you reads this post and has a complete explanation, please tell me!):

  • Is it because of a performance issue? I would prefer to have no ARC at all than to see such a behavior.
  • Is this case too complex to be handled? The problem is it undermines ARC’s utility and might get stuck at the prototype stage.

To me, this is a perfect example of a technology driven product. The engineers know their product so well that they forget to step back and look at it with a fresh eye to analyse the full complexity of their system.

And that leaves me with two important lessons we’ll try to apply to our products:

  • Always organize user tests, even when your users are developers themselves!
  • Always keep a fresh eye when trying to simplify a product (this one may prove particularly difficult!)

I hope you’ll tell us when we will (inevitably) break these rules!

About the author
Julien Lemoine

Co-founder & former CTO at Algolia

githublinkedintwitter

Recommended Articles

Powered byAlgolia Algolia Recommend

Painless integration, crystal clear documentation, please welcome Algolia Search beta 4!
product

Nicolas Dessaigne

Co-founder & board member at Algolia

“Help yourself!” Avoid these 7 mistakes and rock your self-serve customer service
product

Catherine Dee

Search and Discovery writer

C/C++ is still the only way to have high performance on Mobile
engineering

Julien Lemoine

Co-founder & former CTO at Algolia