This is an archive of the discontinued LLVM Phabricator instance.

[llvm-profdata] Use StringRef for CallTargetMap
AbandonedPublic

Authored by huangjd on Jun 8 2023, 5:56 PM.

Details

Reviewers
None
Summary

Use StringRef as key to CallTargetMap. Previously CallTargetMap uses a StringMap, which actually stores a copy of the string. All strings put in the map is found to be backed by the profile data so a StringRef can be safely used.

This refactoring is a prerequisite to implement phase 2 of MD5 refactoring, where StringRef can represent an MD5 value directly, instead of casting it to a string first.

Diff Detail

Unit TestsFailed

Event Timeline

huangjd created this revision.Jun 8 2023, 5:56 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 8 2023, 5:56 PM
Herald added subscribers: hoy, wlei, ormris and 3 others. · View Herald Transcript
huangjd requested review of this revision.Jun 8 2023, 5:56 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 8 2023, 5:56 PM

Interestingly we've seen regression on memory usage with similar change. The reason is that an empty DenseMap preallocates a chunk of memory that turns out more expansive than a single string for our workload. cc @marksantaniello @wenlei

Yeah I tested pretty much the same thing, and it was a big memory regression. The problem is that, upon first insert, the DenseMap grows to 64 entries. Even if I use a const char* key, the KV-pair is 16B, so 64 entries is 1K.

Holding on to this patch. Currently I am also exploring writing a specialized container for all the map types used by SampleProfile, given the fact that in production profiles maps typically have 0 or 1 elements (~90%), and map allocation is a huge bottleneck (~25% full profile load time), having a specialized map structure is beneficial

FWIW I also tried SmallDenseMap with various inline sizes. It still seems to be the case that, once you exceed the in-situ space, you immediately grow to 64 entries.

huangjd updated this revision to Diff 531456.Jun 14 2023, 12:29 PM

(temporary) use std::map in place of StringMap

Another thing worth considering is just to use const char* as the map key. I believe it's safe in this case, and smaller than StringRef which is a pointer and a length.

huangjd abandoned this revision.Sep 14 2023, 2:46 PM