From 33c32bc0356cbfd1247496adc67fae3d1eaf4eb8 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 18 May 2015 21:10:08 +0300 Subject: [PATCH] Speed up timezone detection --- .../classlib/impl/tz/DateTimeZoneProvider.java | 2 +- .../teavm/classlib/java/util/TCalendar.java | 18 ++++++++++++++++-- .../resources/org/teavm/javascript/runtime.js | 7 +++++-- .../org/teavm/dom/canvas/CanvasGradient.java | 10 +++++----- .../teavm/classlib/java/util/TimeZoneTest.java | 4 ---- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/tz/DateTimeZoneProvider.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/tz/DateTimeZoneProvider.java index 6983d14eb..a4d300a5a 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/impl/tz/DateTimeZoneProvider.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/tz/DateTimeZoneProvider.java @@ -131,7 +131,7 @@ public class DateTimeZoneProvider { if (scoreTable.size() == 1 || scoreTable.get(0).tz.previousTransition(time) == time) { return scoreTable.get(0).tz; - } else if (scoreTable.size() > 1 && scoreTable.get(0).value + 48 * 7 < scoreTable.get(1).value) { + } else if (scoreTable.size() > 1 && scoreTable.get(0).value + 48 * 60 < scoreTable.get(1).value) { return scoreTable.get(0).tz; } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TCalendar.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TCalendar.java index 0ebde76ff..e971d771f 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TCalendar.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TCalendar.java @@ -146,6 +146,10 @@ public abstract class TCalendar implements TSerializable, TCloneable, TComparabl "DAY_OF_MONTH=", "DAY_OF_YEAR=", "DAY_OF_WEEK=", "DAY_OF_WEEK_IN_MONTH=", "AM_PM=", "HOUR=", "HOUR_OF_DAY", "MINUTE=", "SECOND=", "MILLISECOND=", "ZONE_OFFSET=", "DST_OFFSET=" }; + private static int firstDayOfWeekCache = -1; + private static int minimalDaysInFirstWeekCache = -1; + private static TLocale cacheFor; + protected TCalendar() { this(TLocale.getDefault()); } @@ -157,6 +161,7 @@ public abstract class TCalendar implements TSerializable, TCloneable, TComparabl setLenient(true); setFirstDayOfWeek(resolveFirstDayOfWeek(locale)); setMinimalDaysInFirstWeek(resolveMinimalDaysInFirstWeek(locale)); + cacheFor = locale; } private static String resolveCountry(TLocale locale) { @@ -170,15 +175,24 @@ public abstract class TCalendar implements TSerializable, TCloneable, TComparabl } private static int resolveFirstDayOfWeek(TLocale locale) { + if (locale == cacheFor && firstDayOfWeekCache >= 0) { + return firstDayOfWeekCache; + } String country = resolveCountry(locale); ResourceMap dayMap = CLDRHelper.getFirstDayOfWeek(); - return dayMap.has(country) ? dayMap.get(country).getValue() : dayMap.get("001").getValue(); + firstDayOfWeekCache = dayMap.has(country) ? dayMap.get(country).getValue() : dayMap.get("001").getValue(); + return firstDayOfWeekCache; } private static int resolveMinimalDaysInFirstWeek(TLocale locale) { + if (locale == cacheFor && minimalDaysInFirstWeekCache >= 0) { + return minimalDaysInFirstWeekCache; + } String country = resolveCountry(locale); ResourceMap dayMap = CLDRHelper.getMinimalDaysInFirstWeek(); - return dayMap.has(country) ? dayMap.get(country).getValue() : dayMap.get("001").getValue(); + minimalDaysInFirstWeekCache = dayMap.has(country) ? dayMap.get(country).getValue() : + dayMap.get("001").getValue(); + return minimalDaysInFirstWeekCache; } abstract public void add(int field, int value); diff --git a/teavm-core/src/main/resources/org/teavm/javascript/runtime.js b/teavm-core/src/main/resources/org/teavm/javascript/runtime.js index 759869cbb..6d8e90300 100644 --- a/teavm-core/src/main/resources/org/teavm/javascript/runtime.js +++ b/teavm-core/src/main/resources/org/teavm/javascript/runtime.js @@ -598,6 +598,7 @@ Long.prototype.toString = function() { return positive ? result : "-" + result; } var Long_ZERO = new Long(0, 0); +var Long_MAX_NORMAL = 1 << 18; function Long_fromInt(val) { return val >= 0 ? new Long(val, 0) : new Long(val, -1); } @@ -619,6 +620,8 @@ function Long_toNumber(val) { function Long_add(a, b) { if (a.hi === (a.lo >> 31) && b.hi === (b.lo >> 31)) { return Long_fromNumber(a.lo + b.lo); + } else if (Math.abs(a.hi) < Long_MAX_NORMAL && Math.abs(b.hi) < Long_MAX_NORMAL) { + return Long_fromNumber(Long_toNumber(a) + Long_toNumber(b)); } var a_lolo = a.lo & 0xFFFF; var a_lohi = a.lo >>> 16; @@ -729,13 +732,13 @@ function Long_mul(a, b) { return positive ? result : Long_neg(result); } function Long_div(a, b) { - if (a.hi === 0 && b.hi === 0) { + if (Math.abs(a.hi) < Long_MAX_NORMAL && Math.abs(b.hi) < Long_MAX_NORMAL) { return Long_fromNumber(Long_toNumber(a) / Long_toNumber(b)); } return Long_divRem(a, b)[0]; } function Long_rem(a, b) { - if (a.hi === 0 && b.hi === 0) { + if (Math.abs(a.hi) < Long_MAX_NORMAL && Math.abs(b.hi) < Long_MAX_NORMAL) { return Long_fromNumber(Long_toNumber(a) % Long_toNumber(b)); } return Long_divRem(a, b)[1]; diff --git a/teavm-dom/src/main/java/org/teavm/dom/canvas/CanvasGradient.java b/teavm-dom/src/main/java/org/teavm/dom/canvas/CanvasGradient.java index ba48b01d0..94d98933a 100644 --- a/teavm-dom/src/main/java/org/teavm/dom/canvas/CanvasGradient.java +++ b/teavm-dom/src/main/java/org/teavm/dom/canvas/CanvasGradient.java @@ -22,12 +22,12 @@ import org.teavm.jso.JSObject; * @author Alexey Andreev */ public interface CanvasGradient extends JSObject { - /** - * The CanvasGradient.addColorStop() method adds a new stop, defined by an - * offset and a color, to the gradient. If the offset is not between 0 and 1, - * an INDEX_SIZE_ERR is raised, if the color can't be parsed as a CSS color, - * a SYNTAX_ERR is raised. + *

The CanvasGradient.addColorStop() method adds a new stop, defined by an + * offset and a color, to the gradient. If the offset is not between 0 and 1, + * an INDEX_SIZE_ERR is raised, if the color can't be parsed as a CSS color, + * a SYNTAX_ERR is raised.

+ * * @param offset Offset between 0 and 1 * @param color A CSS parseable color. */ diff --git a/teavm-tests/src/test/java/org/teavm/classlib/java/util/TimeZoneTest.java b/teavm-tests/src/test/java/org/teavm/classlib/java/util/TimeZoneTest.java index 878a6b533..298d5a994 100644 --- a/teavm-tests/src/test/java/org/teavm/classlib/java/util/TimeZoneTest.java +++ b/teavm-tests/src/test/java/org/teavm/classlib/java/util/TimeZoneTest.java @@ -24,7 +24,6 @@ import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; import org.junit.Test; -import org.teavm.classlib.impl.tz.DateTimeZoneProvider; /** * @@ -36,9 +35,6 @@ public class TimeZoneTest { @Test public void test_getDefault() { assertNotSame("returns identical", TimeZone.getDefault(), TimeZone.getDefault()); - for (int i = 0; i < 30; ++i) { - DateTimeZoneProvider.detectTimezone(); - } } @Test