Merge "Remove PooledConsumer"

This commit is contained in:
Riddle Hsu
2022-10-18 15:04:32 +00:00
committed by Android (Google) Code Review
6 changed files with 17 additions and 287 deletions

View File

@@ -51,13 +51,13 @@ import java.util.function.Function;
* @hide
*/
abstract class OmniFunction<A, B, C, D, E, F, G, H, I, J, K, R> implements
PooledFunction<A, R>, BiFunction<A, B, R>, TriFunction<A, B, C, R>,
BiFunction<A, B, R>, TriFunction<A, B, C, R>,
QuadFunction<A, B, C, D, R>, QuintFunction<A, B, C, D, E, R>,
HexFunction<A, B, C, D, E, F, R>, HeptFunction<A, B, C, D, E, F, G, R>,
OctFunction<A, B, C, D, E, F, G, H, R>, NonaFunction<A, B, C, D, E, F, G, H, I, R>,
DecFunction<A, B, C, D, E, F, G, H, I, J, R>,
UndecFunction<A, B, C, D, E, F, G, H, I, J, K, R>,
PooledConsumer<A>, BiConsumer<A, B>, TriConsumer<A, B, C>, QuadConsumer<A, B, C, D>,
BiConsumer<A, B>, TriConsumer<A, B, C>, QuadConsumer<A, B, C, D>,
QuintConsumer<A, B, C, D, E>, HexConsumer<A, B, C, D, E, F>,
HeptConsumer<A, B, C, D, E, F, G>, OctConsumer<A, B, C, D, E, F, G, H>,
NonaConsumer<A, B, C, D, E, F, G, H, I>, DecConsumer<A, B, C, D, E, F, G, H, I, J>,
@@ -73,11 +73,6 @@ abstract class OmniFunction<A, B, C, D, E, F, G, H, I, J, K, R> implements
return invoke(o, o2, null, null, null, null, null, null, null, null, null);
}
@Override
public R apply(A o) {
return invoke(o, null, null, null, null, null, null, null, null, null, null);
}
public abstract <V> OmniFunction<A, B, C, D, E, F, G, H, I, J, K, V> andThen(
Function<? super R, ? extends V> after);
public abstract OmniFunction<A, B, C, D, E, F, G, H, I, J, K, R> negate();
@@ -87,11 +82,6 @@ abstract class OmniFunction<A, B, C, D, E, F, G, H, I, J, K, R> implements
invoke(o, o2, null, null, null, null, null, null, null, null, null);
}
@Override
public void accept(A o) {
invoke(o, null, null, null, null, null, null, null, null, null, null);
}
@Override
public void run() {
invoke(null, null, null, null, null, null, null, null, null, null, null);
@@ -132,11 +122,6 @@ abstract class OmniFunction<A, B, C, D, E, F, G, H, I, J, K, R> implements
return this;
}
@Override
public PooledConsumer<A> asConsumer() {
return this;
}
@Override
public R apply(A a, B b, C c) {
return invoke(a, b, c, null, null, null, null, null, null, null, null);

View File

@@ -1,31 +0,0 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.internal.util.function.pooled;
import java.util.function.Consumer;
/**
* {@link Consumer} + {@link PooledLambda}
*
* @see PooledLambda
* @hide
*/
public interface PooledConsumer<T> extends PooledLambda, Consumer<T> {
/** @inheritDoc */
PooledConsumer<T> recycleOnUse();
}

View File

@@ -1,36 +0,0 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.internal.util.function.pooled;
import java.util.function.Function;
/**
* {@link Function} + {@link PooledLambda}
*
* @see PooledLambda
* @hide
*/
public interface PooledFunction<A, R> extends PooledLambda, Function<A, R> {
/**
* Ignores the result
*/
PooledConsumer<A> asConsumer();
/** @inheritDoc */
PooledFunction<A, R> recycleOnUse();
}

View File

@@ -232,24 +232,6 @@ public interface PooledLambda {
null, null, null, null);
}
/**
* {@link PooledConsumer} factory
*
* @param function non-capturing lambda(typically an unbounded method reference)
* to be invoked on call
* @param arg1 placeholder for a missing argument. Use {@link #__} to get one
* @param arg2 parameter supplied to {@code function} on call
* @return a {@link PooledConsumer}, equivalent to lambda:
* {@code (arg1) -> function(arg1, arg2) }
*/
static <A, B> PooledConsumer<A> obtainConsumer(
BiConsumer<? super A, ? super B> function,
ArgumentPlaceholder<A> arg1, B arg2) {
return acquire(PooledLambdaImpl.sPool,
function, 2, 1, ReturnType.VOID, arg1, arg2, null, null, null, null, null, null,
null, null, null, null);
}
/**
* {@link PooledPredicate} factory
*
@@ -328,24 +310,6 @@ public interface PooledLambda {
null, null, null, null);
}
/**
* {@link PooledConsumer} factory
*
* @param function non-capturing lambda(typically an unbounded method reference)
* to be invoked on call
* @param arg1 parameter supplied to {@code function} on call
* @param arg2 placeholder for a missing argument. Use {@link #__} to get one
* @return a {@link PooledConsumer}, equivalent to lambda:
* {@code (arg2) -> function(arg1, arg2) }
*/
static <A, B> PooledConsumer<B> obtainConsumer(
BiConsumer<? super A, ? super B> function,
A arg1, ArgumentPlaceholder<B> arg2) {
return acquire(PooledLambdaImpl.sPool,
function, 2, 1, ReturnType.VOID, arg1, arg2, null, null, null, null, null, null,
null, null, null, null);
}
/**
* {@link PooledPredicate} factory
*
@@ -417,63 +381,6 @@ public interface PooledLambda {
null, null, null, null);
}
/**
* {@link PooledConsumer} factory
*
* @param function non-capturing lambda(typically an unbounded method reference)
* to be invoked on call
* @param arg1 placeholder for a missing argument. Use {@link #__} to get one
* @param arg2 parameter supplied to {@code function} on call
* @param arg3 parameter supplied to {@code function} on call
* @return a {@link PooledConsumer}, equivalent to lambda:
* {@code (arg1) -> function(arg1, arg2, arg3) }
*/
static <A, B, C> PooledConsumer<A> obtainConsumer(
TriConsumer<? super A, ? super B, ? super C> function,
ArgumentPlaceholder<A> arg1, B arg2, C arg3) {
return acquire(PooledLambdaImpl.sPool,
function, 3, 1, ReturnType.VOID, arg1, arg2, arg3, null, null, null, null, null,
null, null, null, null);
}
/**
* {@link PooledConsumer} factory
*
* @param function non-capturing lambda(typically an unbounded method reference)
* to be invoked on call
* @param arg1 parameter supplied to {@code function} on call
* @param arg2 placeholder for a missing argument. Use {@link #__} to get one
* @param arg3 parameter supplied to {@code function} on call
* @return a {@link PooledConsumer}, equivalent to lambda:
* {@code (arg2) -> function(arg1, arg2, arg3) }
*/
static <A, B, C> PooledConsumer<B> obtainConsumer(
TriConsumer<? super A, ? super B, ? super C> function,
A arg1, ArgumentPlaceholder<B> arg2, C arg3) {
return acquire(PooledLambdaImpl.sPool,
function, 3, 1, ReturnType.VOID, arg1, arg2, arg3, null, null, null, null, null,
null, null, null, null);
}
/**
* {@link PooledConsumer} factory
*
* @param function non-capturing lambda(typically an unbounded method reference)
* to be invoked on call
* @param arg1 parameter supplied to {@code function} on call
* @param arg2 parameter supplied to {@code function} on call
* @param arg3 placeholder for a missing argument. Use {@link #__} to get one
* @return a {@link PooledConsumer}, equivalent to lambda:
* {@code (arg3) -> function(arg1, arg2, arg3) }
*/
static <A, B, C> PooledConsumer<C> obtainConsumer(
TriConsumer<? super A, ? super B, ? super C> function,
A arg1, B arg2, ArgumentPlaceholder<C> arg3) {
return acquire(PooledLambdaImpl.sPool,
function, 3, 1, ReturnType.VOID, arg1, arg2, arg3, null, null, null, null, null,
null, null, null, null);
}
/**
* Factory of {@link Message}s that contain an
* ({@link PooledLambda#recycleOnUse auto-recycling}) {@link PooledRunnable} as its
@@ -529,86 +436,6 @@ public interface PooledLambda {
null, null, null, null);
}
/**
* {@link PooledConsumer} factory
*
* @param function non-capturing lambda(typically an unbounded method reference)
* to be invoked on call
* @param arg1 placeholder for a missing argument. Use {@link #__} to get one
* @param arg2 parameter supplied to {@code function} on call
* @param arg3 parameter supplied to {@code function} on call
* @param arg4 parameter supplied to {@code function} on call
* @return a {@link PooledConsumer}, equivalent to lambda:
* {@code (arg1) -> function(arg1, arg2, arg3, arg4) }
*/
static <A, B, C, D> PooledConsumer<A> obtainConsumer(
QuadConsumer<? super A, ? super B, ? super C, ? super D> function,
ArgumentPlaceholder<A> arg1, B arg2, C arg3, D arg4) {
return acquire(PooledLambdaImpl.sPool,
function, 4, 1, ReturnType.VOID, arg1, arg2, arg3, arg4, null, null, null, null,
null, null, null, null);
}
/**
* {@link PooledConsumer} factory
*
* @param function non-capturing lambda(typically an unbounded method reference)
* to be invoked on call
* @param arg1 parameter supplied to {@code function} on call
* @param arg2 placeholder for a missing argument. Use {@link #__} to get one
* @param arg3 parameter supplied to {@code function} on call
* @param arg4 parameter supplied to {@code function} on call
* @return a {@link PooledConsumer}, equivalent to lambda:
* {@code (arg2) -> function(arg1, arg2, arg3, arg4) }
*/
static <A, B, C, D> PooledConsumer<B> obtainConsumer(
QuadConsumer<? super A, ? super B, ? super C, ? super D> function,
A arg1, ArgumentPlaceholder<B> arg2, C arg3, D arg4) {
return acquire(PooledLambdaImpl.sPool,
function, 4, 1, ReturnType.VOID, arg1, arg2, arg3, arg4, null, null, null, null,
null, null, null, null);
}
/**
* {@link PooledConsumer} factory
*
* @param function non-capturing lambda(typically an unbounded method reference)
* to be invoked on call
* @param arg1 parameter supplied to {@code function} on call
* @param arg2 parameter supplied to {@code function} on call
* @param arg3 placeholder for a missing argument. Use {@link #__} to get one
* @param arg4 parameter supplied to {@code function} on call
* @return a {@link PooledConsumer}, equivalent to lambda:
* {@code (arg3) -> function(arg1, arg2, arg3, arg4) }
*/
static <A, B, C, D> PooledConsumer<C> obtainConsumer(
QuadConsumer<? super A, ? super B, ? super C, ? super D> function,
A arg1, B arg2, ArgumentPlaceholder<C> arg3, D arg4) {
return acquire(PooledLambdaImpl.sPool,
function, 4, 1, ReturnType.VOID, arg1, arg2, arg3, arg4, null, null, null, null,
null, null, null, null);
}
/**
* {@link PooledConsumer} factory
*
* @param function non-capturing lambda(typically an unbounded method reference)
* to be invoked on call
* @param arg1 parameter supplied to {@code function} on call
* @param arg2 parameter supplied to {@code function} on call
* @param arg3 parameter supplied to {@code function} on call
* @param arg4 placeholder for a missing argument. Use {@link #__} to get one
* @return a {@link PooledConsumer}, equivalent to lambda:
* {@code (arg4) -> function(arg1, arg2, arg3, arg4) }
*/
static <A, B, C, D> PooledConsumer<D> obtainConsumer(
QuadConsumer<? super A, ? super B, ? super C, ? super D> function,
A arg1, B arg2, C arg3, ArgumentPlaceholder<D> arg4) {
return acquire(PooledLambdaImpl.sPool,
function, 4, 1, ReturnType.VOID, arg1, arg2, arg3, arg4, null, null, null, null,
null, null, null, null);
}
/**
* Factory of {@link Message}s that contain an
* ({@link PooledLambda#recycleOnUse auto-recycling}) {@link PooledRunnable} as its

View File

@@ -26,11 +26,6 @@ import java.util.function.Predicate;
*/
public interface PooledPredicate<T> extends PooledLambda, Predicate<T> {
/**
* Ignores the result
*/
PooledConsumer<T> asConsumer();
/** @inheritDoc */
PooledPredicate<T> recycleOnUse();
}

