Hash :
9b00af52
Author :
Date :
2023-02-01T11:10:32
Metal: Add an in-memory MTLLibrary cache. Add a small cache for (msl + compile parameters) -> MTLLibrary at the egl::Display level. In regular executions of Chrome, the same shaders (particularly vertex) are compiled multiple times in different programs. Tested for a regular Chrome startup + open wikipedia + motionmark 1.2: 112/282 (40%) cache hits. Several different caching methods were profiled (LinkProgram perf test) - struct key with std::map : 303309 - struct key with std::unordered_map : 308090 - binary blob key with std::map : 263595 - binary blob key with std::unordered_map : 286051 - struct key + is_transparent with std::unordered_map : 304877 - struct key + is_transparent with absl::flat_hash_map : 335686 Using is_transparent allows us to search the hash map without copying the shader source string to construct the key structure. Bug: chromium:1385510 Change-Id: Ieec4ba526fe286276a4af7114d89cde32a8f9e1d Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4214012 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Geoff Lang <geofflang@chromium.org> Reviewed-by: Kenneth Russell <kbr@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
//
// Copyright 2023 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// mtl_library_cache.h:
// Defines classes for caching of mtl libraries
//
#ifndef LIBANGLE_RENDERER_METAL_MTL_LIBRARY_CACHE_H_
#define LIBANGLE_RENDERER_METAL_MTL_LIBRARY_CACHE_H_
#include "libANGLE/renderer/metal/mtl_utils.h"
#include <type_traits>
namespace rx
{
namespace mtl
{
class LibraryCache
{
public:
LibraryCache() = default;
~LibraryCache() = default;
AutoObjCPtr<id<MTLLibrary>> getOrCompileShaderLibrary(
ContextMtl *context,
const std::string &source,
const std::map<std::string, std::string> ¯os,
bool enableFastMath,
AutoObjCPtr<NSError *> *errorOut);
private:
struct LibraryKey
{
std::string source;
std::map<std::string, std::string> macros;
bool enableFastMath;
using LValueTuple = decltype(std::tie(std::as_const(source),
std::as_const(macros),
std::as_const(enableFastMath)));
LibraryKey() = default;
explicit LibraryKey(const LValueTuple &fromTuple);
LValueTuple tie() const;
};
struct LibraryKeyCompare
{
// Mark this comparator as transparent. This allows types that are not LibraryKey to be used
// as a key for unordered_map::find. We can avoid the construction of a LibraryKey (which
// copies std::strings) when searching the cache.
using is_transparent = void;
// Hash functions for keys and lvalue tuples of keys
size_t operator()(const LibraryKey::LValueTuple &k) const;
size_t operator()(const LibraryKey &k) const;
// Comparison operators for all key/lvalue combinations need by a map
bool operator()(const LibraryKey &a, const LibraryKey &b) const;
bool operator()(const LibraryKey &a, const LibraryKey::LValueTuple &b) const;
bool operator()(const LibraryKey::LValueTuple &a, const LibraryKey &b) const;
};
angle::HashMap<LibraryKey, AutoObjCPtr<id<MTLLibrary>>, LibraryKeyCompare, LibraryKeyCompare>
mCache;
};
} // namespace mtl
} // namespace rx
#endif // LIBANGLE_RENDERER_METAL_MTL_LIBRARY_CACHE_H_