significant speedup for "to_scalar_or_list"
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 18.8k
- Fork
- 2.8k
- Merge medio
- 16h 26m
- PR unite (30g)
- 21
Descrizione
hello,
While investigating a slowness in plotly, I have stumbled upon the to_scalar_or_list function (https://github.com/plotly/plotly.py/blob/abd86092e048d5c8b02da65123824b75e4311838/packages/python/plotly/_plotly_utils/basevalidators.py#L30) that was taking much time.
After some tinkering, I came with the two following changes that vastly improves the performance:
-
move out of the function the lines 38/39 (https://github.com/plotly/plotly.py/blob/abd86092e048d5c8b02da65123824b75e4311838/packages/python/plotly/_plotly_utils/basevalidators.py#L38) with the
get_moduleas it is slow and run each time the function is called (when handling a list of 10k elements, 10k calls) ==> can this be done once in plotly instead of dynamically in each function ? (I see the get_module is also used in many other places in the package) -
move the simplest case (v is a basic type) first as for the case of an iterable of size N, it will first do lot of complex tests for the iterable and then N times also all the complex tests for each items.
So at the end, it looks like
np = get_module("numpy", should_load=False)
pd = get_module("pandas", should_load=False)
# Utility functions
# -----------------
def to_scalar_or_list(v):
# Handle the case where 'v' is a non-native scalar-like type,
# such as numpy.float32. Without this case, the object might be
# considered numpy-convertable and therefore promoted to a
# 0-dimensional array, but we instead want it converted to a
# Python native scalar type ('float' in the example above).
# We explicitly check if is has the 'item' method, which conventionally
# converts these types to native scalars.
# check first for the simple case
if isinstance(v,(int,float,str)):
return v
if np and np.isscalar(v) and hasattr(v, "item"):
return v.item()
if isinstance(v, (list, tuple)):
return [to_scalar_or_list(e) for e in v]
elif np and isinstance(v, np.ndarray):
if v.ndim == 0:
return v.item()
return [to_scalar_or_list(e) for e in v]
elif pd and isinstance(v, (pd.Series, pd.Index)):
return [to_scalar_or_list(e) for e in v]
elif is_numpy_convertable(v):
return to_scalar_or_list(np.array(v))
else:
return v
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia in packages/python/plotly/_plotly_utils/basevalidators.py, in to_scalar_or_list, e analizza l’uso nelle vicinanze di get_module e is_numpy_convertable. Confronta il comportamento e le prestazioni per scalari nativi, liste o tuple, array NumPy e valori Series o Index di pandas. Il lavoro è completato quando i casi proposti mantengono le conversioni esistenti evitando al contempo elaborazioni ripetute non necessarie.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- numpy, pandas, python
- Ambito
- performance
- Tipo di issue
- Refactoring
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 38/100