googleapis / googleapis/google-api-python-client

Option to skip per-method docstring generation in build() (memory / high-concurrency)

オープン
#2,779 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Python
スター
8.9k
フォーク
2.6k
平均マージ
2日 28分
マージ済み PR(30日)
17

説明

## Feature request: an option to skip per-method docstring generation in `build()`

### Problem

`build()` (and lazy sub-resource construction) generates a fully-expanded, recursively
pretty-printed prototype of each method's **response schema** and attaches it as
`method.__doc__` (`discovery.createMethod` → `schema.Schemas.prettyPrintSchema` /
`prettyPrintByName`). For APIs with large, deeply-nested schemas this is very expensive, and it
is paid **every time a service/resource is constructed**.

Concrete numbers from profiling Sheets v4 (`google-api-python-client==2.198.0`, Python 3.13),
measured with RSS (no tracemalloc, to avoid its overhead):

- Building the service, then touching one sub-resource (`service.spreadsheets()`, **no API
call**): **~66 MB**.
- Of that, ~99.9% is the docstring schema expansion — no-oping `prettyPrintSchema`/
`prettyPrintByName` drops it to **~1 MB**. The `.spreadsheets()` methods themselves are ~24 KB.
- The docstrings are only useful for interactive `help()`; in a server they are never read.

### Impact

In a concurrent server (a fresh service built per request, common with per-user credentials),
these allocations are **not shared** across in-flight requests. 8 concurrent Sheets requests
each build ~66 MB of docstrings simultaneously ≈ **530 MB peak**, which OOM-kills a
memory-limited container. This is the concurrent-peak sibling of the long-standing
reference-cycle memory issue in #535 (whose recommended fix — build/reuse a single service — is
not always feasible when credentials differ per request).

### Request

A supported way to skip docstring generation at build time, e.g.:

```python
build("sheets", "v4", credentials=creds, generate_docstrings=False)
# or a module/env toggle
```

Today the only options are to monkeypatch `Schemas.prettyPrintSchema`/`prettyPrintByName`
(fragile across versions) or fork. A first-class flag would let memory-constrained / high-
concurrency deployments opt out of documentation strings they never use.

### Environment

- `google-api-python-client==2.198.0`, Python 3.13
- Reproly: build any large-schema API (Sheets v4), touch a sub-resource, measure RSS; repeat
concurrently to see the multiplier.

Related: #535 (memory from repeated `build()` / reference cycles).

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

Start at build() and trace discovery.createMethod, schema.Schemas.prettyPrintSchema, and prettyPrintByName to see where response-schema docstrings are generated during service and lazy sub-resource construction. Reproduce the Sheets v4 memory profile described in the issue; done means a supported build-time option skips that generation without affecting normal construction when enabled.

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

評価

技術スタック
python
領域
api, backend
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
52/100

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

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