Skip to content

Commit

Permalink
Lazily compile accumulators
Browse files Browse the repository at this point in the history
Makes the tests about 5% faster on my laptop
  • Loading branch information
cberner committed Apr 6, 2015
1 parent 344a957 commit 95b354e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
@@ -0,0 +1,22 @@
/*
* 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.facebook.presto.operator.aggregation;

import java.util.List;
import java.util.Optional;

public interface AccumulatorFactoryBinder
{
AccumulatorFactory bind(List<Integer> argumentChannels, Optional<Integer> maskChannel, Optional<Integer> sampleWeightChannel, double confidence);
}
Expand Up @@ -139,7 +139,7 @@ public List<InternalAggregationFunction> generateAggregationFunctions(Class<?> c
throw Throwables.propagate(e); throw Throwables.propagate(e);
} }


GenericAccumulatorFactoryBinder factory = new AccumulatorCompiler().generateAccumulatorFactoryBinder(metadata, classLoader); AccumulatorFactoryBinder factory = new LazyAccumulatorFactoryBinder(metadata, classLoader);
builder.add(new InternalAggregationFunction(name, inputTypes, intermediateType, outputType, aggregationAnnotation.decomposable(), aggregationAnnotation.approximate(), factory)); builder.add(new InternalAggregationFunction(name, inputTypes, intermediateType, outputType, aggregationAnnotation.decomposable(), aggregationAnnotation.approximate(), factory));
} }
} }
Expand Down
Expand Up @@ -25,6 +25,7 @@
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;


public class GenericAccumulatorFactoryBinder public class GenericAccumulatorFactoryBinder
implements AccumulatorFactoryBinder
{ {
private final boolean approximationSupported; private final boolean approximationSupported;
private final AccumulatorStateSerializer<?> stateSerializer; private final AccumulatorStateSerializer<?> stateSerializer;
Expand Down Expand Up @@ -65,6 +66,7 @@ public GenericAccumulatorFactoryBinder(
} }
} }


@Override
public AccumulatorFactory bind(List<Integer> argumentChannels, Optional<Integer> maskChannel, Optional<Integer> sampleWeightChannel, double confidence) public AccumulatorFactory bind(List<Integer> argumentChannels, Optional<Integer> maskChannel, Optional<Integer> sampleWeightChannel, double confidence)
{ {
if (!approximationSupported) { if (!approximationSupported) {
Expand Down
Expand Up @@ -30,9 +30,9 @@ public final class InternalAggregationFunction
private final Type finalType; private final Type finalType;
private final boolean decomposable; private final boolean decomposable;
private final boolean approximate; private final boolean approximate;
private final GenericAccumulatorFactoryBinder factory; private final AccumulatorFactoryBinder factory;


public InternalAggregationFunction(String name, List<Type> parameterTypes, Type intermediateType, Type finalType, boolean decomposable, boolean approximate, GenericAccumulatorFactoryBinder factory) public InternalAggregationFunction(String name, List<Type> parameterTypes, Type intermediateType, Type finalType, boolean decomposable, boolean approximate, AccumulatorFactoryBinder factory)
{ {
this.name = checkNotNull(name, "name is null"); this.name = checkNotNull(name, "name is null");
checkArgument(!name.isEmpty(), "name is empty"); checkArgument(!name.isEmpty(), "name is empty");
Expand Down
@@ -0,0 +1,38 @@
/*
* 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.facebook.presto.operator.aggregation;

import com.facebook.presto.byteCode.DynamicClassLoader;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;

import java.util.List;
import java.util.Optional;

public class LazyAccumulatorFactoryBinder
implements AccumulatorFactoryBinder
{
private final Supplier<AccumulatorFactoryBinder> binder;

public LazyAccumulatorFactoryBinder(AggregationMetadata metadata, DynamicClassLoader classLoader)
{
binder = Suppliers.memoize(() -> new AccumulatorCompiler().generateAccumulatorFactoryBinder(metadata, classLoader));
}

@Override
public AccumulatorFactory bind(List<Integer> argumentChannels, Optional<Integer> maskChannel, Optional<Integer> sampleWeightChannel, double confidence)
{
return binder.get().bind(argumentChannels, maskChannel, sampleWeightChannel, confidence);
}
}

0 comments on commit 95b354e

Please sign in to comment.