Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Quick lookup process:
- Class object address, memory translation
16 bytes
, get the address, getcache
The first address, namely:_bucketsAndMaybeMask
_bucketsAndMaybeMask & bucketsMask
To obtainbuckets
The first address- judge
_bucketsAndMaybeMask
the0
Position number0
And into theLLookupPreopt
Process to find the shared cache - Otherwise, pass
_bucketsAndMaybeMask >> 48
,mask
(_cmd ^ (_cmd >> 7)) & mask
, get the subscripti
. In the sourcecache_hash
function- through
i * 16
Get the offset,The first address of buckets + offset value
, gets the specified subscriptbucket
Process 1
:- read
bucket_t
In theimp
andsel
- through
Bucket_t - 16 bytes
, read the previous onebucket
- read
Flow 2
:- if
sel
Exists and is equal to_cmd
And into theCacheHit
Cache hitting Process
–
CacheHit
Process: Useimp = imp ^ cls
decoding- Jump to the specified
imp
Function addresses
- if
The process of 3
:- if
sel
Not present. Enter__objc_msgSend_uncached
process - On a
bucket
The address andbuckets
First address comparison, if> =
And into theProcess 1
- Otherwise,
<
First address, getmask
The subscriptbucket
And into theThe process of 4
- if
The process of 4
:- read
bucket_t
In theimp
andsel
- through
Bucket_t - 16 bytes
, read the previous onebucket
- if
sel
Is equal to the_cmd
And into theFlow 2
- if
sel
Exists, and the previous onebucket
address>
Specify the subscriptbucket
Address, enterThe process of 4
- Otherwise, enter
__objc_msgSend_uncached
process
- read
In the message quick lookup process, if the cache is not hit, enter the MissLabelDynamic process. MissLabelDynamic is the __objc_msgSend_uncached process that was passed in when CacheLookup was called:
- Core process:
__objc_msgSend_uncached
–MethodTableLookup
–_lookUpImpOrForward
lookUpImpOrForward
Function, and is not implemented in assembly code, butC/C++
function- Assembly and C/C++ intercalls:
C/C++
The method name is preceded by an underscore when looking in assembly code- Call in assembly
C/C++
Function,C/C++
When looking up in code, remove the first underscore of the method name