Skip to content

Commit 0ee0cd6

Browse files
committed
Fix performance regression from dotnet#22390 on Linux
1 parent 9f611bf commit 0ee0cd6

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/corefx/System.Globalization.Native/pal_collation.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,11 +444,15 @@ const UCollator* GetCollatorFromSortHandle(SortHandle* pSortHandle, int32_t opti
444444

445445
TCollatorMap* map = (TCollatorMap*)malloc(sizeof(TCollatorMap));
446446
map->key = options;
447-
void* entry = tsearch(map, &pSortHandle->collatorsPerOptionRoot, TreeComparer);
448-
if ((*(TCollatorMap**)entry) == map)
447+
// tfind on glibc is significantly faster than tsearch and we expect
448+
// to hit the cache here often so it's benefitial to prefer lookup time
449+
// over addition time
450+
void* entry = tfind(map, &pSortHandle->collatorsPerOptionRoot, TreeComparer);
451+
if (entry == NULL)
449452
{
450453
pCollator = CloneCollatorWithOptions(pSortHandle->regular, options, pErr);
451454
map->UCollator = pCollator;
455+
tsearch(map, &pSortHandle->collatorsPerOptionRoot, TreeComparer);
452456
}
453457
else
454458
{

0 commit comments

Comments
 (0)