Skip to content

Commit

Permalink
Starlark: annotate builtin functions with trustReturnsValid
Browse files Browse the repository at this point in the history
Speed up is 4% for this test:

```
def test():
  for i in range(10):
    print(i)
    d = {"a": range(10), "b": {}, "c": 4, "d": 5, "e": (), "f": True, "g": []}
    for _ in range(1000000):
      d.get("a")
      d.get("b")
      d.get("c")
      d.get("d")
      d.get("e")
      d.get("f")
      d.get("g")

test()
```
  • Loading branch information
stepancheg committed May 2, 2022
1 parent 898fbde commit fcdd290
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/main/java/net/starlark/java/eval/Dict.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public Iterator<K> iterator() {
named = true,
doc = "The default value to use (instead of None) if the key is not found.")
},
trustReturnsValid = true,
useStarlarkThread = true)
// TODO(adonovan): This method is named get2 as a temporary workaround for a bug in
// StarlarkAnnotations.getStarlarkMethod. The two 'get' methods cause it to get
Expand Down Expand Up @@ -247,6 +248,7 @@ public Object get2(Object key, Object defaultValue, StarlarkThread thread) throw
named = true,
doc = "a default value if the key is absent."),
},
trustReturnsValid = true,
useStarlarkThread = true)
public Object pop(Object key, Object defaultValue, StarlarkThread thread) throws EvalException {
Starlark.checkMutable(this);
Expand Down Expand Up @@ -298,7 +300,8 @@ public Tuple popitem() throws EvalException {
defaultValue = "None",
named = true,
doc = "a default value if the key is absent."),
})
},
trustReturnsValid = true)
public V setdefault(K key, V defaultValue) throws EvalException {
Starlark.checkMutable(this);
Starlark.checkHashable(key);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/net/starlark/java/eval/MethodLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class MethodLibrary {
+ "or if no arguments are given. "
+ "<pre class=\"language-python\">min(2, 5, 4) == 2\n"
+ "min([5, 6, 3]) == 3</pre>",
extraPositionals = @Param(name = "args", doc = "The elements to be checked."))
extraPositionals = @Param(name = "args", doc = "The elements to be checked."),
trustReturnsValid = true)
public Object min(Sequence<?> args) throws EvalException {
return findExtreme(args, Starlark.ORDERING.reverse());
}
Expand All @@ -54,7 +55,8 @@ public Object min(Sequence<?> args) throws EvalException {
+ "or if no arguments are given. "
+ "<pre class=\"language-python\">max(2, 5, 4) == 5\n"
+ "max([5, 6, 3]) == 6</pre>",
extraPositionals = @Param(name = "args", doc = "The elements to be checked."))
extraPositionals = @Param(name = "args", doc = "The elements to be checked."),
trustReturnsValid = true)
public Object max(Sequence<?> args) throws EvalException {
return findExtreme(args, Starlark.ORDERING);
}
Expand Down Expand Up @@ -625,6 +627,7 @@ public boolean hasattr(Object obj, String name, StarlarkThread thread) throws Ev
"The default value to return in case the struct "
+ "doesn't have an attribute of the given name.")
},
trustReturnsValid = true,
useStarlarkThread = true)
public Object getattr(Object obj, String name, Object defaultValue, StarlarkThread thread)
throws EvalException, InterruptedException {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/starlark/java/eval/StarlarkList.java
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,8 @@ public int index(Object x, Object start, Object end) throws EvalException {
},
defaultValue = "-1",
doc = "The index of the item.")
})
},
trustReturnsValid = true)
public Object pop(Object i) throws EvalException {
int arg = i == Starlark.NONE ? -1 : Starlark.toInt(i, "i");
int index = EvalUtils.getSequenceIndex(arg, size);
Expand Down

0 comments on commit fcdd290

Please sign in to comment.