python / python/cpython

Adjust the value in objects' insertion order array to the index - position.

未关闭
#138,453 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

3.15 interpreter-core performance type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Currently, we store an array of bytes at the end of the inline value array of objects to record the insertion order.
This is expensive as we need to compute not only the value to insert, but where to insert it. Here's the code:

     PyDictValues *values = _PyObject_InlineValues(owner_o);
     Py_ssize_t index = value_ptr - values->values;
     _PyDictValues_AddToInsertionOrder(values, index);

_PyObject_InlineValues requires some lookup, as it depends on tp->basicsize

    return (PyDictValues *)((char *)obj + tp->tp_basicsize);

but it is _PyDictValues_AddToInsertionOrder that is the slowest:

    int size = values->size;
    uint8_t *array = (uint8_t *)&values->values[values->capacity];
    array[size] = (uint8_t)ix;
    values->size = size+1;

If instead of storing the delta of the index and position, instead of just the index, for the majority of objects the insert order array will go from { 0, 1, 2, 3, 4, ... } to { 0, 0, 0, 0, 0, ... }
And if the delta is zero, we don't need to store anything.

     PyDictValues *values = _PyObject_InlineValues(owner_o);
     Py_ssize_t index = value_ptr - values->values;
     Py_ssize_t delta = index - values->size;
     if (delta != 0) {
        /* This is the expensive part */
        _PyDictValues_AddToInsertionOrder(values, delta);
    }
    values->size++;

In the JIT we can track the size of the inline values, and know when delta will be zero. Reducing the above code to

    values->size = KNOWN_SIZE;

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先跟踪 _PyObject_InlineValues 和 _PyDictValues_AddToInsertionOrder,然后检查 issue 中描述的 JIT 路径,以了解如何跟踪 inline-value 的大小。当插入顺序存储使用提议的零增量行为而不改变对象顺序,并且受影响的运行时行为仍然正确时,即可视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
c, python
领域
backend, performance
Issue 类型
重构
难度
4/5
预计耗时
3-5 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。