{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Is deepcopy a good idea?\n", "\n", "The idea of deepcopy clearly has merits: Prevent typing fatigue with the programmer and permit the developer to be lazy about copying objects.\n", "\n", "Copies, however always come at a cost. Let's measure it." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import copy\n", "\n", "class BaseMessage(object):\n", " def __init__(self, sender, receiver, topic):\n", " self.sender = sender\n", " self.receiver = receiver\n", " self.topic = topic\n", " \n", " def copy(self):\n", " return copy.deepcopy(self)\n", "\n", " def old_school_copy(self):\n", " return type(self)(**{k:v for k,v in self.__dict__.items() if not k.startswith(\"_\")})\n", "\n", " def classic_copy(self):\n", " return BaseMessage(self.sender,self.receiver,self.topic)\n", "\n", "class NewMessage(BaseMessage):\n", " def __init__(self, sender, receiver, topic, payload):\n", " super().__init__(sender,receiver,topic)\n", " self.payload = payload \n", " \n", " def classic_copy(self):\n", " return NewMessage(self.sender,self.receiver,self.topic, self.payload)\n", " " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "m1 = BaseMessage(1,2,3)\n", "m2 = NewMessage(1,2,3,\"payload\")\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.69 µs ± 47.5 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n" ] } ], "source": [ "%timeit m1.copy()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5.15 µs ± 47.4 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n" ] } ], "source": [ "%timeit m2.copy()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "874 ns ± 12.7 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)\n" ] } ], "source": [ "%timeit m1.old_school_copy()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.21 µs ± 20.8 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)\n" ] } ], "source": [ "%timeit m2.old_school_copy()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "274 ns ± 4.03 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)\n" ] } ], "source": [ "%timeit m1.classic_copy()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "468 ns ± 1.43 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)\n" ] } ], "source": [ "%timeit m2.classic_copy()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's summarize:\n", "\n", "|message| `copy`|`old_school_copy`| `classic_copy`|\n", "|---|---|---|---|\n", "| m1 | 4.69µs | 0.874µs | 0.274µs |\n", "| m2 | 5.15µs | 1.21µs | 0.468µs |\n", "\n", "Observations:\n", "- deepcopy does some extra work, to assure decoupling at depth, but is 17x slower that \"classic\" and 5x slower than `old_school`.\n", "- old school copy doesn't provide a guarantee that the subclass doesn't have a variable that startswith \"_\".\n", "- `classic` is the fastest method, but requires the developer to be explicit.\n", "\n", "As messages are compact data packages of central to a kernel we hence have to ask ourselves:\n", "\n", "> Would you accept a 17x slowdown of the most used kernel operation?\n", "\n", "Probably not.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "pages310", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.11" } }, "nbformat": 4, "nbformat_minor": 2 }