mirror of
https://github.com/elastic/elasticsearch.git
synced 2025-06-28 17:34:17 -04:00
Support negative array ofsets in painless
Adds support for indexing into lists and arrays with negative indexes meaning "counting from the back". So for if `x = ["cat", "dog", "chicken"]` then `x[-1] == "chicken"`. This adds an extra branch to every array and list access but some performance testing makes it look like the branch predictor successfully predicts the branch every time so there isn't a in execution time for this feature when the index is positive. When the index is negative performance testing showed the runtime is the same as writing `x[x.length - 1]`, again, presumably thanks to the branch predictor. Those performance metrics were calculated for lists and arrays but `def`s get roughly the same treatment though instead of inlining the test they need to make a invoke dynamic so we don't screw up maps. Closes #20870
This commit is contained in:
parent
d731a330aa
commit
3a7a218e8f
13 changed files with 399 additions and 51 deletions
|
@ -28,6 +28,23 @@ String constants can be declared with single quotes, to avoid escaping horrors w
|
|||
def mystring = 'foo';
|
||||
---------------------------------------------------------
|
||||
|
||||
[float]
|
||||
[[painless-arrays]]
|
||||
==== Arrays
|
||||
|
||||
Arrays can be subscripted starting from `0` for traditional array access or with
|
||||
negative numbers to starting from the back of the array. So the following
|
||||
returns `2`.
|
||||
|
||||
[source,painless]
|
||||
---------------------------------------------------------
|
||||
int[] x = new int[5];
|
||||
x[0]++;
|
||||
x[-5]++;
|
||||
return x[0];
|
||||
---------------------------------------------------------
|
||||
|
||||
|
||||
[float]
|
||||
[[painless-lists]]
|
||||
==== List
|
||||
|
@ -39,11 +56,13 @@ Lists can be created explicitly (e.g. `new ArrayList()`) or initialized similar
|
|||
def list = [1,2,3];
|
||||
---------------------------------------------------------
|
||||
|
||||
Lists can also be accessed similar to arrays: they support subscript and `.length`:
|
||||
Lists can also be accessed similar to arrays. They support `.length` and
|
||||
subscripts, including negative subscripts to read from the back of the list:
|
||||
|
||||
[source,painless]
|
||||
---------------------------------------------------------
|
||||
def list = [1,2,3];
|
||||
list[-1] = 5
|
||||
return list[0]
|
||||
---------------------------------------------------------
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue