<tfoot id='uwufN'></tfoot>
    1. <small id='uwufN'></small><noframes id='uwufN'>

    2. <i id='uwufN'><tr id='uwufN'><dt id='uwufN'><q id='uwufN'><span id='uwufN'><b id='uwufN'><form id='uwufN'><ins id='uwufN'></ins><ul id='uwufN'></ul><sub id='uwufN'></sub></form><legend id='uwufN'></legend><bdo id='uwufN'><pre id='uwufN'><center id='uwufN'></center></pre></bdo></b><th id='uwufN'></th></span></q></dt></tr></i><div id='uwufN'><tfoot id='uwufN'></tfoot><dl id='uwufN'><fieldset id='uwufN'></fieldset></dl></div>

          <bdo id='uwufN'></bdo><ul id='uwufN'></ul>

        <legend id='uwufN'><style id='uwufN'><dir id='uwufN'><q id='uwufN'></q></dir></style></legend>

        元组在 CPython 中是如何实现的?

        时间:2023-09-01
        <legend id='xMer9'><style id='xMer9'><dir id='xMer9'><q id='xMer9'></q></dir></style></legend>

        <tfoot id='xMer9'></tfoot>

          1. <small id='xMer9'></small><noframes id='xMer9'>

            <i id='xMer9'><tr id='xMer9'><dt id='xMer9'><q id='xMer9'><span id='xMer9'><b id='xMer9'><form id='xMer9'><ins id='xMer9'></ins><ul id='xMer9'></ul><sub id='xMer9'></sub></form><legend id='xMer9'></legend><bdo id='xMer9'><pre id='xMer9'><center id='xMer9'></center></pre></bdo></b><th id='xMer9'></th></span></q></dt></tr></i><div id='xMer9'><tfoot id='xMer9'></tfoot><dl id='xMer9'><fieldset id='xMer9'></fieldset></dl></div>

                <tbody id='xMer9'></tbody>

                  <bdo id='xMer9'></bdo><ul id='xMer9'></ul>
                • 本文介绍了元组在 CPython 中是如何实现的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在尝试了解 CPython 是如何在幕后实现的.Python 是高级别的很好,但我不喜欢把它当作一个黑盒子.

                  I've been trying to learn how CPython is implemented under the scenes. It's great that Python is high level, but I don't like treating it like a black box.

                  考虑到这一点,如何实现元组?我看过 源代码 (tupleobject.c),但它超出了我的头脑.

                  With that in mind, how are tuples implemented? I've had a look at the source (tupleobject.c), but it's going over my head.

                  我看到 PyTuple_MAXSAVESIZE = 20PyTuple_MAXFREELIST = 2000,什么是保存和空闲列表"?(长度为 20/21 或 2000/2001 的元组之间会有性能差异吗?是什么强制执行最大元组长度?)

                  I see that PyTuple_MAXSAVESIZE = 20 and PyTuple_MAXFREELIST = 2000, what is saving and the "free list"? (Will there be a performance difference between tuples of length 20/21 or 2000/2001? What enforces the maximum tuple length?)

                  推荐答案

                  请注意,此答案中的所有内容均基于我从查看您链接的实现中收集到的内容.

                  As a caveat, everything in this answer is based on what I've gleaned from looking over the implementation you linked.

                  元组的标准实现似乎只是一个数组.但是,有很多优化可以加快速度.

                  It seems that the standard implementation of a tuple is simply as an array. However, there are a bunch of optimizations in place to speed things up.

                  首先,如果您尝试创建一个空元组,CPython 将返回一个代表空元组的规范对象.因此,它可以节省大量仅分配单个对象的分配.

                  First, if you try to make an empty tuple, CPython instead will hand back a canonical object representing the empty tuple. As a result, it can save on a bunch of allocations that are just allocating a single object.

                  接下来,为了避免分配一堆小对象,CPython 为许多小列表回收内存.有一个固定常量 (PyTuple_MAXSAVESIZE),这样所有小于此长度的元组都有资格回收它们的空间.每当一个长度小于该常数的对象被释放时,与它相关的内存就有可能不会被释放,而是被存储在一个空闲列表"中.(更多关于下一段的内容)基于它的大小.这样,如果您需要分配一个大小为 n 的元组,而其中一个已被分配且不再使用,CPython 可以回收旧数组.

                  Next, to avoid allocating a bunch of small objects, CPython recycles memory for many small lists. There is a fixed constant (PyTuple_MAXSAVESIZE) such that all tuples less than this length are eligible to have their space reclaimed. Whenever an object of length less than this constant is deallocated, there is a chance that the memory associated with it will not be freed and instead will be stored in a "free list" (more on that in the next paragraph) based on its size. That way, if you ever need to allocate a tuple of size n and one has previously been allocated and is no longer in use, CPython can just recycle the old array.

                  空闲列表本身被实现为一个大小为 PyTuple_MAXSAVESIZE 的数组,存储指向未使用元组的指针,其中数组的第 n 个元素指向 NULL(如果没有大小为 n 的额外元组可用)或者到一个大小为 n 的回收元组.如果有多个不同的大小为 n 的元组可以重用,则它们通过将每个元组的第零个入口点指向下一个可以重用的元组,以一种链表的形式链接在一起.(由于只分配了一个长度为零的元组,因此永远不会有读取不存在的第零元素的风险).通过这种方式,分配器可以存储一定数量的每个大小的元组以供重用.为了确保这不会使用太多内存,还有第二个常量 PyTuple_MAXFREELIST 用于控制任何存储桶中任何这些链表的最大长度.然后有一个长度为 PyTuple_MAXSAVESIZE 的辅助数组,它存储每个给定长度的元组的链表长度,这样就不会超过这个上限.

                  The free list itself is implemented as an array of size PyTuple_MAXSAVESIZE storing pointers to unused tuples, where the nth element of the array points either to NULL (if no extra tuples of size n are available) or to a reclaimed tuple of size n. If there are multiple different tuples of size n that could be reused, they are chained together in a sort of linked list by having each tuple's zeroth entry point to the next tuple that can be reused. (Since there is only one tuple of length zero ever allocated, there is never a risk of reading a nonexistent zeroth element). In this way, the allocator can store some number of tuples of each size for reuse. To ensure that this doesn't use too much memory, there is a second constant PyTuple_MAXFREELIST that controls the maximum length of any of these linked lists within any bucket. There is then a secondary array of length PyTuple_MAXSAVESIZE that stores the length of the linked lists for tuples of each given length so that this upper limit isn't exceeded.

                  总而言之,这是一个非常聪明的实现!

                  All in all, it's a very clever implementation!

                  这篇关于元组在 CPython 中是如何实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:变换“元组列表";放入一个平面列表或矩阵 下一篇:为什么元组比 Python 中的列表快?

                  相关文章

                  <legend id='8WVH8'><style id='8WVH8'><dir id='8WVH8'><q id='8WVH8'></q></dir></style></legend>

                • <tfoot id='8WVH8'></tfoot>

                      <small id='8WVH8'></small><noframes id='8WVH8'>

                        <bdo id='8WVH8'></bdo><ul id='8WVH8'></ul>

                      <i id='8WVH8'><tr id='8WVH8'><dt id='8WVH8'><q id='8WVH8'><span id='8WVH8'><b id='8WVH8'><form id='8WVH8'><ins id='8WVH8'></ins><ul id='8WVH8'></ul><sub id='8WVH8'></sub></form><legend id='8WVH8'></legend><bdo id='8WVH8'><pre id='8WVH8'><center id='8WVH8'></center></pre></bdo></b><th id='8WVH8'></th></span></q></dt></tr></i><div id='8WVH8'><tfoot id='8WVH8'></tfoot><dl id='8WVH8'><fieldset id='8WVH8'></fieldset></dl></div>