Commit d5014bc
committed
perf(startup): defer heavy imports in 0002_data.py
The IPython startup script imports pandas, matplotlib.pyplot, e2b_charts,
and orjson at module load time. Every Python kernel pays that cost in
resident memory, regardless of whether the customer ever displays a
DataFrame, Figure, or large JSON object — and that resident heap is
captured in the snapshot at every pause.
Move the heavy imports into the formatter __call__ bodies so they are
only resolved when a matching object is actually rendered. Top-level
imports are limited to IPython + traitlets, which is what the formatter
class definitions actually need.
IPython invokes every registered formatter for every displayed object,
so a naive "import pandas" at the top of __call__ would shift the cost
from kernel boot to the first cell with any output. Instead, gate on
sys.modules.get("pandas"): a pandas.DataFrame cannot exist unless the
user has already imported pandas. The same sentinel works for the
matplotlib + e2b_charts dependency chain in E2BChartFormatter. Sessions
that never touch pandas/matplotlib now never load them.
E2BDataFormatter previously declared type_printers={pandas.DataFrame:
…}, which forced pandas at class-definition time. The new version
dispatches via isinstance() inside __call__ — slightly more work per
formatted object, but the heavy dependencies stay unloaded until the
user genuinely needs them.
Signed-off-by: Nikita Kalyazin <nikita.kalyazin@e2b.dev>1 parent b355e7c commit d5014bc
1 file changed
Lines changed: 35 additions & 32 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | 7 | | |
32 | 8 | | |
33 | 9 | | |
34 | 10 | | |
35 | 11 | | |
36 | 12 | | |
37 | 13 | | |
38 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
39 | 31 | | |
40 | 32 | | |
41 | 33 | | |
| |||
45 | 37 | | |
46 | 38 | | |
47 | 39 | | |
48 | | - | |
49 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
50 | 45 | | |
51 | 46 | | |
52 | | - | |
53 | | - | |
54 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
| 62 | + | |
| 63 | + | |
61 | 64 | | |
62 | 65 | | |
63 | 66 | | |
| |||
0 commit comments