Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): remove usage of getResourceAsStream() #11058

Merged
merged 2 commits into from
Oct 28, 2019

Conversation

garymathews
Copy link
Contributor

@garymathews garymathews commented Jul 17, 2019

JIRA Ticket

Summary:

  • Remove usage of getResourceAsStream() as this method has high overhead
  • Utilize getResource() or getAsset() where appropriate

Test 1:
(This verifies we can still read our "build.properties" file.)

  1. Create a default Classic Titanium app.
  2. Build and run on Android.
  3. Look for a log message like the below on startup. Verify that the "Powered by Titanium" version is 8.3.0 and not 1.0.0
    <YourAppName> <YourAppVersion> (Powered by Titanium 8.3.0.2d9d45bfc7)

Test 2:
(This verifies that our WebView JS bindings still work.)

  1. Build and run the below code on Android.
  2. Wait for the WebView to load the appcelerator website.
  3. Verify the following gets logged.
    @@@ document.title: Home - Appcelerator | The Mobile First Platform
var lastLoadedTitle = null;
var window = Ti.UI.createWindow();
var webView = Ti.UI.createWebView({
	url: "https://appcelerator.com",
});
webView.addEventListener("load", function(e) {
	var loadedTitle = webView.evalJS("document.title;");
	if (loadedTitle !== lastLoadedTitle) {
		lastLoadedTitle = loadedTitle;
		Ti.API.info("@@@ document.title: " + loadedTitle);
	}
});
window.add(webView);
window.open();

Test 3:
(This verifies that our WebView JS bindings still work.)

  1. Build and run the below code on Android.
  2. Verify that you see a WebView which counts-down from 5 seconds.
  3. Verify that a DOMCountentLoaded toast message appears at the bottom of the screen when the countdown reaches zero.
var htmlText =
		'<!DOCTYPE html>' +
		'<html>' +
		'	<head>' +
		'		<meta name="viewport" content="width=device-width, initial-scale=1.0">' +
		'	</head>' +
		'	<body>' +
		'		<p>HTML JavaScript Interop Test</p>' +
		'		<p id="label"></p>' +
		'	</body>' +
		'	<script>' +
		'		document.addEventListener("DOMContentLoaded", function(e) {' +
		'			Ti.API.info("@@@ Firing events from HTML");' +
		'			Ti.App.fireEvent("app:htmlLoaded", { isRequestingStartTimer: true });' +
		'			Ti.App.fireEvent("app:htmlRequestingShowToast", { message: "DOMContentLoaded" });' +
		'		});' +
		'		function updateLabelTo(x) {' +
		'			document.getElementById("label").innerHTML = x;' +
		'		}' +
		'	</script>' +
		'</html>';

var window = Ti.UI.createWindow();
var webView = Ti.UI.createWebView({
	html: htmlText,
});
window.add(webView);
window.open();

var timerId;
var timerCount;
function updateWebView() {
	timerCount = 5;
	function onUpdate() {
		if (timerCount > 0) {
			Ti.API.info("@@@ Calling evalJS()");
			webView.evalJS("updateLabelTo('Reload in:  " + timerCount + "')", function() {});
			timerCount--;
		} else {
			Ti.API.info("@@@ Reloading WebView");
			clearInterval(timerId);
			timerId = null;
			timerCount = 5;
			webView.html = htmlText;
		}
	}
	timerId = setInterval(function(e) {
		onUpdate();
	}, 1000);
	onUpdate();
}

Ti.App.addEventListener("app:htmlLoaded", function(e) {
	Ti.API.info("@@@ Received event 'htmlLoaded'");
	if (e.isRequestingStartTimer) {
		updateWebView();
	}
});

Ti.App.addEventListener("app:htmlRequestingShowToast", function(e) {
	if (Ti.App.Android || Ti.App.Windows) {
		if (e.message) {
			Ti.UI.createNotification({
				message: e.message,
				duration: Ti.UI.NOTIFICATION_DURATION_SHORT,
			}).show();
		}
	}
});

Test 4
(This verifies our TableView icons still work.)

const win = Ti.UI.createWindow({
    backgroundColor: 'gray'
});
const table = Ti.UI.createTableView({
    data: [
        {
            title: 'Apples',
            hasCheck: true
        },
        {
            title: 'Bananas',
            hasChild: true
        },
        {
            title: 'Carrots'
        },
        {
            title: 'Potatoes'
        }
    ]
});
win.add(table);
win.open();

@jquick-axway
Copy link
Contributor

jquick-axway commented Jul 18, 2019

I've updated the PR's summary with test code that will test your changes.

I think the code which loads "ti:" URL schemes might be dead code. We only use a "ti:" scheme when assigning a "sourceURL" path to our JavaScript files. I've never seen us do this with drawables/images before. I think it's okay to keep your code changes just-in-case though.

@garymathews
Copy link
Contributor Author

