[Cuis-dev] Small dictionaries

Jan Vraný Jan.Vrany at labware.com
Mon Jan 12 06:01:11 PST 2026


On Mon, 2026-01-12 at 10:35 -0300, Juan Vuletich via Cuis-dev wrote:
> Hi Luciano,
> 
> On 2026-01-12 4:52 AM, Luciano Notarfrancesco via Cuis-dev wrote:
> > In the software Iʼm currently programming Iʼm using the events 
> > subsystem a lot, and Iʼm subclassing from ActiveModel. Each model 
> > typically only triggers 2-3 types of events, so the action dictionary 
> > is very small. And I have another similar case with a small dictionary 
> > (also typically 2-3 entries, at most 5). Lookups and event triggering 
> > happen at about 300 Hz. Iʼm wondering, wouldnʼt be more efficient to 
> > just use arrays and a linear lookup instead of dictionaries? Or, has 
> > anyone implemented dictionaries optimized for a small number of 
> > entries? Any advice?
> > 
> > In general, I think we donʼt have models that trigger many different 
> > types of events, so it seems this optimization could be used in 
> > ActionModel. But perhaps it hasnt been a priority to optimize this 
> > because we donʼt usually fire events at high rates, I guess.
> > 
> > Thanks,
> > Luciano
> 
> 
> I think such optimization would belong in Dictionary. I mean, if a 
> Dictionary is small, it could use an alternative lookup method if that 
> improves performance. Having a different class would make use way more 
> complicated, and we'd need to keep an eye on whether we're using the 
> correct kind (small / large) all the time.
> 
> Still, I doubt there would be any performance improvement. Especially in 
> IdentityDictionaries where the hash and equality are both quick 
> primitives, it looks like it would perform the same as today.
> 
> Some benchmarking is in order, if you want to investigate.

I did the benchmarking in the past. Expecially for small identity dictionaries
it made a difference. 

It makes sense. If you organize small identity dictionary as a an array of 
alternating key/value, then you can squeeze 4 elements into a single cacheline.

In that respect, the traditional smalltalk dictionary implementation using an
array of associations is somewhat suboptimal. Simply one big array or 
having two arrays one with keys and other with values seems more cache-friendly. 

Jan


> 
> Thanks,
> 
> -- 
> Juan Vuletich
> http://www.cuis.st
> http://github.com/jvuletich
> http://researchgate.net/profile/Juan-Vuletich
> http://independent.academia.edu/JuanVuletich
> http://patents.justia.com/inventor/juan-manuel-vuletich


More information about the Cuis-dev mailing list