-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Attempt to make some operations cheaper #125228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
16a15af
6250cde
330ba8f
634bc38
c13268c
94b1f41
86b0f8b
4eb0220
3e11d25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,7 +67,11 @@ public List<T> children() { | |
| @SuppressWarnings("unchecked") | ||
| public void forEachDown(Consumer<? super T> action) { | ||
| action.accept((T) this); | ||
| children().forEach(c -> c.forEachDown(action)); | ||
| // please do not refactor it to a for-each loop to avoid | ||
| // allocating iterator that performs concurrent modification checks and extra stack traces | ||
| for (int c = 0, size = children.size(); c < size; c++) { | ||
| children.get(c).forEachDown(action); | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
|
|
@@ -81,7 +85,11 @@ public <E extends T> void forEachDown(Class<E> typeToken, Consumer<? super E> ac | |
|
|
||
| @SuppressWarnings("unchecked") | ||
| public void forEachUp(Consumer<? super T> action) { | ||
| children().forEach(c -> c.forEachUp(action)); | ||
| // please do not refactor it to a for-each loop to avoid | ||
| // allocating iterator that performs concurrent modification checks and extra stack traces | ||
| for (int c = 0, size = children.size(); c < size; c++) { | ||
| children.get(c).forEachUp(action); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add this as a comment, please? The explanation in the comment helps, but I think the important point is that this reduces stack size rather than increasing performance, which the current comment kinda implies by mentioning the concurrent modification checks - which I understand didn't seem to be a problem? |
||
| action.accept((T) this); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -140,7 +140,24 @@ private static Object doTransformExpression(Object arg, Function<Expression, ? e | |
| // preserving the type information is hacky and weird (a lot of context needs to be passed around and the lambda itself | ||
| // has no type info so it's difficult to have automatic checking without having base classes). | ||
|
|
||
| if (arg instanceof Collection<?> c) { | ||
| if (arg instanceof List<?> c) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please confirm we do not expect sets in here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't however it's not enforced - add an else throwing an error for safety. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ++ this could easily become a bug in the future, as it makes total sense for a node property to be e.g. a set of expressions. We might also just not have any test that exercises this code path with a non-list collection. To really know, we'd have to go and check all the My suggestion: let's add a code path for |
||
| List<Object> transformed = null; | ||
| boolean hasChanged = false; | ||
| // please do not refactor it to a for-each loop to avoid | ||
| // allocating iterator that performs concurrent modification checks and extra stack traces | ||
| for (int i = 0, size = c.size(); i < size; i++) { | ||
| var e = c.get(i); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're making more and more improvements to how we perform query plan transformations/traversals. Could we please add at least a micro benchmark as one of the next steps? Without one, it's both hard to understand the impact of optimizations, and it's also unclear if we maybe introduce accidental regressions (in whichever PRs). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. I will try to add one in a separate change. |
||
| Object next = doTransformExpression(e, traversal); | ||
| if (e.equals(next) == false) { | ||
| if (hasChanged == false) { | ||
| hasChanged = true; | ||
| transformed = new ArrayList<>(c); | ||
| } | ||
| transformed.set(i, next); | ||
| } | ||
| } | ||
| return hasChanged ? transformed : arg; | ||
| } else if (arg instanceof Collection<?> c) { | ||
| List<Object> transformed = null; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's fine to mark this branch as deprecated/likely dead + we could still add an |
||
| boolean hasChanged = false; | ||
| int i = 0; | ||
|
|
@@ -149,13 +166,14 @@ private static Object doTransformExpression(Object arg, Function<Expression, ? e | |
| if (e.equals(next) == false) { | ||
| if (hasChanged == false) { | ||
| hasChanged = true; | ||
| // TODO if collection is a set then this silently changes its semantics by allowing duplicates | ||
| // We should fix it or confirm this branch is never needed | ||
| transformed = new ArrayList<>(c); | ||
| } | ||
| transformed.set(i, next); | ||
| } | ||
| i++; | ||
| } | ||
|
|
||
| return hasChanged ? transformed : arg; | ||
| } | ||
|
|
||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: