Minimal proxy (clone) pattern in Blockchain / Solidity - Time & Space Complexity
When using the minimal proxy pattern in blockchain, we want to know how the cost of creating clones grows as we make more copies.
We ask: How does the time to deploy or interact with clones change as we increase the number of clones?
Analyze the time complexity of the following minimal proxy deployment code.
function clone(address implementation) external returns (address instance) {
bytes20 targetBytes = bytes20(implementation);
assembly {
let clone := mload(0x40)
mstore(clone, 0x3d602d80600a3d3981f3)
mstore(add(clone, 0x14), targetBytes)
mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf3)
instance := create(0, clone, 0x37)
}
}
This code creates a minimal proxy clone that points to an existing contract, using a fixed-size bytecode template.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Deploying a clone involves a fixed sequence of memory stores and a single create call.
- How many times: Each clone deployment repeats this fixed sequence once per clone.
Each clone deployment takes about the same time, regardless of how many clones exist before.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 fixed deployments |
| 100 | 100 fixed deployments |
| 1000 | 1000 fixed deployments |
Pattern observation: The total work grows linearly with the number of clones created.
Time Complexity: O(n)
This means the time to deploy clones grows directly in proportion to how many clones you create.
[X] Wrong: "Deploying a clone gets faster as more clones exist because the code is reused."
[OK] Correct: Each clone deployment runs the same fixed steps independently; previous clones do not speed up new deployments.
Understanding how clone deployment scales helps you explain efficient contract creation in blockchain projects.
"What if the clone initialization included a loop over a list of addresses? How would the time complexity change?"