Python for Software Engineers
Back to Async, Concurrency, Performance

Build 4 — Memory-Efficient Data Cache & Profiler

Thread-safe LRU cache with __slots__, plus a memory/time profiling decorator. Measure, bound, and prove it.

33 minutesAssignment
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.

Scenario

High-throughput services often suffer from memory bloat due to object overhead and unbounded caches. Build a custom, thread-safe LRU cache with strict memory bounds, optimized with __slots__.

Requirements

  1. Optimized Data Structures: Compare standard classes vs @dataclass(slots=True) vs namedtuples. Measure per-instance footprint with sys.getsizeof and pympler.
  2. Thread-Safe LRU Cache: Implement an LRU cache using a doubly linked list + hash map (or collections.OrderedDict) protected by a reentrant lock (threading.RLock).
  3. Profiling Decorator: Implement @profile_memory_and_time that logs elapsed CPU time and heap allocation delta before and after function execution.

Starter Code Layout

build-4-cache-profiler/
├── src/
│   └── memory_tools/
│       ├── __init__.py
│       ├── cache.py       # Thread-safe bounded LRU cache
│       ├── models.py      # Slots-optimized data structures
│       └── profiler.py    # Memory & execution timing decorator
└── tests/
    └── test_cache.py

Verification Command

pytest tests/test_cache.py -v
python -m memory_tools.benchmark

100-Point Evaluation Rubric

  • Cache Eviction Mechanics (30 pts): Strict O(1) get/put operations and eviction policy.
  • Thread Safety (25 pts): Correct use of RLock during eviction and retrieval.
  • Memory Optimization (25 pts): Demonstrable >40% memory reduction using __slots__.
  • Profiling Tooling (20 pts): Decorator reports accurate timing and allocation deltas.

Grading rubric

Your submission passes when all four hold:

  1. Works: Cache is thread-safe and bounded; slots/namedtuple savings measured; profiler decorator reports time + heap delta.
  2. Tested: a pytest suite covers the happy path plus one failure mode, and all tests pass.
  3. Readable: a README explains setup, the two most important commands, and one design decision you made.
  4. Production habits: structured logging on failures and no hardcoded secrets/paths (env or config file).

Submission contract

Submit a public repo URL containing source, tests, README, and requirements.txt (or pyproject.toml). A reviewer runs your tests and two commands from your README; if either fails on a clean machine, the submission is returned with notes.

Track your learning progressFree

Sign in or enroll free to mark this assignment as complete, save your progress, and earn certificates.