I guess what I want is a struct like data structure, but in an interpreted language
Mightve typed to fast...
I guess what I want is a struct like data structure, but in an interpreted language
Mightve typed to fast...
Essentially, you have an array/list/vector under the hood, and a mapping of field names to array indices to govern field access.
If you're trying to implement this at a lower level, in the interpreted language itself, take a look at the slots mechanism[2] in Python.
[1]: https://docs.python.org/3/library/collections.html#collectio...
[2]: https://docs.python.org/3/reference/datamodel.html#object.__...
I got thrown off by "but in an interpreted language" so I'm not sure whether you're requesting this for C or for another language. In JS, at least the V8 engine does some of this for you: if you have a group of objects that always have the same set of properties, V8 will detect this in many cases and optimize them under the hood as a struct or class (it must also store the string keys somewhere since those can be iterated in JS).
In C/C++ you'd have to store the keys as strings somehow, and also add some way to index the struct via those keys (since you can't even do that with a normal struct). You may be stuck with a hashmap one way or another.
[0] https://llvm.org/docs/CodingStandards.html#do-not-use-rtti-o...
[1] https://google.github.io/styleguide/cppguide.html#Run-Time_T...
It also depends on how you can represent the string and their typical size. If the strings are small, then allocating fixed size chunks to store them inline might make sense. If the strings are long, it might make sense to go crazy and use a "trie" data structure to minimize comparisons.
I believe python uses hash maps for classes by default, but you can alternatively use "slots", which are less mutable and presumably more densely packed.
Python ffi is an option: https://cffi.readthedocs.io/en/latest/using.html#working-wit...
Though I don't know what advantage that route would have over most interpreted language's already existing associative arrays / hashmaps / dicts.
There may be some module already built that serves your needs. DiscoDB is a good example..a very fast hashmap that uses mmap(). https://discodb.readthedocs.io/en/latest/
You can use a static instance if you just need a single object.