DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Challenges of Creating Digital Twins in the Transition to Industry 4.0
  • 4 Best Practices for IoT OTA Updates
  • Introduction to JSON With Java
  • Your Go-to Guide to Develop Cryptocurrency Blockchain in Node.Js

Trending

  • LTS JDK 21 Features
  • How to Submit a Post to DZone
  • Spring Authentication With MetaMask
  • TDD With FastAPI Is Easy
  1. DZone
  2. Data Engineering
  3. Data
  4. Fast Reflection for Value Types

Fast Reflection for Value Types

Reflection is sometimes a necessary evil, so here's a way to take the sting out of the tail.

Josh Anderson user avatar by
Josh Anderson
·
Jan. 13, 16 · Tutorial
Like (2)
Save
Tweet
Share
3.02K Views

Join the DZone community and get the full member experience.

Join For Free

A long while back, I wrote an article on Reflection. 

C# to Objective-C - Part 6: Reflection

And the code in that article still holds true. As you know, reflection is really expensive but sometimes a necessary evil. However, it's especially expensive for value types because of boxing and unboxing. 

I wrote another article on this topic a while ago as well. 

Going From C# To Objective-C: Boxing and Unboxing

Basically, whenever you use reflection, Obj-c takes the value and wraps it in an NSValue object or an NSNumber if you're dealing with a number. 

Today, I'm going to show you, how you can actually skip the boxing and unboxing. Although it's a bit tricky, it'll definitely speed up your application if you're using reflection to loop through large amounts of data. 

To keep the topic as simple as possible, I'm going to show you how to do this for double's. However, you can customize this to support any other value type, although you'll have to know what value types you're supporting up front. 

First we need to typedef a method called "getDouble" that will return our value:

typedef double (*getDouble)(id, SEL);

Next we need to define a block called "helperBlock" that we will invoke for reflection:

typedef double (^helperBlock)(NSObject*, SEL, IMP);

Now we can create an instance of helperBlock

helperBlock block = ^double (NSObject* obj, SEL selector, IMP method)
{
getDouble f = (getDouble)method;
return f(obj, selector);
};

Notice how we're casing the IMP as our getDouble method, so that we know what the return type is. 

Let's create a dummy class that we'd use this against:


@interface ObjectToReflect

@property(nonatomic, assign)double value;
@property(nonatomic, assign)NSString* key;

@end

So how would we reflect and get the value property? Well, let's assume we created an array of "ObjectToReflect" objects. 

IMP method = nil;

for(NSObject* obj in data)
{
    if(method == nil)
       method = [obj methodForSelector:getProp];


    double val = block(obj, NSSelectorFromString(@“value”), method);
}

And now you've reflected against your data object. Like I said, its a bit complicated, but hopefully this helps simplify things. And it certainly will make your code much faster. 

Written by Stephen Zaharuk

Objective C Object (computer science) Data (computing) IT application Blocks Property (programming) Data structure

Published at DZone with permission of Josh Anderson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Challenges of Creating Digital Twins in the Transition to Industry 4.0
  • 4 Best Practices for IoT OTA Updates
  • Introduction to JSON With Java
  • Your Go-to Guide to Develop Cryptocurrency Blockchain in Node.Js

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: