Skip to content
This repository has been archived by the owner on Dec 3, 2022. It is now read-only.

decode to object - make lookup faster #82

Open
milahu opened this issue Oct 10, 2020 · 0 comments
Open

decode to object - make lookup faster #82

milahu opened this issue Oct 10, 2020 · 0 comments

Comments

@milahu
Copy link

milahu commented Oct 10, 2020

what puzzles me is:
why are mappings always decoded into dense arrays
and not into sparse arrays or objects or maps (on demand)?

objects or maps would make 'lookup by column ID' much faster
since the column ID would be used as key

currently we must use some search algorithm (binary search, etc)
to find a segment by its column ID

the only problem i see is the (space and time) overhead of objects / maps
but is it worth .. ?

seems there are two situations:

  1. only a few lookups are done, then dense array + expensive search is faster
  2. many lookups are done, then sparse/object/map + cheap search is faster

= initial cost vs running costs

if you need some code to look at

const mappings_line_dense_array = [
  [0], [2, 0, 0, 2], [4], [8]
];

const mappings_line_sparse_array = [
//0    1  2         3  4    5 6 7  8
  null, , [0, 0, 2], , null, , , , null
];

const mappings_line_object = {
  '0': null, '2': [0, 0, 2], '4': null, '8': null
};

const mappings_line_map = new Map([
  [0, null], [2, [0, 0, 2]], [4, null], [8, null]
]);

edit: or store mappings as new Map() with two-dimensional keys [line, column]

const mappings_map = new Map([
  [[0, 0], null],
  [[0, 2], [0, 0, 2]],
  [[0, 4], null],
  [[0, 8], null]
]);

my intuition says:

  • prefer sparse array, as this is easier to manipulate (insert, concat)
    using sparse array, we delegate optimizations to the javascript implementation
  • object should be slower due to integer-to-string conversion of keys
  • map can only append keys (no random write access, insert/concat is expensive)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant