Oldes / Rebol-wishes

Repository for keeping Rebol related wishes out of issues
0 stars 0 forks source link

Optimize `map!` datatype not to use a hash table when length is small enough #57

Open Oldes opened 1 year ago

Oldes commented 1 year ago

Currently even small maps with just a few keys are using hash tables, so they use more memory and lookup is actually slower, because to go simply thru plain few keys is faster than making a hash value and looking for it in the hash table.

Memory use may be demonstrated:

>> profile [[make map! 10][make block! 10]]

Running 2 code blocks 10 times.
----------------------------------------------------------------------------------------------
Time               | Evals  | S.made | S.expa | Memory      | Code
1.0x (900ns)       | 2      | 1      | 0      | 352         | [make block! 10]
1.33x (1μs)        | 2      | 2      | 0      | 1792        | [make map! 10]
----------------------------------------------------------------------------------------------

Select on small block may be with similar time like on map:

>> profile/times [[select [a 1 b 2 c 3] 'b][select #(a 1 b 2 c 3) 'b]] 1000

Running 2 code blocks 1000 times.
----------------------------------------------------------------------------------------------
Time               | Evals  | S.made | S.expa | Memory      | Code
1.0x (781ns)       | 2      | 0      | 0      | 0           | [select [a 1 b 2 c 3] 'b]
1.09x (850ns)      | 2      | 0      | 0      | 0           | [select #(a: 1 b: 2 c: 3) 'b]
----------------------------------------------------------------------------------------------