{"rsdb":{"rid":"410668","subhead":"","postdate":"0","aid":"283628","fid":"99","uid":"1","topic":"1","content":"
\n

\u4e00\u3001\u6458\u8981<\/p> \n

1.\u9605\u8bfb\u8be5\u7bc7\uff0c\u9700\u8981\u5bf9runtime\u5e95\u5c42\u53ca\u7c7b\u5bf9\u8c61\u6570\u636e\u7ed3\u6784\u6709\u4e00\u5b9a\u4e86\u89e3\uff0c\u672c\u7bc7\u4ec5\u7740\u91cd\u8bb2\u89e3\u65b9\u6cd5\u7f13\u5b58\u7684\u7b97\u6cd5\uff1b<\/p> \n

2.\u4ee5\u4e0b\u4ee5\u7c7b\u5bf9\u8c61\u6765\u8bba\u8ff0\uff0c\u5143\u7c7b\u5bf9\u8c61\u4ee5\u6b64\u7c7b\u63a8\uff1b<\/p> \n

 <\/p> \n

\u4e8c\u3001\u7c7b\u5bf9\u8c61\u6570\u636e\u7ed3\u6784<\/p> \n

\/\/rumtime\u6e90\u7801<\/p> \n

 <\/p> \n

\"\"<\/p> \n

 <\/p> \n

\/\/\u5c0f\u7801\u54e5\u56fe\u7247<\/p> \n

\"\"<\/p> \n

 <\/p> \n

 <\/p> \n

\u8bf4\u660e\uff1a\u5176\u4e2dcache_t\u7c7b\u578b\u53d8\u91cfcache\u5c31\u662f\u7528\u6765\u7f13\u5b58\u66fe\u7ecf\u8c03\u5ea6\u8fc7\u7684\u65b9\u6cd5\uff1b<\/p> \n

 <\/p> \n

\u4e09\u3001\u65b9\u6cd5\u8c03\u5ea6\u539f\u7406<\/p> \n

\n
Person *per = [[Person alloc] init];
createCaches(ORIGINAL_MASK);
handleMethod(<\/span>"<\/span>test1<\/span>"<\/span>, @selector(test1), [per test1]);\nhandleMethod(<\/span>"<\/span>test2<\/span>"<\/span>, @selector(test2), [per test2]);\nhandleMethod(<\/span>"<\/span>test1<\/span>"<\/span>, @selector(test1), [per test1]);\nhandleMethod(<\/span>"<\/span>test3<\/span>"<\/span>, @selector(test3), [per test3]);\nhandleMethod(<\/span>"<\/span>test4<\/span>"<\/span>, @selector(test4), [per test4]);\nhandleMethod(<\/span>"<\/span>test5<\/span>"<\/span>, @selector(test5), [per test5]);\nhandleMethod(<\/span>"<\/span>test4<\/span>"<\/span>, @selector(test4), [per test4]);\nhandleMethod(<\/span>"<\/span>test6WithHeight:age:<\/span>"<\/span>, @selector(test6WithHeight:age:), [per test6WithHeight:1.7<\/span> age:30<\/span>]);\nhandleMethod(<\/span>"<\/span>test7WithName:<\/span>"<\/span>, @selector(test7WithName:), [per test7WithName:@"<\/span>\u5f20\u4e09<\/span>"<\/span>]);\n \nfree(methodCaches);<\/span><\/pre> \n <\/div> \n

\u5982\u4e0a\u6240\u793a\uff1a<\/p> \n

1.\u5b9e\u4f8b\u5bf9\u8c61per\u8c03test1\/2\/3\u7b49\u65b9\u6cd5\u65f6\uff0cruntime\u5e95\u5c42\u672c\u8d28\u662f\u901a\u8fc7msgSend\u5411per\u5bf9\u8c61\u53d1\u9001\u6d88\u606f\uff1b<\/p> \n

2.\u7cfb\u7edf\u4f1a\u901a\u8fc7per\u7684isa\u6307\u9488\u627e\u5230\u5176\u7c7b\u5bf9\u8c61\uff0c\u7136\u540e\u4f18\u5148\u5230\u8be5\u7c7b\u5bf9\u8c61\u7684cache\u91cc\u9762\u53bb\u67e5\u627e\uff0c\u5982\u679c\u80fd\u627e\u5230\u5219\u76f4\u63a5\u8c03\u7528\uff1b\u5982\u679c\u6ca1\u6709\u627e\u5230\u5219\u518d\u5230struct_rw_t\u4e2d\u7684methods\u65b9\u6cd5\u5217\u8868\u4e2d\u67e5\u627e\uff1b\u5982\u679c\u8fd8\u6ca1\u627e\u5230\uff0c\u5219\u901a\u8fc7superClass\u6307\u9488\u5230\u7236\u7c7b\u4e2d\u67e5\u627e\uff08\u67e5\u627e\u987a\u5e8f\u540c\u524d\u6240\u8ff0\uff09\uff1b\u5982\u679c\u4e00\u7ea7\u7236\u7c7b\u6ca1\u627e\u5230\uff0c\u5219\u4e00\u76f4\u5f80\u4e0a\u7ea7\u7236\u7c7b\u67e5\u627e\uff0c\u76f4\u5230\u6839\u7236\u7c7b\uff1b\u5982\u679c\u6839\u7236\u7c7b\u4e5f\u6ca1\u6709\uff0c\u5219\u8fd4\u56de\u7a7a\uff1b<\/p> \n

 <\/p> \n

\u56db\u3001cache\u7f13\u5b58\u7b97\u6cd5<\/p> \n

1.\u65b9\u6cd5\u5e95\u5c42\u7ed3\u6784<\/p> \n

 <\/p> \n

\"\"<\/p> \n

 <\/p> \n

\u8bf4\u660e\uff1acache\u5185\u90e8\u5305\u542b\u4e09\u4e2a\u53d8\u91cf\uff1abuckets\uff08\u6563\u5217\u8868\uff09\uff0c_mask\uff08\u6563\u5217\u8868\u7684\u957f\u5ea6-1\uff09\uff0c_occupied\uff08\u5df2\u7ecf\u7f13\u5b58\u7684\u65b9\u6cd5\u6570\u91cf\uff09\uff1bbucket_t\u5305\u542b\u4e24\u4e2a\u53d8\u91cf\uff1a\u7c7b\u4f3c\u4e8e\u5b57\u5178\u7684\u952e\u503c\u5bf9\uff0c_key\u662f<\/p> \n

\u65b9\u6cd5SEL\uff08\u6574\u578b\u6570\u636e\uff09\uff0c_imp\u7f13\u5b58\u51fd\u6570\u7684\u5185\u5b58\u5730\u5740\uff1b<\/p> \n

2.\u7b97\u6cd5\u601d\u8def\u2014\u2014\u6563\u5217\u8868\uff08\u7a7a\u95f4\u6362\u65f6\u95f4\uff09\uff1a<\/p> \n

1\uff09\u7528\u6563\u5217\u8868\uff08\u5373\u6570\u7ec4\uff09\u6765\u7f13\u5b58\u8c03\u7528\u7684\u65b9\u6cd5\uff0c\u5148\u5f00\u8f9f\u56fa\u5b9a\u957f\u5ea6\u7684\u5185\u5b58\uff08\u6b64\u5904\u8bbe\u7f6e\u4e3a3\uff09\uff0c\u6570\u7ec4\u5143\u7d20\u5219\u4e3a\u952e\u503c\u5bf9\u7684\u7ed3\u6784\u4f53\uff1b<\/p> \n

\/\/\u521b\u5efa\u6563\u5217\u8868<\/p> \n

\n
void<\/span> createCaches(mask_t mask) {\n    <\/span>\/\/<\/span>\u521b\u5efa\u6563\u5217\u8868<\/span>\n    struct<\/span> bucket_t *originalBuckets = (struct<\/span> bucket_t *)malloc(sizeof<\/span>(struct<\/span> bucket_t)*mask);\n    <\/span>for<\/span> (int<\/span> i = 0<\/span>; i < mask; i++) {\n        originalBuckets[i]._name <\/span>= ""<\/span>;\n        originalBuckets[i]._key <\/span>= 0<\/span>;\n        originalBuckets[i]._imp <\/span>= NULL;\n        originalBuckets[i]._types <\/span>= "<\/span>null<\/span>"<\/span>;\n    }\n    \n    methodCaches <\/span>= (struct<\/span> cache_t *)malloc(sizeof<\/span>(struct<\/span> cache_t));\n    methodCaches<\/span>->_mask = (mask_t)(mask-1<\/span>);\n    methodCaches<\/span>->_occupied = 0<\/span>;\n    methodCaches<\/span>->_buckets = originalBuckets;\n}<\/span><\/pre> \n <\/div> \n 

 <\/p> \n

2\uff09\u7528_mask\u4e0e_key\u8fdb\u884c\u6309\u4f4d\u4e0e\u8fd0\u7b97\uff0c\u5f97\u5230\u6bcf\u4e2a\u5143\u7d20\u7684\u4e0b\u6807index\u2014\u2014\u8fd9\u6837\u5f97\u51fa\u7684index\u4e0d\u4f1a\u5927\u4e8e_mask\uff08\u539f\u56e0\uff1a\u53ef\u4ee5\u770b\u4f4d\u57df\u90a3\u7bc7\u6587\u7ae0\uff09\uff0c\u540c\u65f6\u4e3a\u968f\u673a\u6570\uff1b<\/p> \n

 <\/p> \n

3\uff09\u6bcf\u6b21\u8c03\u65b9\u6cd5\uff0c\u4f1a\u5148\u8fdb\u884c\u6309\u4f4d\u4e0e\u8ba1\u7b97\u5f97\u51fa\u4e0b\u6807A\uff0c\u7136\u540e\u67e5\u627e\u8be5\u4e0b\u6807\u4f4d\u7f6e\u5904\u662f\u5426\u7f13\u5b58\u4e86\u65b9\u6cd5\uff1a\u5982\u679c\u6709\u4e14\u7f13\u5b58\u7684\u65b9\u6cd5\u8ddf\u88ab\u8c03\u65b9\u6cd5\u76f8\u540c\uff0c\u5219\u76f4\u63a5\u8c03\u7528\u7f13\u5b58\u4e2d\u7684\u65b9\u6cd5\uff1b\u5982\u679c\u6ca1\u6709\uff0c\u5219\u4e0b\u6807-1\u8fdb\u884c\u904d\u5386\u67e5\u627e\uff08\u4e3a0\u65f6\uff0c\u76f4\u63a5\u56de\u5230\u6570\u7ec4\u672b\u5c3e\uff0c\u518d-1\u7ee7\u7eed\u67e5\u627e\uff09\uff0c\u76f4\u81f3\u56de\u5230A\u5904\uff0c\u5982\u679c\u627e\u5230\u4e86\uff0c\u5219\u76f4\u63a5\u8c03\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0c\u5219\u5c06\u8be5\u65b9\u6cd5\u8fdb\u884c\u7f13\u5b58\uff1b<\/p> \n

\/\/\u67e5\u627e\u6838\u5fc3\u4ee3\u7801<\/p> \n

\n
\/\/<\/span>inline\u5173\u952e\u5b57\uff1aC++<\/a>\u5173\u8054\u51fd\u6570\uff0c\u8868\u793a\u5728\u8c03\u7528\u8be5\u51fd\u6570\u5904\uff0c\u76f4\u63a5\u66ff\u6362\u6210\u51fd\u6570\u4f53\u5185\u7684\u4ee3\u7801\uff08\u597d\u5904\uff1a\u907f\u514d\u9891\u7e41\u8c03\u7528\u8be5\u51fd\u6570\u5bfc\u81f4\u5185\u5b58\u6d88\u8017\uff09<\/span>\nstatic<\/span> inline mask_t cache_next(mask_t i, mask_t mask) {\n    <\/span>return<\/span> i ? i-1<\/span> : mask;\n}\n\n\n\n<\/span>\/\/<\/span>\u67e5\u627e\u65b9\u6cd5<\/span>\n    IMP findSEL(SEL selector) {\n        mask_t begin <\/span>= _mask & (long<\/span> long<\/span>)selector;\n        mask_t i <\/span>= begin;\n        <\/span>do<\/span> {\/\/<\/span>\u5982\u679c\u67e5\u5230\u76f4\u63a5\u8fd4\u56de\uff0c\u5426\u5219-1\u5f80\u56de\u67e5\u627e\uff0c\u76f4\u5230\u53c8\u56de\u5230begin\u4f4d\u7f6e\u5904<\/span>\n            if<\/span> (_buckets[i]._key == (long<\/span> long<\/span>)selector) {\n                <\/span>return<\/span> _buckets[i]._imp;\n            }\n        } <\/span>while<\/span> ((i = cache_next(i, _mask)) != begin);\n        <\/span>return<\/span> NULL;\/\/<\/span>\u6ca1\u6709\u627e\u5230\uff0c\u8fd4\u56denull<\/span>\n    }<\/pre> \n <\/div> \n 

 <\/p> \n

4\uff09\u5982\u679cA\u5904\u662f\u7a7a\uff0c\u5219\u76f4\u63a5\u7f13\u5b58\u81f3A\u5904\uff0c\u5426\u5219-1\u67e5\u627e\u904d\u5386\u7a7a\u4f59\u4f4d\u7f6e\uff08\u539f\u7406\u540c\u4e0a\uff09\uff1b<\/p> \n

\/\/\u7f13\u5b58\u6838\u5fc3\u4ee3\u7801<\/p> \n

\n
void<\/span> saveSEL(char<\/span> const<\/span>*method, SEL selector, IMP methodIMP, char<\/span> const<\/span>*types) {\n    <\/span>\/\/<\/span>\u6563\u5217\u8868\u662f\u5426\u4e3a\u7a7a<\/span>\n    if<\/span> (methodCaches->_buckets && methodCaches->_mask+1<\/span> > 0<\/span>) {\n        mask_t begin <\/span>= methodCaches->_mask & (long<\/span> long<\/span>)selector;\n        mask_t i <\/span>= begin;\n        <\/span>do<\/span> {\n            <\/span>if<\/span> (methodCaches->_buckets[i]._imp == NULL) {\n                methodCaches<\/span>->_buckets[i]._name = method;\n                methodCaches<\/span>->_buckets[i]._key = (long<\/span> long<\/span>)selector;\n                methodCaches<\/span>->_buckets[i]._imp = methodIMP;\n                metho","orderid":"0","title":"\u7c7b\uff08\u5143\u7c7b\uff09\u5bf9\u8c61\u65b9\u6cd5\u7f13\u5b58\u539f\u7406(\u4e00)","smalltitle":"","mid":"0","fname":"iOS","special_id":"0","bak_id":"0","info":"0","hits":"91","pages":"2","comments":"0","posttime":"2020-04-06 18:13:23","list":"1586168003","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"\uff08\u5143\u7c7b<\/A> \u5bf9\u8c61<\/A> \u65b9\u6cd5<\/A> \u539f\u7406<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"14.17.22.33","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u7c7b\uff08\u5143\u7c7b\uff09\u5bf9\u8c61\u65b9\u6cd5\u7f13\u5b58\u539f\u7406","lastview":"1590792446","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}