DexPool writeTo() very slow on R$id
- Dominant language
- Java
- Stars
- 400
- Forks
- 67
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 1
Description
I use dexlib2-3.0.9 to modify dex.
```java
DexBackedDexFile dexFile = ?;
MemoryDataStore memoryDataStore = new MemoryDataStore();
DexRewriter dexRewriter = new DexRewriter(new MyRewriterModule());
Rewriter dexFileRewriter = dexRewriter.getDexFileRewriter();
DexPool.writeTo(memoryDataStore, dexFileRewriter.rewrite(dexNative.dexFile));
```
Now, When parse and write R$id or R$string or R$drawable (include 20000+ field), very slow for more than 60s.
I trace code for dexlib2, and locate at here:
```java
public class ClassPool extends ... {
...
public void intern(@Nonnull ClassDef classDef) {
...
HashSet fields = new HashSet();
for (Field field: poolClassDef.getFields()) {
String fieldDescriptor = DexFormatter.INSTANCE.getShortFieldDescriptor(field);
if (!fields.add(fieldDescriptor)) {
throw new ExceptionWithContext("Multiple definitions for field %s->%s",
poolClassDef.getType(), fieldDescriptor);
}
dexPool.fieldSection.intern(field);
EncodedValue initialValue = field.getInitialValue();
if (initialValue != null) {
dexPool.internEncodedValue(initialValue);
}
dexPool.annotationSetSection.intern(field.getAnnotations());
ArrayEncodedValue staticInitializers = getStaticInitializers(poolClassDef);
if (staticInitializers != null) {
dexPool.encodedArraySection.intern(staticInitializers);
}
}
...
}
...
}
```
OK, maybe I found it, it call `dexPool.encodedArraySection.intern(staticInitializers);` for every field.
So I move "staticInitializers" out of loop, OK, it look like No Problem, and it be very fast!
I'm wondering if this is a dexlib2 coding bug?
Contributor guide
Assessment
This issue has not been assessed yet.