Skip to content

Commit

Permalink
SPMI: Avoid duplicate key lookups on replay (dotnet#78597)
Browse files Browse the repository at this point in the history
The commonly used pattern AssertMapAndKeyExists followed by map->Get
means we do two key lookups. Change the macros to return the value after
we have verified that the key exists. This makes replay 2-3% faster and
avoids some of the boilerplate.
  • Loading branch information
jakobbotsch authored Nov 21, 2022
1 parent 87a37b8 commit 5005b60
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 322 deletions.
48 changes: 18 additions & 30 deletions src/coreclr/tools/superpmi/superpmi-shared/errorhandling.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,15 @@ void MSC_ONLY(__declspec(noreturn)) ThrowException(DWORD exceptionCode, const ch
LogException(EXCEPTIONCODE_MC, "SuperPMI assertion failed (missing map " #map ")" keymsg, ##__VA_ARGS__); \
} while (0)

#define AssertKeyExists(map, key, keymsg, ...) \
do \
{ \
if (map->GetIndex(key) == -1) \
LogException(EXCEPTIONCODE_MC, "SuperPMI assertion failed (missing key \"" #key "\" in map " #map ")" keymsg, ##__VA_ARGS__); \
} while (0)

#define AssertMapAndKeyExist(map, key, keymsg, ...) \
do \
{ \
if (map == nullptr) \
LogException(EXCEPTIONCODE_MC, "SuperPMI assertion failed (missing map " #map ")" keymsg, ##__VA_ARGS__); \
if (map->GetIndex(key) == -1) \
LogException(EXCEPTIONCODE_MC, "SuperPMI assertion failed (missing key \"" #key "\" in map " #map ")" keymsg, ##__VA_ARGS__); \
} while (0)
#define LookupByKeyOrMiss(map, key, keymsg, ...) \
[&]() { \
if (map == nullptr) \
LogException(EXCEPTIONCODE_MC, "SuperPMI assertion failed (missing map " #map ")" keymsg, ##__VA_ARGS__); \
int index = map->GetIndex(key); \
if (index == -1) \
LogException(EXCEPTIONCODE_MC, "SuperPMI assertion failed (missing key \"" #key "\" in map " #map ")" keymsg, ##__VA_ARGS__); \
return map->GetItem(index); \
}()

// clang doesn't allow for an empty __VA_ARGS__, so we need to pass something non-empty to `LogException`, below.

Expand All @@ -72,21 +66,15 @@ void MSC_ONLY(__declspec(noreturn)) ThrowException(DWORD exceptionCode, const ch
LogException(EXCEPTIONCODE_MC, "SuperPMI assertion failed (missing map " #map ")", ""); \
} while (0)

#define AssertKeyExistsNoMessage(map, key) \
do \
{ \
if (map->GetIndex(key) == -1) \
LogException(EXCEPTIONCODE_MC, "SuperPMI assertion failed (missing key \"" #key "\" in map " #map ")", "");\
} while (0)

#define AssertMapAndKeyExistNoMessage(map, key) \
do \
{ \
if (map == nullptr) \
LogException(EXCEPTIONCODE_MC, "SuperPMI assertion failed (missing map " #map ")", ""); \
if (map->GetIndex(key) == -1) \
LogException(EXCEPTIONCODE_MC, "SuperPMI assertion failed (missing key \"" #key "\" in map " #map ")", "");\
} while (0)
#define LookupByKeyOrMissNoMessage(map, key) \
[&]() { \
if (map == nullptr) \
LogException(EXCEPTIONCODE_MC, "SuperPMI assertion failed (missing map " #map ")", ""); \
int index = map->GetIndex(key); \
if (index == -1) \
LogException(EXCEPTIONCODE_MC, "SuperPMI assertion failed (missing key \"" #key "\" in map " #map ")", ""); \
return map->GetItem(index); \
}()

#define AssertMsg(expr, msg, ...) AssertCodeMsg(expr, EXCEPTIONCODE_ASSERT, msg, ##__VA_ARGS__)
#define Assert(expr) AssertCode(expr, EXCEPTIONCODE_ASSERT)
Expand Down
Loading

0 comments on commit 5005b60

Please sign in to comment.