# Optimizing Bytecode by Manipulating Source Code

DevFeed: [Optimizing Bytecode by Manipulating Source Code](<https://devfeed.tech/articles/optimizing-bytecode-by-manipulating-source-code-20950.md>)

Original publisher: [Read original article](<https://jakewharton.com/optimizing-bytecode-by-manipulating-source-code/>)

Published: 2019-04-02T00:00:00Z

Content type: article

Language: en

Sources: [Jake Wharton](<https://devfeed.tech/sources/jake-wharton.md>)

Topics: [Optimization](<https://devfeed.tech/topics/optimization.md>), [Code](<https://devfeed.tech/topics/code.md>), [Exception](<https://devfeed.tech/topics/exception.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [exception](<https://devfeed.tech/tags/exception.md>), [optimization](<https://devfeed.tech/tags/optimization.md>), [post](<https://devfeed.tech/tags/post.md>)

## AI overview

This article examines optimizing generated bytecode by changing its source code. It focuses on reducing duplicated exception-handling bytecode and improving the common execution path when required views are present, including the different control-flow arrangement produced by D8.

## Source excerpt

This post is a follow-up to "The Economics of Generated Code" which argued that spending time optimizing generated code is more worthwhile than the same optimizations done in manually-written code. The second example from that post dealt with looking up views, checking for null, and potentially throwing an exception. In an effort to reduce the impact of the generated exception message string, each was split into a prefix which will be de-duplicated and the view ID name which was effectively free since it matched a field name. If you're lost on what that all means, check out the other post first. public static MainBinding bind(View root) { TextView name = root.findViewById(R.id.name); if (name == null) { - throw new NullPointerException("View 'name' required but not found"); + throw new NullPointerException("Missing required view with ID: ".concat("name")); } TextView email = root.findViewById(R.id.email); if (email == null) { - throw new NullPointerException("View 'email' required but not found"); + throw new NullPointerException("Missing required view with ID: ".concat("email")); } return new MainBinding(root, name, email); } That change was just about strings, but I also mentioned that there's more optimization which could be done. So let's do it! By virtue of the fact that we throw an exception when a view is absent, that case is expected to be rare. This is what allowed us to justify sacrificing a single string constant in favor of multiple constants and runtime concatenation. While that allowed us to de-duplicate the strings, it creates more duplication in the bytecode. [000288] MainBinding.bind:(Landroid/view/View;)LMainBinding; 0000: sget v0, LR$id;.name:I 0002: invoke-virtual {v3, v0}, Landroid/view/View;.findViewById:(I)Landroid/view/View; 0005: move-result-object v0 0006: check-cast v0, Landroid/widget/TextView; 0008: if-nez v0, 0018 000a: new-instance v0, Ljava/lang/NullPointerException; 000c: const-string v1, "Missing required view with ID: " 000e: const