Repository navigation
Conversation
Metal's binding cache walked nested std::maps for every bind and allocated nodes in each pass, so it now uses flat arrays. Binding also stops touching reference counts. The cache compares unretained pointers, the buffer and sampler getters return references, and a texture resolves its Metal object once when it is created instead of on every bind.
There was a problem hiding this comment.
Code Review
This pull request optimizes the PassBindingsCacheMTL in Impeller's Metal backend by replacing std::map lookups with fixed-size arrays and utilizing __unsafe_unretained pointers to eliminate reference counting overhead during draw calls. It also updates DeviceBufferMTL and SamplerMTL to return references to their underlying Metal objects and adds unit tests for the bindings cache. The review feedback highlights a performance regression in render_pass_mtl.mm where a ternary operator triggers ARC retain/release overhead, and identifies misleading validation log messages in SetTexture and SetSampler that refer to buffers instead of textures or samplers.
SetTexture and SetSampler both reported a buffer.
gaaclarke
left a comment
There was a problem hiding this comment.
This is looking good. Happy with the data structure changes. There are some annotations that are superfluous for optimized builds. (We should keep the definitions of those getters in the headers though so they can be inlined easily)
| ~DeviceBufferMTL() override; | ||
|
|
||
| id<MTLBuffer> GetMTLBuffer() const; | ||
| const id<MTLBuffer>& GetMTLBuffer() const { return buffer_; } |
There was a problem hiding this comment.
This is inefficient, an id should just be a pointer. I tested this code and with -O2 it's equivalent, so no reason for the ref.
| uint64_t index, | ||
| uint64_t offset, | ||
| id<MTLBuffer> buffer); | ||
| __unsafe_unretained id<MTLBuffer> buffer); |
There was a problem hiding this comment.
These directives are unnecessary too in O2, can we avoid them?
There was a problem hiding this comment.
These do change the output. Without them each setter retains its argument on entry and releases it on every exit at -O2, cache hits included.
| /// The texture, without acquiring it or touching its reference count, for | ||
| /// use once per bind. Nil for a drawable, which only `GetMTLTexture` can | ||
| /// acquire. | ||
| const id<MTLTexture>& GetResolvedMTLTexture() const { |
| ~SamplerMTL() override; | ||
|
|
||
| id<MTLSamplerState> GetMTLSamplerState() const; | ||
| const id<MTLSamplerState>& GetMTLSamplerState() const { return state_; } |
There was a problem hiding this comment.
Same here, having the definition in the header lets it be inlined which is sufficient.
The getters are inline, so returning by value compiles to the same code as returning a reference.
Related to #193745. Split out of #196621.
Metal's pass binding cache walked nested
std::maps on every bind and allocated nodes in each pass. It now uses flat arrays and compares unretained pointers, so a bind no longer touches reference counts. The buffer and sampler getters return references, and a texture resolves its Metal object once when it is created instead of on every bind.Pre-launch Checklist
///).