[jira] [Updated] (BEANUTILS-539) use ConcurrentHashMap insteadof WeakFastHashMap

2020-05-31 Thread JIN XU (Jira)


 [ 
https://issues.apache.org/jira/browse/BEANUTILS-539?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

JIN XU updated BEANUTILS-539:
-
Description: 
Hi.

throughtout my performance test (using Jprofiler), I found out WeakHashMap is 
far slower than ConcurrentHashMap.

Should we use ConcurrentHashMap instead of WeakHashMap?

I will attach my test codes here.
{code:java}
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.commons.beanutils2;

import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;

/**
 * A customized implementation of {@code java.util.HashMap} designed
 * to operate in a multi-threaded environment where the large majority of
 * method calls are read-only, instead of structural changes.  When operating
 * in "fast" mode, read calls are non-synchronized and write calls perform the
 * following steps:
 * 
 * Clone the existing collection
 * Perform the modification on the clone
 * Replace the existing collection with the (modified) clone
 * 
 * When first created, objects of this class default to "slow" mode, where
 * all accesses of any type are synchronized but no cloning takes place.  This
 * is appropriate for initially populating the collection, followed by a switch
 * to "fast" mode (by calling {@code setFast(true)}) after initialization
 * is complete.
 *
 * NOTE: If you are creating and accessing a
 * {@code HashMap} only within a single thread, you should use
 * {@code java.util.HashMap} directly (with no synchronization), for
 * maximum performance.
 *
 * NOTE: This class is not cross-platform.
 * Using it may cause unexpected failures on some architectures.
 * It suffers from the same problems as the double-checked locking idiom.
 * In particular, the instruction that clones the internal collection and the
 * instruction that sets the internal reference to the clone can be executed
 * or perceived out-of-order.  This means that any read operation might fail
 * unexpectedly, as it may be reading the state of the internal collection
 * before the internal collection is fully formed.
 * For more information on the double-checked locking idiom, see the
 * http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html;>
 * Double-Checked Locking Idiom Is Broken Declaration.
 *
 * @since Commons Collections 1.0
 */
public class WeakFastHashMap extends HashMap {

private static final long serialVersionUID = 1L;

/**
 * The underlying map we are managing.
 */
private volatile Map map = null;

/**
 * Are we currently operating in "fast" mode?
 */
private boolean fast = false;

// Constructors


/**
 * Construct an empty map.
 */
public WeakFastHashMap() {
super();
this.map = createMap();
}

/**
 * Construct an empty map with the specified capacity.
 *
 * @param capacity  the initial capacity of the empty map
 */
public WeakFastHashMap(final int capacity) {
super();
this.map = createMap(capacity);
}

/**
 * Construct an empty map with the specified capacity and load factor.
 *
 * @param capacity  the initial capacity of the empty map
 * @param factor  the load factor of the new map
 */
public WeakFastHashMap(final int capacity, final float factor) {
super();
this.map = createMap(capacity, factor);
}

/**
 * Construct a new map with the same mappings as the specified map.
 *
 * @param map  the map whose mappings are to be copied
 */
public WeakFastHashMap(final Map map) {
super();
this.map = createMap(map);
}

// Property access


/**
 *  Returns true if this map is operating in fast mode.
 *
 *  @return true if this map is operating in fast mode
 */
public boolean getFast() {
return this.fast;
}

/**
 *  Sets whether this map is operating in fast mode.
 *
 *  @param fast true if this map should operate in fast mode
 */
public void setFast(final 

[jira] [Updated] (BEANUTILS-539) use ConcurrentHashMap insteadof WeakFastHashMap

2020-05-31 Thread JIN XU (Jira)


 [ 
https://issues.apache.org/jira/browse/BEANUTILS-539?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

JIN XU updated BEANUTILS-539:
-
Description: 
Hi.

throughtout my performance test (using Jprofiler), I found out WeakHashMap is 
far slower than ConcurrentHashMap.

Should we use ConcurrentHashMap instead of WeakHashMap?

I will attach my test codes here.
{code:java}
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.commons.beanutils2;

import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;

/**
 * A customized implementation of {@code java.util.HashMap} designed
 * to operate in a multi-threaded environment where the large majority of
 * method calls are read-only, instead of structural changes.  When operating
 * in "fast" mode, read calls are non-synchronized and write calls perform the
 * following steps:
 * 
 * Clone the existing collection
 * Perform the modification on the clone
 * Replace the existing collection with the (modified) clone
 * 
 * When first created, objects of this class default to "slow" mode, where
 * all accesses of any type are synchronized but no cloning takes place.  This
 * is appropriate for initially populating the collection, followed by a switch
 * to "fast" mode (by calling {@code setFast(true)}) after initialization
 * is complete.
 *
 * NOTE: If you are creating and accessing a
 * {@code HashMap} only within a single thread, you should use
 * {@code java.util.HashMap} directly (with no synchronization), for
 * maximum performance.
 *
 * NOTE: This class is not cross-platform.
 * Using it may cause unexpected failures on some architectures.
 * It suffers from the same problems as the double-checked locking idiom.
 * In particular, the instruction that clones the internal collection and the
 * instruction that sets the internal reference to the clone can be executed
 * or perceived out-of-order.  This means that any read operation might fail
 * unexpectedly, as it may be reading the state of the internal collection
 * before the internal collection is fully formed.
 * For more information on the double-checked locking idiom, see the
 * http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html;>
 * Double-Checked Locking Idiom Is Broken Declaration.
 *
 * @since Commons Collections 1.0
 */
public class WeakFastHashMap extends HashMap {

private static final long serialVersionUID = 1L;

/**
 * The underlying map we are managing.
 */
private volatile Map map = null;

/**
 * Are we currently operating in "fast" mode?
 */
private boolean fast = false;

// Constructors


/**
 * Construct an empty map.
 */
public WeakFastHashMap() {
super();
this.map = createMap();
}

/**
 * Construct an empty map with the specified capacity.
 *
 * @param capacity  the initial capacity of the empty map
 */
public WeakFastHashMap(final int capacity) {
super();
this.map = createMap(capacity);
}

/**
 * Construct an empty map with the specified capacity and load factor.
 *
 * @param capacity  the initial capacity of the empty map
 * @param factor  the load factor of the new map
 */
public WeakFastHashMap(final int capacity, final float factor) {
super();
this.map = createMap(capacity, factor);
}

/**
 * Construct a new map with the same mappings as the specified map.
 *
 * @param map  the map whose mappings are to be copied
 */
public WeakFastHashMap(final Map map) {
super();
this.map = createMap(map);
}

// Property access


/**
 *  Returns true if this map is operating in fast mode.
 *
 *  @return true if this map is operating in fast mode
 */
public boolean getFast() {
return this.fast;
}

/**
 *  Sets whether this map is operating in fast mode.
 *
 *  @param fast true if this map should operate in fast mode
 */
public void setFast(final