Skip to content
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

Avoid list copy on reverse iteration #12297

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -653,11 +653,11 @@ protected void exitScope(Request request, Context lastContext, ClassLoader lastL
*/
protected void notifyExitScope(Request request)
{
for (ContextScopeListener listener : TypeUtil.reverse(_contextListeners))
for (ListIterator<ContextScopeListener> i = TypeUtil.listIteratorAtEnd(_contextListeners); i.hasPrevious();)
{
try
{
listener.exitScope(_context, request);
i.previous().exitScope(_context, request);
}
catch (Throwable e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;
import java.util.Optional;
import java.util.ServiceConfigurationError;
Expand Down Expand Up @@ -174,18 +175,31 @@ public static <T> List<T> asList(T[] a)
}

/**
* <p>Returns a new list with the elements of the specified list in reverse order.</p>
* <p>The specified list is not modified, differently from {@link Collections#reverse(List)}.</p>
*
* @param list the list whose elements are to be reversed
* @return a new list with the elements in reverse order
* <p>Returns a {@link ListIterator} positioned at the last item in a list</p>
gregw marked this conversation as resolved.
Show resolved Hide resolved
* <p>The method is safe for concurrent modifications iff the list iterator itself is safe.</p>
gregw marked this conversation as resolved.
Show resolved Hide resolved
* @param list the list
* @param <T> the element type
* @return A {@link ListIterator} with {@link ListIterator#hasNext()} that would have return {@code false}
* at the snapshot of the list represented by this call.
gregw marked this conversation as resolved.
Show resolved Hide resolved
*/
public static <T> List<T> reverse(List<T> list)
public static <T> ListIterator<T> listIteratorAtEnd(List<T> list)
{
List<T> result = new ArrayList<>(list);
Collections.reverse(result);
return result;
try
{
int size = list.size();
if (size == 0)
return Collections.emptyListIterator();
return list.listIterator(size);
}
catch (IndexOutOfBoundsException e)
{
// list was concurrently modified, so do this the hard way
ListIterator<T> i = list.listIterator();
while (i.hasNext())
i.next();

return i;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -519,11 +520,11 @@ public void contextDestroyed() throws Exception
//Call context listeners
Throwable multiException = null;
ServletContextEvent event = new ServletContextEvent(getServletContext());
for (ServletContextListener listener : TypeUtil.reverse(_destroyServletContextListeners))
for (ListIterator<ServletContextListener> i = TypeUtil.listIteratorAtEnd(_destroyServletContextListeners); i.hasPrevious();)
{
try
{
callContextDestroyed(listener, event);
callContextDestroyed(i.previous(), event);
}
catch (Exception x)
{
Expand Down Expand Up @@ -574,17 +575,17 @@ protected void requestDestroyed(Request baseRequest, HttpServletRequest request)
if (!_servletRequestListeners.isEmpty())
{
final ServletRequestEvent sre = new ServletRequestEvent(getServletContext(), request);
for (ServletRequestListener listener : TypeUtil.reverse(_servletRequestListeners))
for (ListIterator<ServletRequestListener> i = TypeUtil.listIteratorAtEnd(_servletRequestListeners); i.hasPrevious();)
{
listener.requestDestroyed(sre);
i.previous().requestDestroyed(sre);
}
}

if (!_servletRequestAttributeListeners.isEmpty())
{
for (ServletRequestAttributeListener listener : TypeUtil.reverse(_servletRequestAttributeListeners))
for (ListIterator<ServletRequestAttributeListener> i = TypeUtil.listIteratorAtEnd(_servletRequestAttributeListeners); i.hasPrevious();)
{
scopedRequest.removeEventListener(listener);
scopedRequest.removeEventListener(i.previous());
}
}
}
Expand Down Expand Up @@ -1223,11 +1224,11 @@ protected void notifyExitScope(Request request)
ServletContextRequest scopedRequest = Request.as(request, ServletContextRequest.class);
if (!_contextListeners.isEmpty())
{
for (ServletContextScopeListener listener : TypeUtil.reverse(_contextListeners))
for (ListIterator<ServletContextScopeListener> i = TypeUtil.listIteratorAtEnd(_contextListeners); i.hasPrevious(); )
{
try
{
listener.exitScope(getContext(), scopedRequest);
i.previous().exitScope(getContext(), scopedRequest);
}
catch (Throwable e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.EventListener;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -570,9 +571,9 @@ public void onSessionDestroyed(Session session)
getSessionContext().run(() ->
{
HttpSessionEvent event = new HttpSessionEvent(session.getApi());
for (HttpSessionListener listener : TypeUtil.reverse(_sessionListeners))
for (ListIterator<HttpSessionListener> i = TypeUtil.listIteratorAtEnd(_sessionListeners); i.hasPrevious();)
{
listener.sessionDestroyed(event);
i.previous().sessionDestroyed(event);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -1000,17 +1001,17 @@ protected void requestDestroyed(Request baseRequest, HttpServletRequest request)
if (!_servletRequestListeners.isEmpty())
{
final ServletRequestEvent sre = new ServletRequestEvent(_apiContext, request);
for (ServletRequestListener listener : TypeUtil.reverse(_servletRequestListeners))
for (ListIterator<ServletRequestListener> i = TypeUtil.listIteratorAtEnd(_servletRequestListeners); i.hasPrevious();)
{
listener.requestDestroyed(sre);
i.previous().requestDestroyed(sre);
}
}

if (!_servletRequestAttributeListeners.isEmpty())
{
for (ServletRequestAttributeListener listener : TypeUtil.reverse(_servletRequestAttributeListeners))
for (ListIterator<ServletRequestAttributeListener> i = TypeUtil.listIteratorAtEnd(_servletRequestAttributeListeners); i.hasPrevious();)
{
baseRequest.removeEventListener(listener);
baseRequest.removeEventListener(i.previous());
}
}
}
Expand Down Expand Up @@ -1070,11 +1071,11 @@ protected void exitScope(Request request)
{
if (!_contextListeners.isEmpty())
{
for (ContextScopeListener listener : TypeUtil.reverse(_contextListeners))
for (ListIterator<ContextScopeListener> i = TypeUtil.listIteratorAtEnd(_contextListeners); i.hasPrevious();)
{
try
{
listener.exitScope(_apiContext, request);
i.previous().exitScope(_apiContext, request);
}
catch (Throwable e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Enumeration;
import java.util.EventListener;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
Expand Down Expand Up @@ -835,9 +836,9 @@ public void onSessionDestroyed(Session session)
Runnable r = () ->
{
HttpSessionEvent event = new HttpSessionEvent(session.getApi());
for (HttpSessionListener listener : TypeUtil.reverse(_sessionListeners))
for (ListIterator<HttpSessionListener> i = TypeUtil.listIteratorAtEnd(_sessionListeners); i.hasPrevious();)
{
listener.sessionDestroyed(event);
i.previous().sessionDestroyed(event);
}
};
_contextHandler.getCoreContextHandler().getContext().run(r);
Expand Down
Loading