vault backup: 2025-03-25 11:48:39
All checks were successful
Update pages on webserver / Update (push) Successful in 5s

This commit is contained in:
themodernhakr 2025-03-25 11:48:39 -05:00
parent bcc8891fb2
commit d0a61d5808

View File

@ -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