When designing APIs that interact with remote services, be cautious about implementing convenience features that may introduce significant hidden performance costs. Collection operations like slicing, negative indexing, or operations that process all items can be unexpectedly expensive when they require fetching large amounts of data.
When designing APIs that interact with remote services, be cautious about implementing convenience features that may introduce significant hidden performance costs. Collection operations like slicing, negative indexing, or operations that process all items can be unexpectedly expensive when they require fetching large amounts of data.
Consider these strategies to optimize performance:
limit
to control resource fetchingFor example, instead of supporting negative indexing directly:
# Potentially expensive - fetches all items to get the last one
last_item = collection[-1]
# Better - makes the expensive operation explicit
last_item = collection.to_list()[-1] # User is aware of full collection materialization
# Or provide a direct method
last_item = collection.get_last() # Could be optimized internally
This approach gives users control over performance tradeoffs and avoids surprising them with unexpectedly expensive operations.
Enter the URL of a public GitHub repository