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

Performance optimization for primitive Dynamic***Property classes #369

Merged
merged 12 commits into from
Aug 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2014 Netflix, Inc.
*
* 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.netflix.config;

/**
* An implementation of {@link DynamicBooleanProperty} that caches the primitive value whenever it is changed.
*
* This can give improved performance due to avoiding the unboxing, at the expense of additional
* memory usage.
*
* @author Mike Smith
*
*/
public class CachedDynamicBooleanProperty extends DynamicBooleanProperty {

protected volatile boolean primitiveValue;

public CachedDynamicBooleanProperty(String propName, boolean defaultValue) {
super(propName, defaultValue);

// Set the initial value of the cached primitive value.
this.primitiveValue = chooseValue();
}

@Override
protected void propertyChanged() {
// Update the cached primitive value when the property is changed.
this.primitiveValue = chooseValue();
}

/**
* Get the current value from the underlying DynamicProperty
*
* @return
*/
protected boolean chooseValue() {
return prop.getBoolean(defaultValue).booleanValue();
}

/**
* Get the current cached value.
*
* @return
*/
@Override
public boolean get() {
return primitiveValue;
}

@Override
public Boolean getValue() {
return get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2014 Netflix, Inc.
*
* 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.netflix.config;

/**
* An implementation of {@link DynamicDoubleProperty} that caches the primitive value whenever it is changed.
*
* This can give improved performance due to avoiding the unboxing, at the expense of additional
* memory usage.
*
* @author Mike Smith
*
*/
public class CachedDynamicDoubleProperty extends DynamicDoubleProperty {

protected volatile double primitiveValue;

public CachedDynamicDoubleProperty(String propName, double defaultValue) {
super(propName, defaultValue);

// Set the initial value of the cached primitive value.
this.primitiveValue = chooseValue();
}

@Override
protected void propertyChanged() {
// Update the cached primitive value when the property is changed.
this.primitiveValue = chooseValue();
}

/**
* Get the current value from the underlying DynamicProperty
*
* @return
*/
protected double chooseValue() {
return prop.getDouble(defaultValue).doubleValue();
}

/**
* Get the current cached value.
*
* @return
*/
@Override
public double get() {
return primitiveValue;
}

@Override
public Double getValue() {
return get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2014 Netflix, Inc.
*
* 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.netflix.config;

/**
* An implementation of {@link DynamicFloatProperty} that caches the primitive value whenever it is changed.
*
* This can give improved performance due to avoiding the unboxing, at the expense of additional
* memory usage.
*
* @author Mike Smith
*
*/
public class CachedDynamicFloatProperty extends DynamicFloatProperty {

protected volatile float primitiveValue;

public CachedDynamicFloatProperty(String propName, float defaultValue) {
super(propName, defaultValue);

// Set the initial value of the cached primitive value.
this.primitiveValue = chooseValue();
}

@Override
protected void propertyChanged() {
// Update the cached primitive value when the property is changed.
this.primitiveValue = chooseValue();
}

/**
* Get the current value from the underlying DynamicProperty
*
* @return
*/
protected float chooseValue() {
return prop.getFloat(defaultValue).floatValue();
}

/**
* Get the current cached value.
*
* @return
*/
@Override
public float get() {
return primitiveValue;
}

@Override
public Float getValue() {
return get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2014 Netflix, Inc.
*
* 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.netflix.config;

/**
* An implementation of {@link DynamicIntProperty} that caches the primitive value whenever it is changed.
*
* This can give improved performance due to avoiding the unboxing, at the expense of additional
* memory usage.
*
* @author Mike Smith
*
*/
public class CachedDynamicIntProperty extends DynamicIntProperty {

protected volatile int primitiveValue;

public CachedDynamicIntProperty(String propName, int defaultValue) {
super(propName, defaultValue);

// Set the initial value of the cached primitive value.
this.primitiveValue = chooseValue();
}

@Override
protected void propertyChanged() {
// Update the cached primitive value when the property is changed.
this.primitiveValue = chooseValue();
}

/**
* Get the current value from the underlying DynamicProperty
*
* @return
*/
protected int chooseValue() {
return prop.getInteger(defaultValue).intValue();
}

/**
* Get the current cached value.
*
* @return
*/
@Override
public int get() {
return primitiveValue;
}

@Override
public Integer getValue() {
return get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2014 Netflix, Inc.
*
* 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.netflix.config;

/**
* An implementation of {@link DynamicLongProperty} that caches the primitive value whenever it is changed.
*
* This can give improved performance due to avoiding the unboxing, at the expense of additional
* memory usage.
*
* @author Mike Smith
*
*/
public class CachedDynamicLongProperty extends DynamicLongProperty {

protected volatile long primitiveValue;

public CachedDynamicLongProperty(String propName, long defaultValue) {
super(propName, Long.valueOf(defaultValue));

// Set the initial value of the cached primitive value.
this.primitiveValue = chooseValue();
}

@Override
protected void propertyChanged() {
// Update the cached primitive value when the property is changed.
this.primitiveValue = chooseValue();
}

/**
* Get the current value from the underlying DynamicProperty
*
* @return
*/
protected long chooseValue() {
return prop.getLong(defaultValue).longValue();
}

/**
* Get the current cached value.
*
* @return
*/
@Override
public long get() {
return primitiveValue;
}

@Override
public Long getValue() {
return get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.netflix.config;

/**
* Simple benchmark impl to compare the caching of primitive boolean value in CachedDynamicBooleanProperty with
* the original implementation.
*
* @author Mike Smith
* Date: 11/25/15
*/
public class CachedPropertiesPerfTest
{
public static void main(String[] args)
{
try {
long loopCount = 4000000000l;

// Run twice.
runTest(loopCount);
runTest(loopCount);
}
catch(Exception e) {
e.printStackTrace();
}
}

private static void runTest(long loopCount)
{
// Warmup and then run benchmark.
DynamicBooleanProperty newProp = new CachedDynamicBooleanProperty("zuul.test.cachedprops.new", true);
runDynamicBooleanPropertyTest(newProp, 10000);
long durationPrimitive = runDynamicBooleanPropertyTest(newProp, loopCount);
double avgPrimitiveNs = (durationPrimitive * 1000d * 1000d) / loopCount;

// Warmup and then run benchmark.
DynamicBooleanProperty oldProp = new DynamicBooleanProperty("zuul.test.cachedprops.original", true);
runDynamicBooleanPropertyTest(oldProp, 10000);
long durationOriginal = runDynamicBooleanPropertyTest(oldProp, loopCount);
double avgOriginalNs = (durationOriginal * 1000d * 1000d) / loopCount;

System.out.println("#####################");
System.out.println("DynamicBooleanProperty: " + loopCount + " calls. Total = " + durationOriginal + " ms. Avg = " + avgOriginalNs + " nanos.");
System.out.println("CachedDynamicBooleanProperty: " + loopCount + " calls. Total = " + durationPrimitive + " ms. Avg = " + avgPrimitiveNs + " nanos.");
}

private static long runDynamicBooleanPropertyTest(DynamicBooleanProperty prop, long loopCount)
{
long startTime = System.currentTimeMillis();

boolean value;
for (long i=0; i<loopCount; i++) {
value = prop.get();
}

return System.currentTimeMillis() - startTime;
}
}