diff --git a/Minecraft Datapacking/When Two Macros are Faster than One.md b/Minecraft Datapacking/When Two Macros are Faster than One.md index e089215..a8ccfcf 100644 --- a/Minecraft Datapacking/When Two Macros are Faster than One.md +++ b/Minecraft Datapacking/When Two Macros are Faster than One.md @@ -5,6 +5,7 @@ draft: "false" While working on my Database datapack (still WIP), I knew I'd want to find the most efficient way to dynamically access dynamically populated arrays. I had some ideas and decided to benchmark them using [Kragast's Benchmark Datapack](https://www.planetminecraft.com/data-pack/benchmark-6443027/). This process was really illuminating to me, and I hope it will be for you as well. Thanks for all the help from **PuckiSilver**, **amandin**, and **Nicoder**. # scenario +The following is the dataset and constraints I used to test different methods of accessing data within an array. ## dataset The testing data is stored in the storage `#_macro.array`. The array is populated with a total of 500 entries, each having `id` and `string` fields. ```json @@ -20,6 +21,14 @@ The testing data is stored in the storage `#_macro.array`. The array is populate } ] ``` +This dataset could also be represented as a table: + +| id | string | +| --- | ---------- | +| 1 | "entry1" | +| ... | ... | +| 500 | "entry500" | + ## constraints The objective is to create an interface that receives a keyword, say `entry500`, and searches `#_macro.array` for an entry where the value of `string` matches the keyword. @@ -75,7 +84,7 @@ data remove storage test_namespace:test_namespace temp.result # two macro Another way to crack the problem is through indexing. This was my original plan when I didn't realize that `...[{string:$(keyword)}]` was possible. -This method requires the creation of an index of the field that is going to be searched. The index will look something like this: +This method requires the creation of an index of the field that is going to be searched. The index is a list of key/value pairs: ```json { entry1: 0, @@ -84,7 +93,7 @@ This method requires the creation of an index of the field that is going to be s entry500: 499 } ``` -The *key*, e.g. `entry2:` corresponds with the value of a `string` field in the main array, while the value `1` indicates the main array index where we'll find the full entry. The index can be searched with a direct path, `index.$(keyword)`, and the main array can also be searched with a direct reference to the entry index, `array.#(index)`. Keep in mind that *the index must already exist prior to running the search function*. In a practical application, an index could be updated every time the main array is updated. A scheduled task could also audit the index to ensure that it's up to date. +The *key*, e.g. `entry2:` corresponds with the value of a `string` field in the main array, while the *value* `1` indicates the main array index where we'll find the full entry. The index can be searched with a direct path, `index.$(keyword)`, and the main array can also be searched with a direct reference to the entry index, `array.#(index)`. Keep in mind that *the index must already exist prior to running the search function*. In a practical application, an index could be updated every time the main array is updated. A scheduled task could also audit the index to ensure that it's up to date. The index search looks like this: ```vb