Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Commit 708b85c

Browse files
Merge pull request #498 from dart-lang:remove_intl_stream
PiperOrigin-RevId: 495066469
2 parents 234b291 + 2d9eca9 commit 708b85c

File tree

9 files changed

+227
-179
lines changed

9 files changed

+227
-179
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:math';
6+
7+
import 'package:benchmark_harness/benchmark_harness.dart';
8+
9+
const int asciiZeroCodeUnit = 2;
10+
const int listlength = 10000;
11+
12+
String string = String.fromCharCodes(Iterable.generate(
13+
listlength,
14+
(_) => Random().nextInt(80) + 15,
15+
));
16+
17+
/// This class tests the implementation speed of
18+
/// _DateFormatPatternField::nextInteger, which is assumed to be called often and
19+
/// thus being performance-critical.
20+
class NewMethod extends BenchmarkBase {
21+
late String result;
22+
int zeroDigit = 15;
23+
NewMethod() : super('New version of _DateFormatPatternField::nextInteger');
24+
25+
@override
26+
void run() {
27+
var codeUnits = string.codeUnits;
28+
result = String.fromCharCodes(List.generate(
29+
codeUnits.length,
30+
(index) => codeUnits[index] - zeroDigit + asciiZeroCodeUnit,
31+
growable: false,
32+
));
33+
}
34+
}
35+
36+
// THIS WILL BE REMOVED AFTER CR
37+
class OldMethod extends BenchmarkBase {
38+
late String result;
39+
int zeroDigit = 15;
40+
OldMethod() : super('Old version of _DateFormatPatternField::nextInteger');
41+
42+
@override
43+
void setup() => string = String.fromCharCodes(Iterable.generate(
44+
listlength,
45+
(index) => Random().nextInt(80) + zeroDigit,
46+
));
47+
48+
@override
49+
void run() {
50+
var oldDigits = string.codeUnits;
51+
var newDigits = List<int>.filled(string.length, 0);
52+
for (var i = 0; i < string.length; i++) {
53+
newDigits[i] = oldDigits[i] - zeroDigit + asciiZeroCodeUnit;
54+
}
55+
result = String.fromCharCodes(newDigits);
56+
}
57+
}
58+
// THIS WILL BE REMOVED AFTER CR
59+
60+
void main() {
61+
OldMethod().report();
62+
NewMethod().report();
63+
}

lib/src/intl/date_format.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import 'package:meta/meta.dart';
1010
import 'constants.dart' as constants;
1111
import 'date_builder.dart';
1212
import 'date_computation.dart' as date_computation;
13-
import 'intl_stream.dart';
1413
import 'regexp.dart' as regexp;
14+
import 'string_stack.dart';
1515

1616
part 'date_format_field.dart';
1717

@@ -350,11 +350,11 @@ class DateFormat {
350350
DateTime _parseLoose(String inputString, bool utc) {
351351
var dateFields = DateBuilder(locale, dateTimeConstructor);
352352
if (utc) dateFields.utc = true;
353-
var stream = IntlStream(inputString);
353+
var stack = StringStack(inputString);
354354
for (var field in _formatFields) {
355-
field.parseLoose(stream, dateFields);
355+
field.parseLoose(stack, dateFields);
356356
}
357-
if (!stream.atEnd()) {
357+
if (!stack.atEnd) {
358358
throw FormatException(
359359
'Characters remaining after date parsing in $inputString');
360360
}
@@ -379,11 +379,11 @@ class DateFormat {
379379
var dateFields = DateBuilder(locale, dateTimeConstructor);
380380
if (utc) dateFields.utc = true;
381381
dateFields.dateOnly = dateOnly;
382-
var stream = IntlStream(inputString);
382+
var stack = StringStack(inputString);
383383
for (var field in _formatFields) {
384-
field.parse(stream, dateFields);
384+
field.parse(stack, dateFields);
385385
}
386-
if (strict && !stream.atEnd()) {
386+
if (strict && !stack.atEnd) {
387387
throw FormatException(
388388
'Characters remaining after date parsing in $inputString');
389389
}

0 commit comments

Comments
 (0)