Skip to content

Commit f6c2dc2

Browse files
committed
Minimize HashSet object creation #97 #98
1 parent daf17cb commit f6c2dc2

2 files changed

Lines changed: 349 additions & 28 deletions

File tree

‎src/main/java/com/reandroid/arsc/item/StringItem.java‎

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/*
2-
* Copyright (C) 2022 github.com/REAndroid
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
2+
* Copyright (C) 2022 github.com/REAndroid
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
1616
package com.reandroid.arsc.item;
1717

1818
import com.reandroid.arsc.base.Block;
@@ -24,9 +24,9 @@
2424
import com.reandroid.json.JSONConvert;
2525
import com.reandroid.json.JSONObject;
2626
import com.reandroid.utils.CompareUtil;
27+
import com.reandroid.utils.HashSetStore;
2728
import com.reandroid.utils.ObjectsUtil;
2829
import com.reandroid.utils.collection.ComputeIterator;
29-
import com.reandroid.utils.collection.FilterIterator;
3030
import com.reandroid.xml.StyleDocument;
3131
import org.xmlpull.v1.XmlSerializer;
3232

@@ -36,21 +36,18 @@
3636
import java.nio.charset.CharacterCodingException;
3737
import java.nio.charset.CharsetDecoder;
3838
import java.nio.charset.StandardCharsets;
39-
import java.util.HashSet;
4039
import java.util.Iterator;
41-
import java.util.Set;
4240
import java.util.function.Predicate;
4341

4442
public class StringItem extends StringBlock implements JSONConvert<JSONObject>, Comparable<StringItem> {
4543

4644
private boolean mUtf8;
47-
private final Set<ReferenceItem> mReferencedList;
45+
private Object mReferencedSet;
4846
private StyleItem mStyleItem;
4947

5048
public StringItem(boolean utf8) {
5149
super();
5250
this.mUtf8 = utf8;
53-
this.mReferencedList = new HashSet<>();
5451
}
5552

5653
public StyleDocument getStyleDocument() {
@@ -76,22 +73,21 @@ public<T extends Block> Iterator<T> getUsers(Class<T> parentClass,
7673
}
7774

7875
public void removeReference(ReferenceItem reference) {
79-
mReferencedList.remove(reference);
76+
mReferencedSet = HashSetStore.remove(mReferencedSet, reference);
8077
}
8178
public void clearReferences() {
82-
mReferencedList.clear();
79+
mReferencedSet = HashSetStore.clear(mReferencedSet);
8380
}
8481
public boolean hasReference() {
8582
ensureStringLinkUnlocked();
86-
return FilterIterator.of(mReferencedList.iterator(), referenceItem ->
87-
!(referenceItem instanceof StyleItem.StyleIndexReference)).hasNext();
83+
return HashSetStore.containsIf(mReferencedSet, NON_STYLE_INDEX);
8884
}
8985
public int getReferencesSize() {
90-
return mReferencedList.size();
86+
return HashSetStore.size(mReferencedSet);
9187
}
9288
public Iterator<ReferenceItem> getReferences() {
9389
ensureStringLinkUnlocked();
94-
return mReferencedList.iterator();
90+
return HashSetStore.iterator(mReferencedSet);
9591
}
9692
void ensureStringLinkUnlocked() {
9793
StringPool<?> stringPool = getParentInstance(StringPool.class);
@@ -101,15 +97,17 @@ void ensureStringLinkUnlocked() {
10197
}
10298
public void addReference(ReferenceItem reference) {
10399
if (reference != null) {
104-
mReferencedList.add(reference);
100+
mReferencedSet = HashSetStore.add(mReferencedSet, reference);
105101
int index = this.getIndex();
106102
if (reference.get() != index) {
107103
reference.set(index);
108104
}
109105
}
110106
}
111107
private void reUpdateReferences(int newIndex) {
112-
for (ReferenceItem reference : new HashSet<>(mReferencedList)) {
108+
Iterator<ReferenceItem> iterator = HashSetStore.clonedIterator(mReferencedSet);
109+
while (iterator.hasNext()) {
110+
ReferenceItem reference = iterator.next();
113111
reference.set(newIndex);
114112
}
115113
}
@@ -361,7 +359,9 @@ public void transferReferences(StringItem source) {
361359
if (getIndex() < 0 || source.getIndex() < 0) {
362360
return;
363361
}
364-
for (ReferenceItem reference : new HashSet<>(source.mReferencedList)) {
362+
Iterator<ReferenceItem> iterator = HashSetStore.clonedIterator(source.mReferencedSet);
363+
while (iterator.hasNext()) {
364+
ReferenceItem reference = iterator.next();
365365
if (isTransferable(reference)) {
366366
source.removeReference(reference);
367367
addReference(reference);
@@ -599,4 +599,7 @@ private static byte[] addBytes(byte[] bytes1, byte[] bytes2, byte[] bytes3) {
599599

600600
public static final String NAME_string = ObjectsUtil.of("string");
601601
public static final String NAME_style = ObjectsUtil.of("style");
602+
603+
private static final Predicate<Object> NON_STYLE_INDEX = item ->
604+
!(item instanceof StyleItem.StyleIndexReference);
602605
}

0 commit comments

Comments
 (0)