From 552a13415573da19eafa46e1ac00fb0eb68f2b23 Mon Sep 17 00:00:00 2001 From: Vladimir Marko Date: Tue, 31 Oct 2017 10:56:47 +0000 Subject: ART: Optimize StringBuilder append pattern. Recognize appending with StringBuilder and replace the entire expression with a runtime call that perfoms the append in a more efficient manner. For now, require the entire pattern to be in a single block and be very strict about the StringBuilder environment uses. Also, do not accept StringBuilder/char[]/Object/float/double arguments as they throw non-OOME exceptions and/or require a call from the entrypoint back to a helper function in Java; these shall be implemented later. Boot image size for aosp_taimen-userdebug: - before: arm/boot*.oat: 19653872 arm64/boot*.oat: 23292784 oat/arm64/services.odex: 22408664 - after: arm/boot*.oat: 19432184 (-216KiB) arm64/boot*.oat: 22992488 (-293KiB) oat/arm64/services.odex: 22376776 (-31KiB) Note that const-string in compiled boot image methods cannot throw, but for apps it can and therefore its environment can prevent the optimization for apps. We could implement either a simple carve-out for const-string or generic environment pruning to allow this pattern to be applied more often. Results for the new StringBuilderAppendBenchmark on taimen: timeAppendLongStrings: ~700ns -> ~200ns timeAppendStringAndInt: ~220ns -> ~140ns timeAppendStrings: ~200ns -> 130ns Bug: 19575890 Test: 697-checker-string-append Test: m test-art-host-gtest Test: testrunner.py --host --optimizing Test: aosp_taimen-userdebug boots. Test: run-gtests.sh Test: testrunner.py --target --optimizing Test: vogar --benchmark art/benchmark/stringbuilder-append/src/StringBuilderAppendBenchmark.java Change-Id: I51789bf299f5219f68ada4c077b6a1d3fe083964 --- compiler/optimizing/nodes.h | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'compiler/optimizing/nodes.h') diff --git a/compiler/optimizing/nodes.h b/compiler/optimizing/nodes.h index 8a8b371758..cb53ae3ada 100644 --- a/compiler/optimizing/nodes.h +++ b/compiler/optimizing/nodes.h @@ -1438,6 +1438,7 @@ class HLoopInformationOutwardIterator : public ValueObject { M(Shr, BinaryOperation) \ M(StaticFieldGet, Instruction) \ M(StaticFieldSet, Instruction) \ + M(StringBuilderAppend, Instruction) \ M(UnresolvedInstanceFieldGet, Instruction) \ M(UnresolvedInstanceFieldSet, Instruction) \ M(UnresolvedStaticFieldGet, Instruction) \ @@ -6889,6 +6890,55 @@ class HStaticFieldSet final : public HExpression<2> { const FieldInfo field_info_; }; +class HStringBuilderAppend final : public HVariableInputSizeInstruction { + public: + HStringBuilderAppend(HIntConstant* format, + uint32_t number_of_arguments, + ArenaAllocator* allocator, + uint32_t dex_pc) + : HVariableInputSizeInstruction( + kStringBuilderAppend, + DataType::Type::kReference, + // The runtime call may read memory from inputs. It never writes outside + // of the newly allocated result object (or newly allocated helper objects). + SideEffects::AllReads().Union(SideEffects::CanTriggerGC()), + dex_pc, + allocator, + number_of_arguments + /* format */ 1u, + kArenaAllocInvokeInputs) { + DCHECK_GE(number_of_arguments, 1u); // There must be something to append. + SetRawInputAt(FormatIndex(), format); + } + + void SetArgumentAt(size_t index, HInstruction* argument) { + DCHECK_LE(index, GetNumberOfArguments()); + SetRawInputAt(index, argument); + } + + // Return the number of arguments, excluding the format. + size_t GetNumberOfArguments() const { + DCHECK_GE(InputCount(), 1u); + return InputCount() - 1u; + } + + size_t FormatIndex() const { + return GetNumberOfArguments(); + } + + HIntConstant* GetFormat() { + return InputAt(FormatIndex())->AsIntConstant(); + } + + bool NeedsEnvironment() const override { return true; } + + bool CanThrow() const override { return true; } + + DECLARE_INSTRUCTION(StringBuilderAppend); + + protected: + DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend); +}; + class HUnresolvedInstanceFieldGet final : public HExpression<1> { public: HUnresolvedInstanceFieldGet(HInstruction* obj, -- cgit v1.2.3-59-g8ed1b