# ignite-47 Optimize injection: use GridLeanIdentitySet. (cherry picked from commit 6d2f81a)
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/b73239ff Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/b73239ff Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/b73239ff Branch: refs/heads/ignite-45 Commit: b73239ff038af217c8f8a0cbbd3771c083eef731 Parents: 573b658 Author: sevdokimov <sevdoki...@gridgain.com> Authored: Sat Mar 7 16:40:17 2015 +0300 Committer: sevdokimov <sevdoki...@gridgain.com> Committed: Wed Mar 11 18:30:15 2015 +0300 ---------------------------------------------------------------------- .../processors/resource/GridResourceIoc.java | 2 +- .../internal/util/GridIdentityHashSet.java | 63 ---------- .../internal/util/GridLeanIdentitySet.java | 117 +++++++++++++++++++ .../ignite/internal/util/IgniteUtils.java | 2 +- .../lang/utils/GridLeanIdentitySetSelfTest.java | 62 ++++++++++ .../testsuites/IgniteLangSelfTestSuite.java | 1 + 6 files changed, 182 insertions(+), 65 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b73239ff/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceIoc.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceIoc.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceIoc.java index 1ebe2f5..e86dde9 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceIoc.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceIoc.java @@ -113,7 +113,7 @@ class GridResourceIoc { if (isAnnotationPresent(target, annCls, dep)) // Use identity hash set to compare via referential equality. - return injectInternal(target, annCls, injector, dep, depCls, new GridIdentityHashSet<>(3)); + return injectInternal(target, annCls, injector, dep, depCls, new GridLeanIdentitySet<>()); return false; } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b73239ff/modules/core/src/main/java/org/apache/ignite/internal/util/GridIdentityHashSet.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/GridIdentityHashSet.java b/modules/core/src/main/java/org/apache/ignite/internal/util/GridIdentityHashSet.java deleted file mode 100644 index a48679c..0000000 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/GridIdentityHashSet.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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.ignite.internal.util; - -import org.apache.ignite.internal.util.typedef.*; -import org.apache.ignite.internal.util.typedef.internal.*; - -import java.util.*; - -/** - * Set counterpart for {@link IdentityHashMap}. - */ -public class GridIdentityHashSet<E> extends GridSetWrapper<E> { - /** */ - private static final long serialVersionUID = 0L; - - /** - * Creates default identity hash set. - */ - public GridIdentityHashSet() { - super(new IdentityHashMap<E, Object>()); - } - - /** - * Creates identity hash set of given size. - * - * @param size Start size for the set. - */ - public GridIdentityHashSet(int size) { - super(new IdentityHashMap<E, Object>(size)); - - A.ensure(size >= 0, "size >= 0"); - } - - /** - * Creates identity has set initialized given collection. - * - * @param vals Values to initialize. - */ - public GridIdentityHashSet(Collection<E> vals) { - super(F.isEmpty(vals) ? new IdentityHashMap<E, Object>(0) : new IdentityHashMap<E, Object>(vals.size()), vals); - } - - /** {@inheritDoc} */ - @Override public String toString() { - return S.toString(GridIdentityHashSet.class, this, super.toString()); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b73239ff/modules/core/src/main/java/org/apache/ignite/internal/util/GridLeanIdentitySet.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/GridLeanIdentitySet.java b/modules/core/src/main/java/org/apache/ignite/internal/util/GridLeanIdentitySet.java new file mode 100644 index 0000000..a4e18c8 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/GridLeanIdentitySet.java @@ -0,0 +1,117 @@ +/* + * 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.ignite.internal.util; + +import java.util.*; + +/** + * + */ +public class GridLeanIdentitySet<T> extends AbstractSet<T> { + /** */ + private static final int MAX_ARR_SIZE = 8; + + /** */ + private Object data; + + /** */ + private int size; + + /** {@inheritDoc} */ + @Override public boolean contains(Object o) { + if (size == 0) + return false; + + if (size == 1) + return o == data; + + if (size <= MAX_ARR_SIZE) { + Object[] arr = (Object[])data; + + for (int i = 0; i < size; i++) { + if (arr[i] == o) + return true; + } + + return false; + } + + return ((Map<Object, Boolean>)data).containsKey(o); + } + + /** {@inheritDoc} */ + @Override public boolean add(T t) { + if (size > MAX_ARR_SIZE) { + if (((Map<Object, Boolean>)data).put(t, Boolean.TRUE) == null) { + size++; + + return true; + } + + return false; + } + + if (contains(t)) + return false; + + if (size == 0) + data = t; + else if (size == 1) { + Object[] arr = new Object[MAX_ARR_SIZE]; + + arr[0] = data; + arr[1] = t; + + data = arr; + } + else if (size < MAX_ARR_SIZE) + ((Object[])data)[size] = t; + else if (size == MAX_ARR_SIZE) { + Map<Object, Boolean> map = new IdentityHashMap<>(); + + for (Object o : (Object[])data) + map.put(o, Boolean.TRUE); + + map.put(t, Boolean.TRUE); + + assert map.size() == size + 1; + + data = map; + } + + size++; + + return true; + } + + /** {@inheritDoc} */ + @Override public void clear() { + data = null; + size = 0; + } + + /** {@inheritDoc} */ + @Override public Iterator<T> iterator() { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + @Override public int size() { + return size; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b73239ff/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java b/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java index 21c62cc..cf21d46 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java @@ -5368,7 +5368,7 @@ public abstract class IgniteUtils { * @return Top level user class. */ public static GridPeerDeployAware detectPeerDeployAware(GridPeerDeployAware obj) { - GridPeerDeployAware p = nestedPeerDeployAware(obj, true, new GridIdentityHashSet<>(3)); + GridPeerDeployAware p = nestedPeerDeployAware(obj, true, new GridLeanIdentitySet<>()); // Pass in obj.getClass() to avoid infinite recursion. return p != null ? p : peerDeployAware(obj.getClass()); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b73239ff/modules/core/src/test/java/org/apache/ignite/lang/utils/GridLeanIdentitySetSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/lang/utils/GridLeanIdentitySetSelfTest.java b/modules/core/src/test/java/org/apache/ignite/lang/utils/GridLeanIdentitySetSelfTest.java new file mode 100644 index 0000000..0ab3ebc --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/lang/utils/GridLeanIdentitySetSelfTest.java @@ -0,0 +1,62 @@ +/* + * 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.ignite.lang.utils; + +import org.apache.ignite.internal.util.*; +import org.apache.ignite.testframework.junits.common.*; + +import java.util.*; + +/** + * Tests for {@link org.apache.ignite.internal.util.GridLeanMap}. + */ +@GridCommonTest(group = "Lang") +public class GridLeanIdentitySetSelfTest extends GridCommonAbstractTest { + /** + * JUnit. + * + * @throws Exception If failed. + */ + public void testAddSizeContainsClear() throws Exception { + Set<Integer> set = new GridLeanIdentitySet<>(); + + assert set.isEmpty(); + + for (int i = 0; i < 100; i++) { + assertEquals(i, set.size()); + + for (int j = 0; j < 100; j++) { + if (j < i) { + assert set.contains(Integer.valueOf(j)); + + assert !set.add(j); + } + else + assert !set.contains(Integer.valueOf(j)); + } + + assert set.add(i); + + assert !set.isEmpty(); + } + + set.clear(); + + assert set.isEmpty(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b73239ff/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteLangSelfTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteLangSelfTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteLangSelfTestSuite.java index 4ad8ba6..b4d5bbc 100644 --- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteLangSelfTestSuite.java +++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteLangSelfTestSuite.java @@ -39,6 +39,7 @@ public class IgniteLangSelfTestSuite extends TestSuite { suite.addTest(new TestSuite(GridTupleSelfTest.class)); suite.addTest(new TestSuite(GridByteArrayListSelfTest.class)); suite.addTest(new TestSuite(GridLeanMapSelfTest.class)); + suite.addTest(new TestSuite(GridLeanIdentitySetSelfTest.class)); suite.addTest(new TestSuite(GridListSetSelfTest.class)); suite.addTest(new TestSuite(GridSetWrapperSelfTest.class)); suite.addTest(new TestSuite(GridConcurrentWeakHashSetSelfTest.class));