Reduce memory allocations in dexlayout's CodeFixups.
Using heaptrack for the services.odex compilation of
aosp_taimen-userdebug:
- before: calls to allocation functions: 5032880
- after: calls to allocation functions: 4645378
Test: Rely on TreeHugger
Bug: 70331349
Change-Id: I477d5a84e0bf22994ce3de9fe56525de0b498801
diff --git a/dexlayout/dex_ir.cc b/dexlayout/dex_ir.cc
index 2af579c..90df2d7 100644
--- a/dexlayout/dex_ir.cc
+++ b/dexlayout/dex_ir.cc
@@ -674,20 +674,20 @@
// Add "fixup" references to types, strings, methods, and fields.
// This is temporary, as we will probably want more detailed parsing of the
// instructions here.
- std::unique_ptr<std::vector<TypeId*>> type_ids(new std::vector<TypeId*>());
- std::unique_ptr<std::vector<StringId*>> string_ids(new std::vector<StringId*>());
- std::unique_ptr<std::vector<MethodId*>> method_ids(new std::vector<MethodId*>());
- std::unique_ptr<std::vector<FieldId*>> field_ids(new std::vector<FieldId*>());
+ std::vector<TypeId*> type_ids;
+ std::vector<StringId*> string_ids;
+ std::vector<MethodId*> method_ids;
+ std::vector<FieldId*> field_ids;
if (GetIdsFromByteCode(*this,
code_item,
- type_ids.get(),
- string_ids.get(),
- method_ids.get(),
- field_ids.get())) {
- CodeFixups* fixups = new CodeFixups(type_ids.release(),
- string_ids.release(),
- method_ids.release(),
- field_ids.release());
+ /*out*/ &type_ids,
+ /*out*/ &string_ids,
+ /*out*/ &method_ids,
+ /*out*/ &field_ids)) {
+ CodeFixups* fixups = new CodeFixups(std::move(type_ids),
+ std::move(string_ids),
+ std::move(method_ids),
+ std::move(field_ids));
code_item->SetCodeFixups(fixups);
}
diff --git a/dexlayout/dex_ir.h b/dexlayout/dex_ir.h
index 8421774..b25e164 100644
--- a/dexlayout/dex_ir.h
+++ b/dexlayout/dex_ir.h
@@ -1013,25 +1013,25 @@
class CodeFixups {
public:
- CodeFixups(std::vector<TypeId*>* type_ids,
- std::vector<StringId*>* string_ids,
- std::vector<MethodId*>* method_ids,
- std::vector<FieldId*>* field_ids)
- : type_ids_(type_ids),
- string_ids_(string_ids),
- method_ids_(method_ids),
- field_ids_(field_ids) { }
+ CodeFixups(std::vector<TypeId*> type_ids,
+ std::vector<StringId*> string_ids,
+ std::vector<MethodId*> method_ids,
+ std::vector<FieldId*> field_ids)
+ : type_ids_(std::move(type_ids)),
+ string_ids_(std::move(string_ids)),
+ method_ids_(std::move(method_ids)),
+ field_ids_(std::move(field_ids)) { }
- std::vector<TypeId*>* TypeIds() const { return type_ids_.get(); }
- std::vector<StringId*>* StringIds() const { return string_ids_.get(); }
- std::vector<MethodId*>* MethodIds() const { return method_ids_.get(); }
- std::vector<FieldId*>* FieldIds() const { return field_ids_.get(); }
+ const std::vector<TypeId*>& TypeIds() const { return type_ids_; }
+ const std::vector<StringId*>& StringIds() const { return string_ids_; }
+ const std::vector<MethodId*>& MethodIds() const { return method_ids_; }
+ const std::vector<FieldId*>& FieldIds() const { return field_ids_; }
private:
- std::unique_ptr<std::vector<TypeId*>> type_ids_;
- std::unique_ptr<std::vector<StringId*>> string_ids_;
- std::unique_ptr<std::vector<MethodId*>> method_ids_;
- std::unique_ptr<std::vector<FieldId*>> field_ids_;
+ std::vector<TypeId*> type_ids_;
+ std::vector<StringId*> string_ids_;
+ std::vector<MethodId*> method_ids_;
+ std::vector<FieldId*> field_ids_;
DISALLOW_COPY_AND_ASSIGN(CodeFixups);
};
diff --git a/dexlayout/dex_visualize.cc b/dexlayout/dex_visualize.cc
index 4b46341..e4ed69b 100644
--- a/dexlayout/dex_visualize.cc
+++ b/dexlayout/dex_visualize.cc
@@ -188,20 +188,16 @@
DumpAddressRange(code_item, class_index);
const dex_ir::CodeFixups* fixups = code_item->GetCodeFixups();
if (fixups != nullptr) {
- std::vector<dex_ir::TypeId*>* type_ids = fixups->TypeIds();
- for (dex_ir::TypeId* type_id : *type_ids) {
+ for (dex_ir::TypeId* type_id : fixups->TypeIds()) {
DumpTypeId(type_id, class_index);
}
- std::vector<dex_ir::StringId*>* string_ids = fixups->StringIds();
- for (dex_ir::StringId* string_id : *string_ids) {
+ for (dex_ir::StringId* string_id : fixups->StringIds()) {
DumpStringId(string_id, class_index);
}
- std::vector<dex_ir::MethodId*>* method_ids = fixups->MethodIds();
- for (dex_ir::MethodId* method_id : *method_ids) {
+ for (dex_ir::MethodId* method_id : fixups->MethodIds()) {
DumpMethodId(method_id, class_index);
}
- std::vector<dex_ir::FieldId*>* field_ids = fixups->FieldIds();
- for (dex_ir::FieldId* field_id : *field_ids) {
+ for (dex_ir::FieldId* field_id : fixups->FieldIds()) {
DumpFieldId(field_id, class_index);
}
}
diff --git a/dexlayout/dexlayout.cc b/dexlayout/dexlayout.cc
index d904a52..1016fca 100644
--- a/dexlayout/dexlayout.cc
+++ b/dexlayout/dexlayout.cc
@@ -1656,11 +1656,11 @@
continue;
}
// Add const-strings.
- for (dex_ir::StringId* id : *fixups->StringIds()) {
+ for (dex_ir::StringId* id : fixups->StringIds()) {
from_hot_method[id->GetIndex()] = true;
}
// Add field classes, names, and types.
- for (dex_ir::FieldId* id : *fixups->FieldIds()) {
+ for (dex_ir::FieldId* id : fixups->FieldIds()) {
// TODO: Only visit field ids from static getters and setters.
from_hot_method[id->Class()->GetStringId()->GetIndex()] = true;
from_hot_method[id->Name()->GetIndex()] = true;
@@ -1668,7 +1668,7 @@
}
// For clinits, add referenced method classes, names, and protos.
if (is_clinit) {
- for (dex_ir::MethodId* id : *fixups->MethodIds()) {
+ for (dex_ir::MethodId* id : fixups->MethodIds()) {
from_hot_method[id->Class()->GetStringId()->GetIndex()] = true;
from_hot_method[id->Name()->GetIndex()] = true;
is_shorty[id->Proto()->Shorty()->GetIndex()] = true;