Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[Date] Implement a new DateFormatter.format_experimental method. It u…

…ses Parse and some optimizations to try and do formatting as quickly as possible. Short format strings see a 25-33% speed increase (roughly). Larger strings see a dramatic slowdown for reasons I have not yet determined.
  • Loading branch information...
commit 90756d788e652bb5d1774cc481233b47a49099fc 1 parent 3d4846b
Andrew Whitworth authored
7 src/core/Hash.winxed
View
@@ -22,4 +22,11 @@ namespace Rosella
h.set_value_type(DATATYPE_STRING);
return h;
}
+
+ function get_integer_string_hash()
+ {
+ var h = int_keyed_hash();
+ h.set_value_type(DATATYPE_STRING);
+ return h;
+ }
}
78 src/date/DateFormatter.winxed
View
@@ -1,3 +1,23 @@
+inline make_key(int c, int n) return int
+{
+ return c << 2 | n;
+}
+
+inline push_2digit_number(var sb, int i)
+{
+ push(sb, int(i / 10) + ASCII_0);
+ push(sb, int(i % 10) + ASCII_0);
+}
+
+inline push_4digit_number(var sb, int i)
+{
+
+ push(sb, int(i / 1000) + ASCII_0);
+ push(sb, (int(i / 100) % 10) + ASCII_0);
+ push(sb, int(i / 10) + ASCII_0);
+ push(sb, int(i % 10) + ASCII_0);
+}
+
/* Date String Formatter
Formats a Date object into a string according to the given formatting string
*/
@@ -43,83 +63,91 @@ class Rosella.Date.DateFormatter : Rosella.StringFormatter
return f;
}
- function format_fast(var obj, string f)
+ function format_experimental(var obj, string f)
{
- var sb = new 'StringBuilder';
+ int flen = length(f);
+ var sb;
+ ${ new sb, 'StringBuilder', flen };
+ int iyear = obj.year();
+ int imonth = obj.month();
+ int iday = obj.day();
+ int ihour = obj.hours();
+ int iminute = obj.minutes();
+ int isecond = obj.seconds();
+
:(f, var s, var b, int len) = Rosella.Parse.setup_parse(f);
- while(have_more_chars(s, b)) {
+ while(true) {
int c = get_next(s, b);
+ if (c == ASCII_NULL)
+ break;
if (!codepoint_is_alpha(c)) {
- push(sb, chr(c));
+ add_char_to_builder(sb, c);
continue;
}
int num = Rosella.Parse.parse_repeat(c, f, s, b, len);
+
string sval;
int ival;
switch(c) {
case ASCII_y:
- ival = obj.year();
if (num == 4) {
- push(sb, format_string("%04d", ival));
+ push_4digit_number(sb, iyear);
break;
}
self.__format_error(c, num);
case ASCII_M:
- if (num == 4) {
+ if (num == 2) {
+ push_2digit_number(sb, imonth);
+ break;
+ } else if (num == 4) {
sval = obj.month_name();
push(sb, sval);
break;
} else if (num == 3) {
sval = obj.month_name();
- push(sb, substr(sval, 0, 3));
- break;
- } else if (num == 2) {
- ival = obj.month();
- push(sb, format_string("%02d", ival));
+ sval = substr(sval, 0, 3);
+ push(sb, sval);
break;
}
self.__format_error(c, num);
case ASCII_d:
if (num == 2) {
- ival = obj.day();
- push(sb, format_string("%02d", ival));
+ push_2digit_number(sb, iday);
break;
}
self.__format_error(c, num);
case ASCII_H:
if (num == 2) {
- ival = obj.hours();
+ ival = ihour;
if (ival > 12)
ival = ival - 12;
- push(sb, format_string("%02d", ival));
+ push_2digit_number(sb, ival);
break;
}
self.__format_error(c, num);
case ASCII_h:
if (num == 2) {
- ival = obj.hours();
- push(sb, format_string("%02d", ival));
+ push_2digit_number(sb, ihour);
break;
}
self.__format_error(c, num);
case ASCII_m:
if (num == 2) {
- ival = obj.minutes();
- push(sb, format_string("%02d", ival));
+ push_2digit_number(sb, iminute);
break;
}
self.__format_error(c, num);
case ASCII_s:
if (num == 2) {
- ival = obj.seconds();
- push(sb, format_string("%02d", ival));
+ push_2digit_number(sb, isecond);
break;
}
self.__format_error(c, num);
case ASCII_w:
if (num == 2) {
ival = obj.week_day();
- push(sb, format_string("%02d", ival));
+ sval = format_string("%02d", ival);
+ push(sb, sval);
break;
}
self.__format_error(c, num);
@@ -133,7 +161,8 @@ class Rosella.Date.DateFormatter : Rosella.StringFormatter
case ASCII_A:
if (num == 2) {
ival = obj.hours();
- push(sb, ival > 12 ? "PM" : "AM");
+ sval = ival > 12 ? "PM" : "AM";
+ push(sb, sval);
break;
}
self.__format_error(c, num);
@@ -141,6 +170,7 @@ class Rosella.Date.DateFormatter : Rosella.StringFormatter
self.__format_error(c, num);
}
}
+ return sb;
}
function __format_error(int c, int num)
6 src/include/Parse_builtins.winxed
View
@@ -42,3 +42,9 @@ inline have_more_chars(var s, var b) return int
return true;
return false;
}
+
+inline add_char_to_builder(var sb, int c)
+{
+ // push(sb, c);
+ push(sb, chr(c));
+}
39 src/unstable/parse/Parse.winxed
View
@@ -27,7 +27,7 @@ namespace Rosella.Parse
break;
if (c == ASCII_BACKSLASH)
c = get_next(s, b);
- push(sb, chr(c));
+ add_char_to_builder(sb, c);
}
return sb;
}
@@ -44,39 +44,38 @@ namespace Rosella.Parse
c = get_next(s, b);
switch (c) {
case ASCII_b:
- push(sb, chr(ASCII_BACKSPACE));
+ add_char_to_builder(sb, ASCII_BACKSPACE);
break;
case ASCII_f:
- push(sb, chr(ASCII_LINE_FEED));
+ add_char_to_builder(sb, ASCII_LINE_FEED);
break;
case ASCII_n:
push(sb, "\n");
break;
case ASCII_r:
- push(sb, chr(ASCII_CARRIAGE_RETURN));
+ add_char_to_builder(sb, ASCII_CARRIAGE_RETURN);
break;
case ASCII_t:
- push(sb, "\t");
+ add_char_to_builder(sb, ASCII_TAB);
break;
case ASCII_u:
case ASCII_x:
int x = parse_hexadecimal(_str, s, b, len, 4);
- push(sb, chr(x));
+ add_char_to_builder(sb, x);
break;
case ASCII_o:
int o = parse_octal(_str, s, b, len, 3);
- push(sb, chr(o));
+ add_char_to_builder(sb, o);
break;
default:
- push(sb, chr(c));
+ add_char_to_builder(sb, c);
break;
-
}
continue;
}
if (c == q)
break;
- push(sb, chr(c));
+ add_char_to_builder(sb, c);
}
return sb;
}
@@ -92,7 +91,7 @@ namespace Rosella.Parse
unshift_int(s, c);
break;
}
- push(sb, chr(c));
+ add_char_to_builder(sb, c);
}
string result = string(sb);
return result;
@@ -108,7 +107,7 @@ namespace Rosella.Parse
unshift_int(s, c);
break;
}
- push(sb, chr(c));
+ add_char_to_builder(sb, c);
}
string result = string(sb);
return result;
@@ -121,25 +120,25 @@ namespace Rosella.Parse
int have_dot = false;
int have_sign = false;
var sb = new 'StringBuilder';
- push(sb, codepoint_to_string(c));
+ add_char_to_builder(sb, c);
while (have_more_chars(s, b)) {
int d = get_next(s, b);
if (!have_sign && (d == ASCII_PLUS|| d == ASCII_DASH)) {
have_sign = true;
- push(sb, codepoint_to_string(d));
+ add_char_to_builder(sb, d);
}
else if (codepoint_is_digit(d)) {
- push(sb, codepoint_to_string(d));
+ add_char_to_builder(sb, d);
continue;
}
else if (!have_e && (d == ASCII_e || d == ASCII_E)) {
have_e = true;
have_sign = false;
- push(sb, 'e');
+ add_char_to_builder(sb, ASCII_e);
}
else if (!have_e && !have_dot && d == ASCII_PERIOD) {
have_dot = true;
- push(sb, '.');
+ add_char_to_builder(sb, ASCII_PERIOD);
}
else {
unshift_int(s, d);
@@ -236,7 +235,7 @@ namespace Rosella.Parse
break;
if (c == ASCII_NULL)
break;
- push(sb, chr(c));
+ add_char_to_builder(sb, c);
}
return sb, c;
}
@@ -254,7 +253,7 @@ namespace Rosella.Parse
break;
if (c == ASCII_NULL)
break;
- push(sb, chr(c));
+ add_char_to_builder(sb, c);
}
return sb, c;
}
@@ -269,7 +268,7 @@ namespace Rosella.Parse
int c = get_next(s, b);
if (c == ASCII_NULL)
break;
- push(sb, chr(c));
+ add_char_to_builder(sb, c);
}
return sb;
}
Please sign in to comment.
Something went wrong with that request. Please try again.