spec: from_spec spends about 2 ms per constraint entry, whatever its size

オープン
#1,159 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
18/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
活発
技術スタック
python
領域
performance

調査の方向性

Start with the reproducer in the issue, which times Model.from_spec per constraint entry at two grid sizes. The profile points at where / admits_a_row, _constraints and _objective, so read those in the spec code path first. The issue does not say what to change, and the work is performance investigation with an open design question. Done means a measured drop in per-entry cost that the maintainers accept.

索引モデルが issue の本文から書いたものです。

説明

[!NOTE]
The following content was generated by AI.

Model.from_spec has a fixed cost of about 2 ms per constraint entry. The grid size hardly changes it:

grid 10 entries 200 entries
2 × 2 2.38 ms per entry 2.06 ms per entry
200 × 200 2.87 ms per entry 2.62 ms per entry

Measured on stack/spec-perf (master + #1155, #1156, #1157), Python 3.13, 4 cores, spec lowered beforehand, best of 3.

This dominates real builds: in PyPSA/PyPSA#2128, from_spec is 79–84% of a warm create_model, and a network 300× larger costs only 0.43 s more. A profile of that build points at where / admits_a_row, _constraints and _objective. A spec with one flat name dimension will have fewer entries, but the cost per entry still sets the floor.

Reproducer
import time

import xarray as xr
from linopy import Model
from linopy.spec import lowered


def spec(n: int) -> dict:
    constraints = {
        f"c{i}": {"dims": ["t", "g"], "where": "active", "expression": f"x <= {i + 1}"}
        for i in range(n)
    }
    return {
        "dimensions": {"t": {"dtype": "str"}, "g": {"dtype": "str"}},
        "parameters": {"active": {"dims": ["t", "g"], "dtype": "bool"}},
        "variables": {"x": {"dims": ["t", "g"], "bounds": {"lower": 0}}},
        "constraints": constraints,
        "objective": {"sense": "minimize", "expression": "sum(x)"},
    }


for size in (2, 200):
    t, g = [f"t{i}" for i in range(size)], [f"g{i}" for i in range(size)]
    active = xr.DataArray(True, coords={"t": t, "g": g}, dims=["t", "g"]).copy()
    sources = {"t": t, "g": g, "active": active}
    for n in (10, 200):
        program = lowered(spec(n))
        times = []
        for _ in range(3):
            start = time.perf_counter()
            Model.from_spec(program, sources)
            times.append(time.perf_counter() - start)
        print(f"{size}x{size}, {n} entries: {1000 * min(times) / n:.2f} ms per entry")

Part of PyPSA/PyPSA#2138.

主要言語
Python
スター
264
フォーク
95
平均マージ
11時間 33分
マージ済み PR(30日)
86

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

PyPSA/linopy のほかの issue

PyPSA/linopy の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。