View File

@@ -29,7 +29,6 @@ import android.util.Log;
import androidx.test.filters.LargeTest;
import com.android.internal.util.function.pooled.PooledConsumer;
import com.android.internal.util.function.pooled.PooledLambda;
import com.android.internal.util.function.pooled.PooledPredicate;
@@ -46,7 +45,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -94,27 +92,27 @@ public class LambdaPerfTest {
};
@Test
public void test1ParamConsumer() {
evaluate(LAMBDA_FORM_REGULAR, () -> forAllTask(t -> t.doSomething(mTask)));
public void test1ParamPredicate() {
evaluate(LAMBDA_FORM_REGULAR, () -> handleTask(t -> t.doSomething(mTaskId, mTime)));
evaluate(LAMBDA_FORM_POOLED, () -> {
final PooledConsumer c = PooledLambda.obtainConsumer(Task::doSomething,
PooledLambda.__(Task.class), mTask);
forAllTask(c);
final PooledPredicate c = PooledLambda.obtainPredicate(Task::doSomething,
PooledLambda.__(Task.class), mTaskId, mTime);
handleTask(c);
c.recycle();
});
}
@Test
public void test2PrimitiveParamsConsumer() {
public void test2PrimitiveParamsPredicate() {
// Not in Integer#IntegerCache (-128~127) for autoboxing, that may create new object.
mTaskId = 12345;
mTime = 54321;
evaluate(LAMBDA_FORM_REGULAR, () -> forAllTask(t -> t.doSomething(mTaskId, mTime)));
evaluate(LAMBDA_FORM_REGULAR, () -> handleTask(t -> t.doSomething(mTaskId, mTime)));
evaluate(LAMBDA_FORM_POOLED, () -> {
final PooledConsumer c = PooledLambda.obtainConsumer(Task::doSomething,
final PooledPredicate c = PooledLambda.obtainPredicate(Task::doSomething,
PooledLambda.__(Task.class), mTaskId, mTime);
forAllTask(c);
handleTask(c);
c.recycle();
});
}
@@ -164,15 +162,15 @@ public class LambdaPerfTest {
public void testMultiThread() {
final int numThread = 3;
final Runnable regularAction = () -> forAllTask(t -> t.doSomething(mTask));
final Runnable regularAction = () -> handleTask(t -> t.doSomething(mTaskId, mTime));
final Runnable[] regularActions = new Runnable[numThread];
Arrays.fill(regularActions, regularAction);
evaluateMultiThread(LAMBDA_FORM_REGULAR, regularActions);
final Runnable pooledAction = () -> {
final PooledConsumer c = PooledLambda.obtainConsumer(Task::doSomething,
PooledLambda.__(Task.class), mTask);
forAllTask(c);
final PooledPredicate c = PooledLambda.obtainPredicate(Task::doSomething,
PooledLambda.__(Task.class), mTaskId, mTime);
handleTask(c);
c.recycle();
};
final Runnable[] pooledActions = new Runnable[numThread];
@@ -180,12 +178,6 @@ public class LambdaPerfTest {
evaluateMultiThread(LAMBDA_FORM_POOLED, pooledActions);
}
private void forAllTask(Consumer<Task> callback) {
for (int i = mTasks.size() - 1; i >= 0; i--) {
callback.accept(mTasks.get(i));
}
}
private void handleTask(Predicate<Task> callback) {
for (int i = mTasks.size() - 1; i >= 0; i--) {
final Task task = mTasks.get(i);
@@ -318,10 +310,8 @@ public class LambdaPerfTest {
void doSomething() {
}
void doSomething(Task t) {
}
void doSomething(int taskId, long time) {
boolean doSomething(int taskId, long time) {
return false;
}
boolean doSomething(Rect bounds, boolean top, int taskId) {