最新消息:XAMPP默认安装之后是很不安全的,我们只需要点击左方菜单的 "安全"选项,按照向导操作即可完成安全设置。

OC底层原理实现 cache_t 结构方法缓存

XAMPP教程 admin 457浏览 0评论

在前面几篇文章中 我们分别探索了 objc_class 中的 isa , superClass , bits. 现在我们来看看 cache_t 中到底有什么作用

一 . cache_t 的结构

在这段类结构代码中,我们可以看到 类结构中存在一个cache_t

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct objc_class : objc_object {
// Class ISA;
Class superclass;
cache_t cache;             // formerly cache pointer and vtable
class_data_bits_t bits;    // class_rw_t * plus custom rr/alloc flags

class_rw_t *data() const {
return bits.data();
}
void setData(class_rw_t *newData) {
bits.setData(newData);
}
… 省略
}

下面是部分cache_t的结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#if CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_OUTLINED // macOS 模拟器
explicit_atomic<struct bucket_t *> _buckets;
explicit_atomic<mask_t> _mask;
#elif CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_HIGH_16 // 64 位真机
explicit_atomic<uintptr_t> _maskAndBuckets;
mask_t _mask_unused;

// How much the mask is shifted by.
static constexpr uintptr_t maskShift = 48;

// Additional bits after the mask which must be zero. msgSend
// takes advantage of these additional bits to construct the value
// `mask << 4` from `_maskAndBuckets` in a single instruction.
static constexpr uintptr_t maskZeroBits = 4;

// The largest mask value we can store. 可以存储的最大掩码值 2^16-1
static constexpr uintptr_t maxMask = ((uintptr_t)1 << (64 – maskShift)) – 1;

// The mask applied to `_maskAndBuckets` to retrieve the buckets pointer.
static constexpr uintptr_t bucketsMask = ((uintptr_t)1 << (maskShift – maskZeroBits)) – 1;
// Ensure we have enough bits for the buckets pointer.
static_assert(bucketsMask >= MACH_VM_MAX_ADDRESS, “Bucket field doesn’t have enough bits for arbitrary pointers.”);
#elif CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_LOW_4 // 非64位真机
// _maskAndBuckets stores the mask shift in the low 4 bits, and
// the buckets pointer in the remainder of the value. The mask
// shift is the value where (0xffff >> shift) produces the correct
// mask. This is equal to 16 – log2(cache_size).
explicit_atomic<uintptr_t> _maskAndBuckets;
mask_t _mask_unused;

static constexpr uintptr_t maskBits = 4;
static constexpr uintptr_t maskMask = (1 << maskBits) – 1;
static constexpr uintptr_t bucketsMask = ~maskMask;
#else
#error Unknown cache mask storage type.
#endif

#if __LP64__
uint16_t _flags;
#endif
uint16_t _occupied;

由于 主要我们实在 Mac 上调试, 所以最终的 cache_t 的结构 具有四个参数

 

1
2
3
explicit_atomic<struct bucket_t *> _buckets; // 存储方法的 hash 表
explicit_atomic<mask_t> _mask; // 算法使用的掩码
uint16_t _occupied; // hash 表中占用的数量

其中主要是看 bucket_t 的结构
我们可以看到 真机和 模拟器的区别就是。 imp 和 sel 的顺序问题
看注释我们可以知道 这样调整顺序是在相应的架构下 有更好的优化

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
struct bucket_t {
private:
// IMP-first is better for arm64e ptrauth and no worse for arm64.
// SEL-first is better for armv7* and i386 and x86_64.
#if __arm64__ // 真机
explicit_atomic<uintptr_t> _imp; // 函数指针,指向方法的具体实现
explicit_atomic<SEL> _sel; // 方法编号
#else // 模拟器
explicit_atomic<SEL> _sel;
explicit_atomic<uintptr_t> _imp;
#endif

// Compute the ptrauth signing modifier from &_imp, newSel, and cls.
uintptr_t modifierForSEL(SEL newSel, Class cls) const {
return (uintptr_t)&_imp ^ (uintptr_t)newSel ^ (uintptr_t)cls;
}

// Sign newImp, with &_imp, newSel, and cls as modifiers.
uintptr_t encodeImp(IMP newImp, SEL newSel, Class cls) const {
if (!newImp) return 0;
#if CACHE_IMP_ENCODING == CACHE_IMP_ENCODING_PTRAUTH
return (uintptr_t)
ptrauth_auth_and_resign(newImp,
ptrauth_key_function_pointer, 0,
ptrauth_key_process_dependent_code,
modifierForSEL(newSel, cls));
#elif CACHE_IMP_ENCODING == CACHE_IMP_ENCODING_ISA_XOR
return (uintptr_t)newImp ^ (uintptr_t)cls;
#elif CACHE_IMP_ENCODING == CACHE_IMP_ENCODING_NONE
return (uintptr_t)newImp;
#else
#error Unknown method cache IMP encoding.
#endif
}

二. cache_t 方法缓存的添加

