一、介绍
从64bit开始,iOS引入了 Tagged Pointer 技术,用于优化NSNumber、NSDate、NSString等小对象的存储。在没有使用Tagged Pointer之前, NSNumber等对象需要动态分配内存、维护引用计数等,NSNumber指针存储的是堆中NSNumber对象的地址值。使用Tagged Pointer之后,NSNumber指针里面存储的数据变成了:Tag + Data(标记类型+数据),也就是将数据直接存储在了指针中,当指针(8字节)不够存储数据时,才会使用动态分配内存的方式来存储数据。
二、未引入前后对比
image.png image.pngNSNumber *number = @5;
没使用之前一个number指针指向一个NSNumber对象,指针存储的是NSNumber对象的内存地址,NSNumber对象储存值是10。指针8个字节,NSNumber对象16个字节(分配16字节默认只使用了8个字节),总共24个字节太消耗资源了,所以引入了 Tagged Pointer,引入后指针大概就是图上这样,是不是完全看不懂应该是做了混淆和一些运算防(以前没有这么复杂可能就是0x527,5就是值,27可能代表NSNumber类型),值和类型隐藏在地址其中,源码当中其实也解释了。
* Tagged pointer objects.
* /// Tagged pointer对象将类和对象值存储在对象指针中; “指针”实际上并不指向任何东西。
* Tagged pointer objects store the class and the object value in the
* object pointer; the "pointer" does not actually point to anything.
*
* Tagged pointer objects currently use this representation:
* (LSB)
* 1 bit set if tagged, clear if ordinary object pointer
* 3 bits tag index
* 60 bits payload
* (MSB)
* The tag index defines the object's class.
* The payload format is defined by the object's class.
*
* If the tag index is 0b111, the tagged pointer object uses an
* "extended" representation, allowing more classes but with smaller payloads:
* (LSB)
* 1 bit set if tagged, clear if ordinary object pointer
* 3 bits 0b111
* 8 bits extended tag index
* 52 bits payload
* (MSB)
*
* Some architectures reverse the MSB and LSB in these representations.
*
* This representation is subject to change. Representation-agnostic SPI is:
* objc-internal.h for class implementers.
* objc-gdb.h for debuggers.
三、如何判断一个指针是否是Tagged Pointer
#if OBJC_MSB_TAGGED_POINTERS
# define _OBJC_TAG_MASK (1UL<<63)
#else
# define _OBJC_TAG_MASK 1UL
static inline bool _objc_isTaggedPointer(const void * _Nullable ptr)
{ /// _OBJC_TAG_MASK = 1
return ((uintptr_t)ptr & _OBJC_TAG_MASK) == _OBJC_TAG_MASK;
}
用地址值 & 上一个_OBJC_TAG_MASK值
思考以下2段代码能发生什么事?有什么区别?
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"0---:%s",__func__);
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
for (int i = 0; i<10000; i++) {
dispatch_async(queue, ^{
/// NSTaggedPointerString
self.name = [NSString stringWithFormat:@"012345678"];
});
}
NSLog(@"1---:%s",__func__);
for (int i = 0; i<10000; i++) {
dispatch_async(queue, ^{
/// NSCFString
self.name = [NSString stringWithFormat:@"0123456789"];
});
}
NSLog(@"2---:%s",__func__);
}
/// 打印如下
2022-02-11 17:23:28.264620+0800 OCMJTestDemo[1348:483309] 0---:-[ViewController touchesBegan:withEvent:]
2022-02-11 17:23:28.586147+0800 OCMJTestDemo[1348:483309] 1---:-[ViewController touchesBegan:withEvent:]
2022-02-11 17:23:28.979591+0800 OCMJTestDemo[1348:483309] 2---:-[ViewController touchesBegan:withEvent:]
2022-02-11 17:23:28.991820+0800 OCMJTestDemo[1348:483322] *** -[CFString release]: message sent to deallocated instance 0x28028e240
下面的那块循环代码大概率会crash,报错坏内存,因为是 NSCFString 类型,理论上复制name的代码是这样的,因为是异步线程访问,所以是有多个线程同时调用到_name = nil; 所以会出现坏内存访问,使用了已经释放的对象。Tagged Pointer 类型就不会出现这种情况,因为它没有指向实例对象,不会调用set方法,值都在指针里面。
- (void)setName:(NSString *)name {
if (_name != name) {
_name = nil; /// [_name release];
_name = [name copy];
}
}
网友评论