<jupyter_start><jupyter_text>KeyviSophisticated key-value store using shared memory.See [github](https://github.com/KeyviDev/keyvi)  First steps<jupyter_code>%%sh

keyvi compile toy.txt toy.kv key-only
keyvi dump toy.kv toy.kv.txt
data = {"1": "aap", "2": "noot"}
kv = StringDictionaryCompiler()
for (k, v) in data.items():
    kv.Add(k, v)
kv.Compile()
kv.WriteToFile("data.txt")
D = Dictionary("data.txt")
"1" in D
"2" in D
"3" in D
D["1"].GetValueAsString()<jupyter_output><empty_output><jupyter_text>Data preparationWe load the feature `g_word_utf8` from the BHSA.<jupyter_code>path = '_temp/g_word_utf8.tf'
tmObj = Timestamp()
F = Data(path, tmObj)
F._readDataBin()
print(len(F.data))<jupyter_output>426584<jupyter_text>We also compile this data as keyvi data:<jupyter_code>kv = StringDictionaryCompiler()
for (k, v) in F.data.items():
    kv.Add(str(k), v)
kv.Compile()
kv.WriteToFile("_temp/kv/g_word_utf8.txt")
with gzip.open("_temp/kv/g_word_utf8.txt.gz", "wb", compresslevel=GZIP_LEVEL) as fout:
    with open("_temp/kv/g_word_utf8.txt", "rb") as fin:
        fout.write(fin.read())
D = Dictionary("_temp/kv/g_word_utf8.txt")<jupyter_output><empty_output><jupyter_text>Performance of accessing keys:<jupyter_code>def testPerformanceTf(data):
    times = 1000000
    key = 200000
    return timeit("data[key]", globals=locals(), number=times)

testPerformanceTf(F.data)
def testPerformanceKv(data):
    times = 1000000
    key = "200000"
    return timeit("data[key].GetValueAsString()", globals=locals(), number=times)
testPerformanceKv(D)<jupyter_output><empty_output><jupyter_text>ConclusionKeyVI is roughly **10 times as slow** as dictionary lookup.That makes it uninteresting for TF. Measure the memory size of this data:  Measuring RAMNevertheless, we measure the RAM needed.We cannot directly measure the size of D, because it is an object produced by custom C code.We do it in a roundabout way.<jupyter_code>%%sh

python3 -m memory_profiler tfmem.py<jupyter_output><class 'tf.core.data.Data'>
Filename: tfmem.py

Line #    Mem usage    Increment   Line Contents
================================================
     5   40.434 MiB   40.434 MiB   @profile
     6                             def mem():
     7   40.434 MiB    0.000 MiB       path = '_temp/g_word_utf8.tf'
     8   40.434 MiB    0.000 MiB       tmObj = Timestamp()
     9   40.438 MiB    0.004 MiB       F = Data(path, tmObj)
    10   96.426 MiB   55.988 MiB       F._readDataBin()
    11   96.426 MiB    0.000 MiB       print(type(F))<jupyter_text>After reading the TF data, the memory usage has increased by 54 MB.<jupyter_code>%%sh

python3 -m memory_profiler kvmem.py<jupyter_output><class '_core.Dictionary'>
Filename: kvmem.py

Line #    Mem usage    Increment   Line Contents
================================================
     4   40.727 MiB   40.727 MiB   @profile
     5                             def mem():
     6   40.809 MiB    0.082 MiB       D = Dictionary("_temp/kv/g_word_utf8.txt")
     7   40.809 MiB    0.000 MiB       print(type(D))
{'fraction_non_alphanumeric': 0.14812376563528637, 'fraction_numerical': 0.04904542462146149, 'mean_word_length': 3.718944099378882, 'pattern_counts': {'":': 2, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '122448', 'license': 'MIT', 'n_tokens_mistral': 1168, 'n_tokens_neox': 1072, 'n_words': 272, 'path': 'test/keyvi/pandas.ipynb', 'repo_name': 'ancient-data/text-fabric', 'size': '3038'}