  • LLDB 调试准备 我们先建立一个测试类,其中添加一些实例方法
    eg.
1
2
3
4
– (void)logCup;
– (void)logPen;
– (void)logKeyboard;
– (void)logMouse;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here…
TObject *t = [TObject alloc];
NSLog(@”%p”, t);

[t logCup];
[t logPen];
[t logMouse];
[t logKeyboard];

}
return 0;
}

  1. 我们在初始化之前先下个断点 进行 lldb 调试 ,由下面的调试我们可以知道 缓存 内都是空的,并没有任何方法的痕迹
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(lldb) p/x TObject.class // 获取 class 类对象地址
(Class) $0 = 0x0000000100002318 TObject
(lldb) p (cache_t*)0x0000000100002328 // 通过地址偏移16字节,强转 cache_t 类型
(cache_t *) $1 = 0x0000000100002328
(lldb) p *$1
(cache_t) $2 = {
_buckets = {
std::__1::atomic<bucket_t *> = 0x000000010032d420 {
_sel = {
std::__1::atomic<objc_selector *> = (null)
}
_imp = {
std::__1::atomic<unsigned long> = 0
}
}
}
_mask = {
std::__1::atomic<unsigned int> = 0
}
_flags = 32804
_occupied = 0
}
  1. 接下来 我们在调用 alloc 之后下一个断点 继续调试
    我们可以看到 _buckets 缓存数据发生了改变
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(lldb) p/x t.class
(Class) $6 = 0x0000000100002318 TObject
(lldb) p (cache_t*) 0x0000000100002328
(cache_t *) $7 = 0x0000000100002328
(lldb) p *$7
(cache_t) $8 = {
_buckets = {
std::__1::atomic<bucket_t *> = 0x0000000101004bf0 {
_sel = {
std::__1::atomic<objc_selector *> = “”
}
_imp = {
std::__1::atomic<unsigned long> = 3274184
}
}
}
_mask = {
std::__1::atomic<unsigned int> = 3
}
_flags = 32804
_occupied = 2
}
(lldb)
  1. 我们在每个方法都打个断点,当调用完第一个方法之后我们进行调试, 我们看到 _occupied 又变成 1 了,那这到底做了什么呢?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2020-12-21 23:54:11.872707+0800 Objc_m [2835:50950] -[TObject logCup]
(lldb) p *$1
(cache_t) $3 = {
_buckets = {
std::__1::atomic<bucket_t *> = 0x00000001007117a0 {
_sel = {
std::__1::atomic<objc_selector *> = (null)
}
_imp = {
std::__1::atomic<unsigned long> = 0
}
}
}
_mask = {
std::__1::atomic<unsigned int> = 7
}
_flags = 32804
_occupied = 1
}
  1. 我们在第二个方法调用完下个断点,继续调试,我们可以看到 _occupied 又加了1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2020-12-21 23:56:51.210222+0800 Objc_m[2835:50950] -[TObject logPen]
(lldb) p *$1
(cache_t) $4 = {
_buckets = {
std::__1::atomic<bucket_t *> = 0x00000001007117a0 {
_sel = {
std::__1::atomic<objc_selector *> = (null)
}
_imp = {
std::__1::atomic<unsigned long> = 0
}
}
}
_mask = {
std::__1::atomic<unsigned int> = 7
}
_flags = 32804
_occupied = 2
}
  1. 我们在第三个方法调用完下个断点,继续调试,我们可以看到 _occupied 又加了1, 这是我们可以猜测 _occupied 和方法的个数有关
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2020-12-21 23:59:32.413929+0800 Objc_m[2835:50950] -[TObject logMouse]
(lldb) p *$1
(cache_t) $5 = {
_buckets = {
std::__1::atomic<bucket_t *> = 0x00000001007117a0 {
_sel = {
std::__1::atomic<objc_selector *> = “”
}
_imp = {
std::__1::atomic<unsigned long> = 12264
}
}
}
_mask = {
std::__1::atomic<unsigned int> = 7
}
_flags = 32804
_occupied = 3
}

此时我们就可以通过 源码进行调试,搜索_occupied,观察其变化,也就有了下面的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
void cache_t::incrementOccupied()
{
_occupied++;
}

void cache_t::insert(Class cls, SEL sel, IMP imp, id receiver)
{
#if CONFIG_USE_CACHE_LOCK
cacheUpdateLock.assertLocked();
#else
runtimeLock.assertLocked();
#endif

ASSERT(sel != 0 && cls->isInitialized());

// Use the cache as-is if it is less than 3/4 full
mask_t newOccupied = occupied() + 1;
unsigned oldCapacity = capacity(), capacity = oldCapacity;
if (slowpath(isConstantEmptyCache())) {
// Cache is read-only. Replace it.
// 假如之前没有分配过缓存空间的,分配一个初始容量为4的空间,并在其中将_occupied置为0
// INIT_CACHE_SIZE  1<<2
if (!capacity) capacity = INIT_CACHE_SIZE;
reallocate(oldCapacity, capacity, /* freeOld */false);
}
else if (fastpath(newOccupied + CACHE_END_MARKER <= capacity / 4 * 3)) {
// Cache is less than 3/4 full. Use it as-is.
// 缓存不超过3/4就会维持缓存空间不变
}
else {
// 缓存超过3/4则分配一个容量为当前两倍的新空间
capacity = capacity ? capacity * 2 : INIT_CACHE_SIZE;
// MAX_CACHE_SIZE 1<<16
if (capacity > MAX_CACHE_SIZE) {
capacity = MAX_CACHE_SIZE;
}
reallocate(oldCapacity, capacity, true);
}

bucket_t *b = buckets();
mask_t m = capacity – 1;
mask_t begin = cache_hash(sel, m); // 通过掩码获取方法的缓存的次序
mask_t i = begin;

// Scan for the first unused slot and insert there.
// There is guaranteed to be an empty slot because the
// minimum size is 4 and we resized at 3/4 full.
// 最小是4,当充满3/4 就会 重新分配
do {
if (fastpath(b[i].sel() == 0)) {
incrementOccupied(); // 自增 _occupied
b[i].set<Atomic, Encoded>(sel, imp, cls); // 方法写入缓存
return;
}
if (b[i].sel() == sel) { // 已经加入了缓存
// The entry was added to the cache by some other thread
// before we grabbed the cacheUpdateLock.
return;
}
} while (fastpath((i = cache_next(i, m)) != begin));

cache_t::bad_cache(receiver, (SEL)sel, cls);
}
void cache_fill(Class cls, SEL sel, IMP imp, id receiver)
{
runtimeLock.assertLocked();

#if !DEBUG_TASK_THREADS
// Never cache before +initialize is done
if (cls->isInitialized()) {
cache_t *cache = getCache(cls);
#if CONFIG_USE_CACHE_LOCK
mutex_locker_t lock(cacheUpdateLock);
#endif
cache->insert(cls, sel, imp, receiver);
}
#else
_collecting_in_critical();
#endif
}

我们下断点 观察一下 alloc 到底做了什么
可以看到 调用了 alloc 方法, 接着 开辟了4 字节的空间,接着调用 return 方法,添加引用技术,所以 _occupied 变成了2

f00000005

f000000005

通过断点 我们发现 之前 的 2 都是针对于 NSObject 类对象产生的

而之后的方法的 Tobject 调用方法 使用的是 Tobject 的类对象中的 cache_t
三. 总结

  • OC 中的实例方法混存在类中,类方法缓存在元类上
  • 缓存最小是4字节,缓存充满 3/4时,会扩容至当前的2倍
  • 扩容并不是在原来的基础上添加,而是重新开辟新的 allocateBuckets,释放旧的 cache_collect_free,把最近一次临界的imp和key缓存进来,经典的LRU算法。

转载请注明:XAMPP中文组官网 » OC底层原理实现 cache_t 结构方法缓存

您必须 登录 才能发表评论!