目标C类方法与C函数 [英] Objective C Class Methods vs C Functions

查看:157
本文介绍了目标C类方法与C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开源项目上工作时,我遇到了下面的C函数声明和实现:

  // FSNData。 h 
NSString * stringForMimeType(MimeType type);

@interface FSNData:NSObject
//所有预期的objective-c属性和实例方法声明
@end

// FSNData.m
#importFSNData.h

//其中'type'是一个枚举
//这可以按预期工作
NSString * stringForMimeType(MimeType type){
switch(type){
case MimeType_image_jpeg:return @image / jpeg;
case MimeType_image_png:return @image / png;
默认值:
NSLog(@ERROR:FSNData:unknown MimeType:%d,type);

//不返回application / octet-stream;相反,让接收者猜测
// http://en.wikipedia.org/wiki/Internet_media_type
return nil;



@implementation

// FSData.h中定义的所有属性和方法按预期执行

@end

这个例子很容易被重写为一个没有任何问题的类级方法。实际上,使用 stringFormMimeType() sill需要导入 FSNData 头文件。



查看


因为Objective- C基于ANSI C的基础上,您可以自由地
将直接C代码与Objective-C代码混合在一起。此外,您的代码
可以调用在非Cocoa编程接口中定义的函数,例如
作为/ usr / include中的BSD库接口。

没有提及C函数何时应该支持Objective-C方法。

我现在看到的唯一好处是调用上述函数,而不是类方法,一些Objective-C运行时调用将被跳过。在 FSNData 的典型用例中,这不会显着提升用户的性能(可能甚至包括开发人员)。*。



有什么好处(除了编码风格)用于支持C函数而不是类方法?

* FSNData 被用作 FSNetworking 图书馆的一部分,所以我怀疑会有成千上万的图书馆(或C ++)的实现非常有用:

/ p>


  • 用于抽象

  • 用于可重用性

  • 和大型程序

  • 在性能关键路径中

  • 对于'Interior'实现




有利于C函数的优点(编码风格除外)o是类方法吗?




  • ObjC消息传递引入了间接函数调用。这些是优化器的防火墙。
    C函数可以轻松限制访问,而'私人'ObjC实现可以使用ObjC运行时查找,或者意外覆盖。
  • $ b $如果未被引用,b
  • C函数可能会从您的可执行文件中删除,或者它们可能会变为私有。如果您编写可重用代码(并且您应该),这会对您的二进制大小和加载时间产生巨大影响 - 未被引用/使用的C函数可能会被删除,但ObjC类型和方法将被保留(包括它们的所有内容参考)。这就是为什么当您仅使用ObjC静态库的一小部分时,应用程序的二进制大小可能会显着增加 - 库中的每个objc类都将保留。如果该库是C或C ++,那么你可以通过非常小的增长来获得,因为你只需要引用它。什么是或未被引用更容易用C和C ++证明。

  • C函数可以在编译期间或链接时间优化阶段内联。

  • 编译器和优化器能够使用C函数(例如,程序间优化)做很多优化,但是对于ObjC方法来说很少,因为它们总是间接的。
  • 为了避免ObjC消息调度开销(如您所述)
  • 在与ObjC对象交互时,可能需要额外的引用计数操作和自动释放池活动。


    当然,你不会总是为不需要或不需要的东西付钱 - 并且记住ObjC类方法对C函数也有一些好处。因此,只需将C或C ++实现视为工具箱中的另一个工具即可。随着复杂性和项目规模的增加,我发现它们非常有用,它们可以用来使您的程序更快。只要做你最不可能在2015年后悔的事;)

    While working on on open source project, I came across the following C function declaration and implementation:

    // FSNData.h
    NSString *stringForMimeType(MimeType type);
    
    @interface FSNData : NSObject
    // All the expected objective-c property and instance method declarations
    @end
    
    // FSNData.m
    #import "FSNData.h"
    
    // where 'type' is an enum
    // this does work as expected
    NSString *stringForMimeType(MimeType type) {
        switch (type) {
            case MimeType_image_jpeg: return @"image/jpeg";
            case MimeType_image_png:  return @"image/png";
            default:
                NSLog(@"ERROR: FSNData: unknown MimeType: %d", type);
    
            // do not return "application/octet-stream"; instead, let the recipient guess
            // http://en.wikipedia.org/wiki/Internet_media_type
            return nil;
        }
    }
    
    @implementation
    
    // all properties and methods defined in FSData.h implemented as expected
    
    @end
    

    This example could easily be re-written as a class level method with out any problem. As it is, using stringFormMimeType() sill requires importing the FSNData header file anyway.

    Looking at the Apple docs, it states only:

    Because Objective-C rests on a foundation of ANSI C, you can freely intermix straight C code with Objective-C code. Moreover, your code can call functions defined in non-Cocoa programmatic interfaces, such as the BSD library interfaces in /usr/include.

    There is no mention of when C functions should favour Objective-C methods.

    The only benefit I can see at this point, is that calling the above function, as opposed to a class method, some Objective-C runtime call(s) would be skipped. In a typical use case of FSNData, this would not give a noticeable boost in performance to the user (probably even to developers)*.

    What benefit exists (other than coding style) for favouring a C function over a class method?

    *FSNData is used as part of the FSNetworking library, so I doubt there would be thousands upon thousands of network operations being performed during any application's life cycle.

    解决方案

    In short, C (or C++) implementations are very useful:

    • For Abstraction
    • For Reusability
    • When making medium and large scale programs
    • In performance critical paths
    • For 'Interior' implementations

    What benefit exists (other than coding style) for favouring a C function over a class method?

    • ObjC messaging introduces indirect function calls. These are firewalls for optimizers.
    • C functions can easily restrict access, whereas 'private' ObjC implementations may be looked up using the ObjC runtime, or accidentally overridden.
    • C functions may be removed from your executable if not referenced, or they may be made private. If you write reusable code (and you should), this can have a huge impact on your binary sizes and load times -- C functions which are not referenced/used may be removed, but ObjC types and methods will be preserved (including everything they reference). This is why your app's binary size may grow significantly when you use only small part of an ObjC static library -- every objc class in the library is preserved. If that library were C or C++, then you could get by with very small growth because you need only what is referenced. What is or is not referenced is easier to prove with C and C++.
    • C functions can be inlined, either during compilation or during Link Time Optimization phases.
    • The compiler and optimizers are able to do much optimization with C functions (e.g. inter-procedural optimizations), but very little with ObjC methods because they are always indirect.
    • To avoid ObjC message dispatch overhead (as you mentioned)
    • Potential for additional reference counting operations and autorelease pool activity when interacting with ObjC objects.

    Of course you won't always hurt paying for things you don't need or use -- and remember that ObjC class methods have some benefits over C functions, too. So, just look at C or C++ implementations as another tool in your toolbox. I find them very useful as complexity and project sizes increase, and they can be used to make your programs much faster. Just do what you are least likely to regret in 2015 ;)

    这篇关于目标C类方法与C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