{"id":343185,"date":"2021-02-15T22:05:27","date_gmt":"2021-02-15T14:05:27","guid":{"rendered":"http:\/\/4563.org\/?p=343185"},"modified":"2021-02-15T22:05:27","modified_gmt":"2021-02-15T14:05:27","slug":"%e6%b7%b1%e5%85%a5-python-%e8%a7%a3%e9%87%8a%e5%99%a8%e6%ba%90%e7%a0%81%ef%bc%8c%e6%88%91%e7%bb%88%e4%ba%8e%e6%90%9e%e6%98%8e%e7%99%bd%e4%ba%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e9%a9%bb%e7%95%99%e7%9a%84","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=343185","title":{"rendered":"\u6df1\u5165 Python \u89e3\u91ca\u5668\u6e90\u7801\uff0c\u6211\u7ec8\u4e8e\u641e\u660e\u767d\u4e86\u5b57\u7b26\u4e32\u9a7b\u7559\u7684\u539f\u7406\uff01"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6df1\u5165 Python \u89e3\u91ca\u5668\u6e90\u7801\uff0c\u6211\u7ec8\u4e8e\u641e\u660e\u767d\u4e86\u5b57\u7b26\u4e32\u9a7b\u7559\u7684\u539f\u7406\uff01               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chinesehuazhou <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u82f1\u6587\uff1a https:\/\/arpitbhayani.me\/blogs\/string-interning<\/p>\n<p>\u4f5c\u8005\uff1aarpit<\/p>\n<p>\u8bd1\u8005\uff1a\u8c4c\u8c46\u82b1\u4e0b\u732b\uff08\u201cPython \u732b\u201d\u516c\u4f17\u53f7\u4f5c\u8005\uff09<\/p>\n<p>\u58f0\u660e\uff1a\u672c\u7ffb\u8bd1\u662f\u51fa\u4e8e\u4ea4\u6d41\u5b66\u4e60\u7684\u76ee\u7684\uff0c\u57fa\u4e8e CC BY-NC-SA 4.0 \u6388\u6743\u534f\u8bae\u3002\u4e3a\u4fbf\u4e8e\u9605\u8bfb\uff0c\u5185\u5bb9\u7565\u6709\u6539\u52a8\u3002<\/p>\n<p>\u6bcf\u79cd\u7f16\u7a0b\u8bed\u8a00\u4e3a\u4e86\u8868\u73b0\u51fa\u8272\uff0c\u5e76\u4e14\u5b9e\u73b0\u5353\u8d8a\u7684\u6027\u80fd\uff0c\u90fd\u9700\u8981\u6709\u5927\u91cf\u7f16\u8bd1\u5668\u7ea7\u4e0e\u89e3\u91ca\u5668\u7ea7\u7684\u4f18\u5316\u3002<\/p>\n<p>\u7531\u4e8e\u5b57\u7b26\u4e32\u662f\u4efb\u4f55\u7f16\u7a0b\u8bed\u8a00\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u4e2a\u90e8\u5206\uff0c\u56e0\u6b64\uff0c\u5982\u679c\u6709\u5feb\u901f\u64cd\u4f5c\u5b57\u7b26\u4e32\u7684\u80fd\u529b\uff0c\u5c31\u53ef\u4ee5\u8fc5\u901f\u5730\u63d0\u9ad8\u6574\u4f53\u7684\u6027\u80fd\u3002<\/p>\n<p>\u5728\u672c\u6587\u4e2d\uff0c<strong>\u6211\u4eec\u5c06\u6df1\u5165\u7814\u7a76 Python \u7684\u5185\u90e8\u5b9e\u73b0\uff0c\u5e76\u4e86\u89e3 Python \u5982\u4f55\u4f7f\u7528\u4e00\u79cd\u540d\u4e3a\u5b57\u7b26\u4e32\u9a7b\u7559\uff08String Interning\uff09\u7684\u6280\u672f\uff0c\u5b9e\u73b0\u89e3\u91ca\u5668\u7684\u9ad8\u6027\u80fd\u3002<\/strong> \u672c\u6587\u7684\u76ee\u7684\u4e0d\u4ec5\u5728\u4e8e\u4ecb\u7ecd Python \u7684\u5185\u90e8\u77e5\u8bc6\uff0c\u800c\u4e14\u8fd8\u65e8\u5728\u4f7f\u8bfb\u8005\u80fd\u591f\u8f7b\u677e\u5730\u6d4f\u89c8 Python \u7684\u6e90\u4ee3\u7801\uff1b\u56e0\u6b64\uff0c\u672c\u6587\u4e2d\u5c06\u6709\u5f88\u591a\u51fa\u81ea CPython \u7684\u4ee3\u7801\u7247\u6bb5\u3002<\/p>\n<p>\u5168\u6587\u63d0\u7eb2\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/02\/20210219_602fe50fbab8f.jpg\" alt=\"\u6df1\u5165 Python \u89e3\u91ca\u5668\u6e90\u7801\uff0c\u6211\u7ec8\u4e8e\u641e\u660e\u767d\u4e86\u5b57\u7b26\u4e32\u9a7b\u7559\u7684\u539f\u7406\uff01\" \/><\/p>\n<h2>1 \u3001\u4ec0\u4e48\u662f\u201c\u5b57\u7b26\u4e32\u9a7b\u7559\u201d\uff1f<\/h2>\n<p><strong>\u5b57\u7b26\u4e32\u9a7b\u7559\u662f\u4e00\u79cd\u7f16\u8bd1\u5668 \/\u89e3\u91ca\u5668\u7684\u4f18\u5316\u65b9\u6cd5\uff0c\u5b83\u901a\u8fc7<code>\u7f13\u5b58<\/code>\u4e00\u822c\u6027\u7684\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u8282\u7701\u5b57\u7b26\u4e32\u5904\u7406\u4efb\u52a1\u7684\u7a7a\u95f4\u548c\u65f6\u95f4\u3002<\/strong> <\/p>\n<p>\u8fd9\u79cd\u4f18\u5316\u65b9\u6cd5\u4e0d\u4f1a\u6bcf\u6b21\u90fd\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u526f\u672c\uff0c\u800c\u662f\u4ec5\u4e3a\u6bcf\u4e2a<em>\u9002\u5f53\u7684<\/em>\u4e0d\u53ef\u53d8\u503c\u4fdd\u7559\u4e00\u4e2a\u5b57\u7b26\u4e32\u526f\u672c\uff0c\u5e76\u4f7f\u7528\u6307\u9488\u5f15\u7528\u4e4b\u3002<\/p>\n<p>\u6bcf\u4e2a\u5b57\u7b26\u4e32\u7684\u552f\u4e00\u62f7\u8d1d\u88ab\u79f0\u4e3a\u5b83\u7684<code>intern<\/code>\uff0c\u5e76\u56e0\u6b64\u800c\u5f97\u540d String Interning \u3002<\/p>\n<blockquote>\n<p>Python \u732b\u6ce8\uff1aString Interning \u4e00\u822c\u88ab\u8bd1\u4e3a\u201c\u5b57\u7b26\u4e32\u9a7b\u7559\u201d\u6216\u201c\u5b57\u7b26\u4e32\u7559\u7528\u201d\uff0c\u5728\u67d0\u4e9b\u8bed\u8a00\u4e2d\u53ef\u80fd\u4e60\u60ef\u7528 String Pool \uff08\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\uff09\u7684\u6982\u5ff5\uff0c\u5176\u5b9e\u662f\u5bf9\u540c\u4e00\u79cd\u673a\u5236\u7684\u4e0d\u540c\u8868\u8ff0\u3002intern \u4f5c\u4e3a\u540d\u8bcd\u65f6\uff0c\u662f\u201c\u5b9e\u4e60\u751f\u3001\u5b9e\u4e60\u533b\u751f\u201d\u7684\u610f\u601d\uff0c\u5728\u6b64\u53ef\u4ee5\u7406\u89e3\u6210\u201c\u9a7b\u7559\u7269\u3001\u9a7b\u7559\u503c\u201d\u3002<\/p>\n<\/blockquote>\n<p>\u67e5\u627e\u5b57\u7b26\u4e32 intern \u7684\u65b9\u6cd5\u53ef\u80fd\u4f5c\u4e3a\u516c\u5f00\u63a5\u53e3\u516c\u5f00\uff0c\u4e5f\u53ef\u80fd\u4e0d\u516c\u5f00\u3002\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\u5982 Java \u3001Python \u3001PHP \u3001Ruby \u3001Julia \u7b49\u7b49\uff0c\u90fd\u652f\u6301\u5b57\u7b26\u4e32\u9a7b\u7559\uff0c\u4ee5\u4f7f\u5176\u7f16\u8bd1\u5668\u548c\u89e3\u91ca\u5668\u505a\u5230\u9ad8\u6027\u80fd\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/02\/20210219_602fe51d07814.jpg\" alt=\"\u6df1\u5165 Python \u89e3\u91ca\u5668\u6e90\u7801\uff0c\u6211\u7ec8\u4e8e\u641e\u660e\u767d\u4e86\u5b57\u7b26\u4e32\u9a7b\u7559\u7684\u539f\u7406\uff01\" \/><\/p>\n<h2>2 \u3001\u4e3a\u4ec0\u4e48\u8981\u9a7b\u7559\u5b57\u7b26\u4e32\uff1f<\/h2>\n<p><strong>\u5b57\u7b26\u4e32\u9a7b\u7559\u63d0\u5347\u4e86\u5b57\u7b26\u4e32\u6bd4\u8f83\u7684\u901f\u5ea6\u3002<\/strong> \u5982\u679c\u6ca1\u6709\u9a7b\u7559\uff0c\u5f53\u6211\u4eec\u8981\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49\u65f6\uff0c\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c06\u4e0a\u5347\u5230 O(n)\uff0c\u5373\u9700\u8981\u68c0\u67e5\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u624d\u80fd\u5224\u65ad\u51fa\u5b83\u4eec\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u662f\u56fa\u5b9a\u7684\uff0c\u7531\u4e8e\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u5c06\u4f7f\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u5f15\u7528\uff0c\u56e0\u6b64\u53ea\u9700\u68c0\u67e5\u6307\u9488\u662f\u5426\u76f8\u540c\uff0c\u5c31\u8db3\u4ee5\u5224\u65ad\u51fa\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49\uff0c\u4e0d\u5fc5\u518d\u9010\u4e00\u68c0\u67e5\u6bcf\u4e2a\u5b57\u7b26\u3002\u7531\u4e8e\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u666e\u904d\u7684\u64cd\u4f5c\uff0c\u56e0\u6b64\uff0c\u5b83\u88ab\u5178\u578b\u5730\u5b9e\u73b0\u4e3a\u6307\u9488\u76f8\u7b49\u6027\u6821\u9a8c\uff0c\u4ec5\u4f7f\u7528\u4e00\u6761\u5b8c\u5168\u6ca1\u6709\u5185\u5b58\u5f15\u7528\u7684\u673a\u5668\u6307\u4ee4\u3002<\/p>\n<p><strong>\u5b57\u7b26\u4e32\u9a7b\u7559\u51cf\u5c11\u4e86\u5185\u5b58\u5360\u7528\u3002<\/strong> Python \u907f\u514d\u5185\u5b58\u4e2d\u5145\u65a5\u591a\u4f59\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u901a\u8fc7<code>\u4eab\u5143\u8bbe\u8ba1\u6a21\u5f0f<\/code>\u5171\u4eab\u548c\u91cd\u7528\u5df2\u7ecf\u5b9a\u4e49\u7684\u5bf9\u8c61\uff0c\u4ece\u800c\u4f18\u5316\u5185\u5b58\u5360\u7528\u3002<\/p>\n<h2>3 \u3001Python \u7684\u5b57\u7b26\u4e32\u9a7b\u7559<\/h2>\n<p>\u50cf\u5927\u591a\u6570\u5176\u5b83\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\u4e00\u6837\uff0cPython \u4e5f\u4f7f\u7528\u5b57\u7b26\u4e32\u9a7b\u7559\u6765\u63d0\u9ad8\u6027\u80fd\u3002\u5728 Python \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>is<\/code>\u8fd0\u7b97\u7b26\uff0c\u68c0\u67e5\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u5f15\u7528\u4e86\u540c\u4e00\u4e2a\u5185\u5b58\u5bf9\u8c61\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5982\u679c\u4e24\u4e2a\u5b57\u7b26\u4e32\u5bf9\u8c61\u5f15\u7528\u4e86\u76f8\u540c\u7684\u5185\u5b58\u5bf9\u8c61\uff0c\u5219<code>is<\/code>\u8fd0\u7b97\u7b26\u5c06\u5f97\u51fa<code>True<\/code>\uff0c\u5426\u5219\u4e3a<code>False<\/code>\u3002<\/p>\n<pre><code>&gt;&gt;&gt; 'python' is 'python' True <\/code><\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u7279\u5b9a\u7684\u8fd0\u7b97\u7b26\uff0c\u6765\u5224\u65ad\u54ea\u4e9b\u5b57\u7b26\u4e32\u662f\u88ab\u9a7b\u7559\u7684\u3002\u5728 CPython \u7684\uff0c\u5b57\u7b26\u4e32\u9a7b\u7559\u662f\u901a\u8fc7\u4ee5\u4e0b\u51fd\u6570\u5b9e\u73b0\u7684\uff0c\u58f0\u660e\u5728 unicodeobject.h \u4e2d\uff0c\u5b9a\u4e49\u5728 unicodeobject.c \u4e2d\u3002<\/p>\n<pre><code>PyAPI_FUNC(void) PyUnicode_InternInPlace(PyObject **); <\/code><\/pre>\n<p>\u4e3a\u4e86\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u88ab\u9a7b\u7559\uff0cCPython \u5b9e\u73b0\u4e86\u4e00\u4e2a\u540d\u4e3a<code>PyUnicode_CHECK_INTERNED<\/code>\u7684\u5b8f\uff0c\u540c\u6837\u662f\u5b9a\u4e49\u5728 unicodeobject.h \u4e2d\u3002<\/p>\n<p>\u8fd9\u4e2a\u5b8f\u8868\u660e\u4e86 Python \u5728<code>PyASCIIObject<\/code>\u7ed3\u6784\u4e2d\u7ef4\u62a4\u7740\u4e00\u4e2a\u540d\u4e3a<code>interned<\/code>\u7684\u6210\u5458\u53d8\u91cf\uff0c\u5b83\u7684\u503c\u8868\u793a\u76f8\u5e94\u7684\u5b57\u7b26\u4e32\u662f\u5426\u88ab\u9a7b\u7559\u3002<\/p>\n<pre><code>#define PyUnicode_CHECK_INTERNED(op)      (((PyASCIIObject *)(op))-&gt;state.interned) <\/code><\/pre>\n<h2>4 \u3001\u5b57\u7b26\u4e32\u9a7b\u7559\u7684\u539f\u7406<\/h2>\n<p><strong>\u5728 CPython \u4e2d\uff0c\u5b57\u7b26\u4e32\u7684\u5f15\u7528\u88ab\u4e00\u4e2a\u540d\u4e3a<code>interned<\/code>\u7684 Python \u5b57\u5178\u6240\u5b58\u50a8\u3001\u8bbf\u95ee\u548c\u7ba1\u7406\u3002<\/strong> \u8be5\u5b57\u5178\u5728\u7b2c\u4e00\u6b21\u8c03\u7528\u5b57\u7b26\u4e32\u9a7b\u7559\u65f6\uff0c\u88ab\u5ef6\u8fdf\u5730\u521d\u59cb\u5316\uff0c\u5e76\u6301\u6709\u5168\u90e8\u5df2\u9a7b\u7559\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u5f15\u7528\u3002<\/p>\n<h3>4.1 \u5982\u4f55\u9a7b\u7559\u5b57\u7b26\u4e32\uff1f<\/h3>\n<p>\u8d1f\u8d23\u9a7b\u7559\u5b57\u7b26\u4e32\u7684\u6838\u5fc3\u51fd\u6570\u662f<code>PyUnicode_InternInPlace<\/code>\uff0c\u5b83\u5b9a\u4e49\u5728 unicodeobject.c \u4e2d\uff0c\u5f53\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u521b\u5efa\u4e00\u4e2a\u51c6\u5907\u5bb9\u7eb3\u6240\u6709\u9a7b\u7559\u7684\u5b57\u7b26\u4e32\u7684\u5b57\u5178<code>interned<\/code>\uff0c\u7136\u540e\u767b\u8bb0\u5165\u53c2\u4e2d\u7684\u5bf9\u8c61\uff0c\u4ee4\u5176\u952e\u548c\u503c\u90fd\u4f7f\u7528\u76f8\u540c\u7684\u5bf9\u8c61\u5f15\u7528\u3002<\/p>\n<p>\u4ee5\u4e0b\u51fd\u6570\u7247\u6bb5\u663e\u793a\u4e86 Python \u5b9e\u73b0\u5b57\u7b26\u4e32\u9a7b\u7559\u7684\u8fc7\u7a0b\u3002<\/p>\n<pre><code>void PyUnicode_InternInPlace(PyObject **p) {     PyObject *s = *p;      .........      \/\/ Lazily build the dictionary to hold interned Strings     if (interned == NULL) {         interned = PyDict_New();         if (interned == NULL) {             PyErr_Clear();             return;         }     }      PyObject *t;      \/\/ Make an entry to the interned dictionary for the     \/\/ given object     t = PyDict_SetDefault(interned, s, s);      .........          \/\/ The two references in interned dict (key and value) are     \/\/ not counted by refcnt.     \/\/ unicode_dealloc() and _PyUnicode_ClearInterned() take     \/\/ care of this.     Py_SET_REFCNT(s, Py_REFCNT(s) - 2);      \/\/ Set the state of the string to be INTERNED     _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; } <\/code><\/pre>\n<h3>4.2 \u5982\u4f55\u6e05\u7406\u9a7b\u7559\u7684\u5b57\u7b26\u4e32\uff1f<\/h3>\n<p>\u6e05\u7406\u51fd\u6570\u4ece<code>interned<\/code>\u5b57\u5178\u4e2d\u904d\u5386\u6240\u6709\u7684\u5b57\u7b26\u4e32\uff0c\u8c03\u6574\u8fd9\u4e9b\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\uff0c\u5e76\u628a\u5b83\u4eec\u6807\u8bb0\u4e3a<code>NOT_INTERNED<\/code>\uff0c\u4f7f\u5176\u88ab\u5783\u573e\u56de\u6536\u3002\u4e00\u65e6\u6240\u6709\u7684\u5b57\u7b26\u4e32\u90fd\u88ab\u6807\u8bb0\u4e3a<code>NOT_INTERNED<\/code>\uff0c\u5219<code>interned<\/code>\u5b57\u5178\u4f1a\u88ab\u6e05\u7a7a\u5e76\u5220\u9664\u3002<\/p>\n<p>\u8fd9\u4e2a\u6e05\u7406\u51fd\u6570\u5c31\u662f<code>_PyUnicode_ClearInterned<\/code>\uff0c\u5728 unicodeobject.c \u4e2d\u5b9a\u4e49\u3002<\/p>\n<pre><code>void _PyUnicode_ClearInterned(PyThreadState *tstate) {     .........      \/\/ Get all the keys to the interned dictionary     PyObject *keys = PyDict_Keys(interned);      .........      \/\/ Interned Unicode strings are not forcibly deallocated;     \/\/ rather, we give them their stolen references back     \/\/ and then clear and DECREF the interned dict.      for (Py_ssize_t i = 0; i &lt; n; i++) {         PyObject *s = PyList_GET_ITEM(keys, i);          .........          switch (PyUnicode_CHECK_INTERNED(s)) {         case SSTATE_INTERNED_IMMORTAL:             Py_SET_REFCNT(s, Py_REFCNT(s) + 1);             break;         case SSTATE_INTERNED_MORTAL:             \/\/ Restore the two references (key and value) ignored             \/\/ by PyUnicode_InternInPlace().             Py_SET_REFCNT(s, Py_REFCNT(s) + 2);             break;         case SSTATE_NOT_INTERNED:             \/* fall through *\/         default:             Py_UNREACHABLE();         }          \/\/ marking the string to be NOT_INTERNED         _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED;     }      \/\/ decreasing the reference to the initialized and     \/\/ access keys object.     Py_DECREF(keys);      \/\/ clearing the dictionary     PyDict_Clear(interned);      \/\/ clearing the object interned     Py_CLEAR(interned); } <\/code><\/pre>\n<h2>5 \u3001\u5b57\u7b26\u4e32\u9a7b\u7559\u7684\u5b9e\u73b0<\/h2>\n<p>\u65e2\u7136\u4e86\u89e3\u4e86\u5b57\u7b26\u4e32\u9a7b\u7559\u53ca\u6e05\u7406\u7684\u5185\u90e8\u539f\u7406\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u627e\u51fa Python \u4e2d\u6240\u6709\u4f1a\u88ab\u9a7b\u7559\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4e3a\u4e86\u505a\u5230\u8fd9\u70b9\uff0c\u6211\u4eec\u8981\u505a\u7684\u5c31\u662f\u5728 CPython \u6e90\u4ee3\u7801\u4e2d\u67e5\u627e<code>PyUnicode_InternInPlace<\/code> \u51fd\u6570\u7684\u8c03\u7528\uff0c\u5e76\u67e5\u770b\u5176\u9644\u8fd1\u7684\u4ee3\u7801\u3002\u4e0b\u9762\u662f\u5728 Python \u4e2d\u5173\u4e8e\u5b57\u7b26\u4e32\u9a7b\u7559\u7684\u4e00\u4e9b\u6709\u8da3\u7684\u53d1\u73b0\u3002<\/p>\n<h3>5.1 \u53d8\u91cf\u3001\u5e38\u91cf\u4e0e\u51fd\u6570\u540d<\/h3>\n<p><strong>CPython \u5bf9\u5e38\u91cf\uff08\u4f8b\u5982\u51fd\u6570\u540d\u3001\u53d8\u91cf\u540d\u3001\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7b49\uff09\u6267\u884c\u5b57\u7b26\u4e32\u9a7b\u7559\u3002<\/strong> <\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u51fa\u81eacodeobject.c\uff0c\u5b83\u8868\u660e\u5728\u521b\u5efa\u65b0\u7684<code>PyCode<\/code>\u5bf9\u8c61\u65f6\uff0c\u89e3\u91ca\u5668\u5c06\u5bf9\u6240\u6709\u7f16\u8bd1\u671f\u7684\u5e38\u91cf\u3001\u540d\u79f0\u548c\u5b57\u9762\u91cf\u8fdb\u884c\u9a7b\u7559\u3002<\/p>\n<pre><code>PyCodeObject * PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,                           int nlocals, int stacksize, int flags,                           PyObject *code, PyObject *consts, PyObject *names,                           PyObject *varnames, PyObject *freevars, PyObject *cellvars,                           PyObject *filename, PyObject *name, int firstlineno,                           PyObject *linetable) {      ........      if (intern_strings(names) &lt; 0) {         return NULL;     }      if (intern_strings(varnames) &lt; 0) {         return NULL;     }      if (intern_strings(freevars) &lt; 0) {         return NULL;     }      if (intern_strings(cellvars) &lt; 0) {         return NULL;     }      if (intern_string_constants(consts, NULL) &lt; 0) {         return NULL;     }      ........  } <\/code><\/pre>\n<h3>5.2 \u5b57\u5178\u7684\u952e<\/h3>\n<p><strong>CPython \u8fd8\u4f1a\u9a7b\u7559\u4efb\u4f55\u5b57\u5178\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u952e\u3002<\/strong><\/p>\n<p>\u5f53\u5728\u5b57\u5178\u4e2d\u63d2\u5165\u5143\u7d20\u65f6\uff0c\u89e3\u91ca\u5668\u4f1a\u5bf9\u8be5\u5143\u7d20\u7684\u952e\u4f5c\u5b57\u7b26\u4e32\u9a7b\u7559\u3002\u4ee5\u4e0b\u4ee3\u7801\u51fa\u81ea dictobject.c\uff0c\u5c55\u793a\u4e86\u5b9e\u9645\u7684\u884c\u4e3a\u3002<\/p>\n<p>\u6709\u8da3\u7684\u5730\u65b9\uff1a\u5728<code>PyUnicode_InternInPlace<\/code>\u51fd\u6570\u88ab\u8c03\u7528\u5904\u6709\u4e00\u6761\u6ce8\u91ca\uff0c\u5b83\u95ee\u9053\uff0c\u6211\u4eec\u662f\u5426\u771f\u7684\u9700\u8981\u5bf9\u6240\u6709\u5b57\u5178\u4e2d\u7684\u5168\u90e8\u952e\u8fdb\u884c\u9a7b\u7559\uff1f<\/p>\n<pre><code>int PyDict_SetItemString(PyObject *v, const char *key, PyObject *item) {     PyObject *kv;     int err;     kv = PyUnicode_FromString(key);     if (kv == NULL)         return -1;      \/\/ Invoking String Interning on the key     PyUnicode_InternInPlace(&amp;kv); \/* XXX Should we really? *\/      err = PyDict_SetItem(v, kv, item);     Py_DECREF(kv);     return err; } <\/code><\/pre>\n<h3>5.3 \u4efb\u4f55\u5bf9\u8c61\u7684\u5c5e\u6027<\/h3>\n<p>Python \u4e2d\u5bf9\u8c61\u7684\u5c5e\u6027\u53ef\u4ee5\u901a\u8fc7<code>setattr<\/code>\u51fd\u6570\u663e\u5f0f\u5730\u8bbe\u7f6e\uff0c\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u7c7b\u6210\u5458\u7684\u4e00\u90e8\u5206\u800c\u9690\u5f0f\u5730\u8bbe\u7f6e\uff0c\u6216\u8005\u5728\u5176\u6570\u636e\u7c7b\u578b\u4e2d\u9884\u5b9a\u4e49\u3002<\/p>\n<p><strong>CPython \u4f1a\u9a7b\u7559\u6240\u6709\u8fd9\u4e9b\u5c5e\u6027\u540d\uff0c\u4ee5\u4fbf\u5b9e\u73b0\u5feb\u901f\u67e5\u627e\u3002<\/strong> \u4ee5\u4e0b\u662f\u51fd\u6570<code>PyObject_SetAttr<\/code>\u7684\u4ee3\u7801\u7247\u6bb5\uff0c\u8be5\u51fd\u6570\u5b9a\u4e49\u5728\u6587\u4ef6object.c \u4e2d\uff0c\u8d1f\u8d23\u4e3a Python \u5bf9\u8c61\u8bbe\u7f6e\u65b0\u5c5e\u6027\u3002<\/p>\n<pre><code>int PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) {      ........      PyUnicode_InternInPlace(&amp;name);      ........ } <\/code><\/pre>\n<h3>5.4 \u663e\u5f0f\u5730\u9a7b\u7559<\/h3>\n<p><strong>Python \u8fd8\u652f\u6301\u901a\u8fc7<code>sys<\/code>\u6a21\u5757\u4e2d\u7684<code>intern<\/code>\u51fd\u6570\u8fdb\u884c\u663e\u5f0f\u5730\u5b57\u7b26\u4e32\u9a7b\u7559\u3002<\/strong> <\/p>\n<p>\u5f53\u4f7f\u7528\u4efb\u4f55\u5b57\u7b26\u4e32\u5bf9\u8c61\u8c03\u7528\u6b64\u51fd\u6570\u65f6\uff0c\u8be5\u5b57\u7b26\u4e32\u5bf9\u8c61\u5c06\u88ab\u9a7b\u7559\u3002\u4ee5\u4e0b\u662f sysmodule.c \u6587\u4ef6\u7684\u4ee3\u7801\u7247\u6bb5\uff0c\u5b83\u5c55\u793a\u4e86\u5728<code>sys_intern_impl<\/code>\u51fd\u6570\u4e2d\u7684\u5b57\u7b26\u4e32\u9a7b\u7559\u8fc7\u7a0b\u3002<\/p>\n<pre><code>static PyObject * sys_intern_impl(PyObject *module, PyObject *s) {      ........      if (PyUnicode_CheckExact(s)) {         Py_INCREF(s);         PyUnicode_InternInPlace(&amp;s);         return s;     }      ........ } <\/code><\/pre>\n<h2>6 \u3001\u5b57\u7b26\u4e32\u9a7b\u7559\u7684\u5176\u5b83\u53d1\u73b0<\/h2>\n<p><strong>\u53ea\u6709\u7f16\u8bd1\u671f\u7684\u5b57\u7b26\u4e32\u4f1a\u88ab\u9a7b\u7559\u3002<\/strong> \u5728\u89e3\u91ca\u65f6\u6216\u7f16\u8bd1\u65f6\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u4f1a\u88ab\u9a7b\u7559\uff0c\u800c\u52a8\u6001\u521b\u5efa\u7684\u5b57\u7b26\u4e32\u5219\u4e0d\u4f1a\u3002<\/p>\n<blockquote>\n<p>Python \u732b\u6ce8\uff1a\u8fd9\u4e00\u6761\u89c4\u5219\u503c\u5f97\u5c55\u5f00\u601d\u8003\uff0c\u6211\u66fe\u7ecf\u5728\u4e0a\u9762\u8e29\u8fc7\u5751\u2026\u2026\u6709\u4e24\u4e2a\u77e5\u8bc6\u70b9\uff0c\u6211\u76f8\u4fe1 99% \u7684\u4eba\u90fd\u4e0d\u77e5\u9053\uff1a\u5b57\u7b26\u4e32\u7684 join() \u65b9\u6cd5\u662f\u52a8\u6001\u521b\u5efa\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u5176\u521b\u5efa\u7684\u5b57\u7b26\u4e32\u4e0d\u4f1a\u88ab\u9a7b\u7559\uff1b\u5e38\u91cf\u6298\u53e0\u673a\u5236\u4e5f\u53d1\u751f\u5728\u7f16\u8bd1\u671f\uff0c\u56e0\u6b64\u6709\u65f6\u5019\u5bb9\u6613\u628a\u5b83\u8ddf\u5b57\u7b26\u4e32\u9a7b\u7559\u641e\u6df7\u6dc6\u3002\u63a8\u8350\u9605\u8bfb\u300ajoin()\u65b9\u6cd5\u7684\u795e\u5947\u7528\u5904\u4e0e Intern \u673a\u5236\u7684\u8f6f\u808b\u300b<\/p>\n<\/blockquote>\n<p><strong>\u5305\u542b ASCII \u5b57\u7b26\u548c\u4e0b\u5212\u7ebf\u7684\u5b57\u7b26\u4e32\u4f1a\u88ab\u9a7b\u7559\u3002<\/strong> \u5728\u7f16\u8bd1\u671f\u95f4\uff0c\u5f53\u5bf9\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u8fdb\u884c\u9a7b\u7559\u65f6\uff0cCPython \u786e\u4fdd\u4ec5\u5bf9\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f<code>[a-zA-Z0-9_]*<\/code>\u7684\u5e38\u91cf\u8fdb\u884c\u9a7b\u7559\uff0c\u56e0\u4e3a\u5b83\u4eec\u975e\u5e38\u8d34\u8fd1\u4e8e Python \u7684\u6807\u8bc6\u7b26\u3002<\/p>\n<blockquote>\n<p>Python \u732b\u6ce8\uff1a\u5173\u4e8e Python \u4e2d\u6807\u8bc6\u7b26\u7684\u547d\u540d\u89c4\u5219\uff0c\u5728 Python2 \u7248\u672c\u53ea\u6709\u201c\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\u201d\uff0c\u4f46\u5728 Python 3.x \u7248\u672c\u4e2d\uff0c\u5df2\u7ecf\u652f\u6301 Unicode \u7f16\u7801\u3002\u8fd9\u90e8\u5206\u5185\u5bb9\u63a8\u8350\u9605\u8bfb\u300a\u9192\u9192\uff01 Python \u5df2\u7ecf\u652f\u6301\u4e2d\u6587\u53d8\u91cf\u540d\u5566\uff01\u300b<\/p>\n<\/blockquote>\n<h2>\u53c2\u8003\u6750\u6599<\/h2>\n<ul>\n<li>\u5b57\u7b26\u4e32\u9a7b\u7559( https:\/\/en.wikipedia.org\/wiki\/String_interning)<\/li>\n<li>CPython \u4f18\u5316( https:\/\/stummjr.org\/post\/cpython-optimizations\/)<\/li>\n<li>Python \u5bf9\u8c61\u7b2c\u4e09\u90e8\u5206\uff1a\u5b57\u7b26\u4e32\u9a7b\u7559( https:\/\/medium.com\/@bdov_\/https-medium-com-bdov-python-objects-part-iii-string-interning-625d3c7319de)<\/li>\n<li>Python \u5b57\u7b26\u4e32\u9a7b\u7559\u7684\u5185\u90e8\u539f\u7406( http:\/\/guilload.com\/python-string-interning\/)<\/li>\n<li>Python \u4f18\u5316\u673a\u5236\uff1a\u5e38\u91cf\u6298\u53e0( https:\/\/mp.weixin.qq.com\/s\/p1Zb_linFLWwPlNyA5Ui1Q)<\/li>\n<li>join()\u65b9\u6cd5\u7684\u795e\u5947\u7528\u5904\u4e0e Intern \u673a\u5236\u7684\u8f6f\u808b( https:\/\/mp.weixin.qq.com\/s\/M2uHVqaHe_nyO5jT60V_6Q)<\/li>\n<\/ul><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165 Python \u89e3\u91ca\u5668\u6e90\u7801\uff0c\u6211&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/343185"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=343185"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/343185\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=343185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=343185"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=343185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}