I use dexlib2-3.0.9 to modify dex.
DexBackedDexFile dexFile = ?;
MemoryDataStore memoryDataStore = new MemoryDataStore();
DexRewriter dexRewriter = new DexRewriter(new MyRewriterModule());
Rewriter<DexFile> 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:
public class ClassPool extends ... {
...
public void intern(@Nonnull ClassDef classDef) {
...
HashSet<String> fields = new HashSet<String>();
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?
I use dexlib2-3.0.9 to modify dex.
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:
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?