garymathews commented Jul 25, 2019

@jquick-axway Updated PR, thanks for the tests!

@sgtcoolguy
Copy link
Contributor

@garymathews Got any timing data? Just curious, since you've made such a big perf boost in the snapshot stuff you've done...

Copy link
Contributor

@jquick-axway jquick-axway left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CR: Pass

@jquick-axway
Copy link
Contributor

jquick-axway commented Jul 27, 2019

I just took some benchmarks.

Since every app is getting burned by us reading "build.properties" on startup, this is what I measured. I measured the old getResourceAsStream("build.properties") method call and this PR's KrollAssetHelper.openAsset("build.properties") method call that replaces it (which internally calls Google's openAsset() method).

Nexus 4:

  • Before PR: 300 ms
  • After PR: 0.678 ms
  • Result: 442.5x faster

Pixel 2:

  • Before PR: 16 ms
  • After PR: 0.180 ms
  • Result: 88.9x faster

It's so fast, I had to time it in nanoseconds to measure it properly. :)

The key thing here is this PR shaves off 299.4ms from the app startup time on our Nexus 4 (our slowest device). Gary's snapshot PR shaves off an additional 3057ms from the Nexus 4 startup time. Every bit helps. This is looking great and our launch times now feel on-par with Google's own major apps on the same device.

@garymathews garymathews changed the title fix(android): remove usage of getResourceAsStream() fix(android)(8_3_X): remove usage of getResourceAsStream() Aug 14, 2019
@garymathews garymathews changed the base branch from master to 8_3_X August 14, 2019 18:16
@keerthi1032
Copy link
Contributor

@garymathews I don't see the fix with sdk in this PR .with the testcase 1 im seeing powered by 1.0.n/a not 8.3 .for test case 2 &3 throws following error and for 4 table view appears different than GA.Can you please take a look
Screen Shot 2019-08-19 at 11 57 17 AM

@sgtcoolguy sgtcoolguy changed the base branch from 8_3_X to master September 9, 2019 18:01
@sgtcoolguy sgtcoolguy changed the title fix(android)(8_3_X): remove usage of getResourceAsStream() fix(android): remove usage of getResourceAsStream() Sep 9, 2019
@sgtcoolguy sgtcoolguy self-requested a review September 10, 2019 13:46
Copy link
Contributor

@sgtcoolguy sgtcoolguy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's 2 changes that @jquick-axway mentions that need to be addressed before this can be FR'd

@keerthi1032
Copy link
Contributor

@garymathews I'm still seeing the same error as before #11058 (comment) can you please look at it

@garymathews garymathews force-pushed the TIMOB-26910 branch 2 times, most recently from 09822d4 to ffb2d4c Compare October 15, 2019 22:36
@build
Copy link
Contributor

build commented Oct 15, 2019

Warnings
⚠️

Commit 12362357152ec21e59d3a74fbfd2dd8e10791a85 has a message "fix(android): TableView item image size" giving 1 errors:

  • subject must not be sentence-case, start-case, pascal-case, upper-case
Messages
📖 👍 Hey!, You deleted more code than you added. That's awesome!
📖

💾 Here's the generated SDK zipfile.

📖

✅ All tests are passing
Nice one! All 4407 tests are passing.
(There are 479 skipped tests not included in that total)

📖

🚨 This PR has one or more commits with warnings/errors for commit messages not matching our configuration. You may want to squash merge this PR and edit the message to match our conventions, or ask the original developer to modify their history.

Generated by 🚫 dangerJS against 1236235

@garymathews garymathews force-pushed the TIMOB-26910 branch 2 times, most recently from 779d4ce to 81ce27c Compare October 16, 2019 18:23
@ssekhri
Copy link

ssekhri commented Oct 21, 2019

@garymathews there is an issue with the PR related to tableviewRow height. Any tableview row with icons is shown with more height and also the icons are displayed quite big. Use the test code from Test4 in this PR.
Tested on:
Mac OS: 10.14.5
SDK: 8.3.0.v20191016113141
Appc CLI: 7.1.1
JDK: 1.8.0_162
Node: 10.5.0
Device: Nexus 4
Nexus4_TableView

@garymathews garymathews force-pushed the TIMOB-26910 branch 2 times, most recently from 10db315 to f934825 Compare October 25, 2019 21:18
@garymathews
Copy link
Contributor Author

@ssekhri Pushed a fix

@ssekhri
Copy link

ssekhri commented Oct 28, 2019

FR Passed.
Verified on:
Mac OS: 10.14.5
SDK: 8.3.0.v20191025142840
Appc CLI: 7.1.1
JDK: 1.8.0_162
Node: 10.5.0
Studio: 5.1.4.201909061933
Device: Nexus4(v5.1.1) device, Pixel3(v10.0) emulator

@ssekhri ssekhri merged commit a4c2b7a into tidev:master Oct 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants