Advice on transitioning a wtf::HashMap of max 50 elems to be keyed off gfx::Size

15 views
Skip to first unread message

Colin Blundell

unread,
Feb 7, 2025, 12:37:58 PMFeb 7
to platform-architecture-dev
Hi folks,

I'm transitioning this code away from using SkImageInfo. That HashMap is a cache that is currently keyed by SkImageInfo, but the only non-constant element of the SkImageInfo is the size. I'd like to change it to be keyed by gfx::Size, which currently neither has hashing traits for wtf::HashMap nor the necessary comparators for std::map. Before investing in changing either of those things, I'm wondering: What would folks' recommendations be for the best change to make here?

Thanks,

Colin

Steinar H. Gunderson

unread,
Feb 7, 2025, 12:55:24 PMFeb 7
to Colin Blundell, platform-architecture-dev
On Fri, Feb 07, 2025 at 06:37:40PM +0100, Colin Blundell wrote:
> I'd like to change it to be keyed by gfx::Size, which
> currently neither has hashing traits for wtf::HashMap nor the necessary
> comparators for std::map. Before investing in changing either of those
> things, I'm wondering: What would folks' recommendations be for the best
> change to make here?

If you have max 50 elements, and _especially_ if your typical number is lower
than that, then consider simply a Vector<pair<Size, Value>>. Gives small code
for lookup and insert, and unless your Value is very big (gfx::Size is just
comparing two ints, right?), zooming through them is fast.

Otherwise, you'd usually want HashMap over map. Make your own traits for
hashing, which should be fairly simple for Size.

/* Steinar */
--
Homepage: https://www.sesse.net/

Xianzhu Wang

unread,
Feb 7, 2025, 12:57:44 PMFeb 7
to Steinar H. Gunderson, Colin Blundell, platform-architecture-dev
We have some hash traits for geometry types in renderer/platform/geometry/geometry_hash_traits.h. You can add one for gfx::Size if you really need it.

--
You received this message because you are subscribed to the Google Groups "platform-architecture-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to platform-architect...@chromium.org.
To view this discussion visit https://groups.google.com/a/chromium.org/d/msgid/platform-architecture-dev/20250207175519.ajqxupv3e7ezsvkt%40sesse.net.

Colin Blundell

unread,
Feb 10, 2025, 8:26:00 AMFeb 10
to Xianzhu Wang, Steinar H. Gunderson, Colin Blundell, platform-architecture-dev
Thanks, both! Steinar, that was my intuition as well. I'll go in that direction. It's even simpler than what you outlined because the size is available as a getter off the CanvasResourceProvider (e.g., WebGL and WebGPU have similar caches, and they both just keep a vector and query equality on the relevant fields of the CanvasResourceProvider).

Best,

Colin
Reply all
Reply to author
Forward
0 new messages