Skip to content

Commit

Permalink
Disable bounds check in critical character parsers.
Browse files Browse the repository at this point in the history
  • Loading branch information
renggli committed Jan 4, 2025
1 parent 1a625cb commit eeef1ec
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/src/parser/character/predicates/lookup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class LookupCharPredicate extends CharacterPredicate {

@inlineJs
@inlineVm
@noBoundsChecksVm
@noBoundsChecksJs
bool _testBit(int value) =>
(bits[value >> _shift] & _mask[value & _offset]) != 0;

Expand Down
3 changes: 3 additions & 0 deletions lib/src/parser/character/predicates/ranges.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:typed_data';

import 'package:collection/collection.dart' show ListEquality;

import '../../../shared/annotations.dart';
import '../predicate.dart';
import 'range.dart';

Expand All @@ -21,6 +22,8 @@ class RangesCharPredicate extends CharacterPredicate {
final Uint32List ranges;

@override
@noBoundsChecksVm
@noBoundsChecksJs
bool test(int value) {
var min = 0;
var max = ranges.length - 2;
Expand Down
9 changes: 9 additions & 0 deletions lib/src/parser/predicate/single_character.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:meta/meta.dart';

import '../../core/context.dart';
import '../../core/result.dart';
import '../../shared/annotations.dart';
import '../character/predicate.dart';
import '../character/predicates/constant.dart';
import 'character.dart';
Expand All @@ -27,6 +28,8 @@ class SingleCharacterParser extends CharacterParser {
: super.internal();

@override
@noBoundsChecksVm
@noBoundsChecksJs
Result<String> parseOn(Context context) {
final buffer = context.buffer;
final position = context.position;
Expand All @@ -37,6 +40,8 @@ class SingleCharacterParser extends CharacterParser {
}

@override
@noBoundsChecksVm
@noBoundsChecksJs
int fastParseOn(String buffer, int position) =>
position < buffer.length && predicate.test(buffer.codeUnitAt(position))
? position + 1
Expand All @@ -55,6 +60,8 @@ class AnySingleCharacterParser extends SingleCharacterParser {
super.internal();

@override
@noBoundsChecksVm
@noBoundsChecksJs
Result<String> parseOn(Context context) {
final buffer = context.buffer;
final position = context.position;
Expand All @@ -64,6 +71,8 @@ class AnySingleCharacterParser extends SingleCharacterParser {
}

@override
@noBoundsChecksVm
@noBoundsChecksJs
int fastParseOn(String buffer, int position) =>
position < buffer.length ? position + 1 : -1;
}
8 changes: 8 additions & 0 deletions lib/src/parser/predicate/unicode_character.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class UnicodeCharacterParser extends CharacterParser {
: super.internal();

@override
@noBoundsChecksVm
@noBoundsChecksJs
Result<String> parseOn(Context context) {
final buffer = context.buffer;
final position = context.position;
Expand All @@ -51,6 +53,8 @@ class UnicodeCharacterParser extends CharacterParser {
}

@override
@noBoundsChecksVm
@noBoundsChecksJs
int fastParseOn(String buffer, int position) {
if (position < buffer.length) {
var codeUnit = buffer.codeUnitAt(position++);
Expand Down Expand Up @@ -81,6 +85,8 @@ class AnyUnicodeCharacterParser extends UnicodeCharacterParser {
super.internal();

@override
@noBoundsChecksVm
@noBoundsChecksJs
Result<String> parseOn(Context context) {
final buffer = context.buffer;
final position = context.position;
Expand All @@ -98,6 +104,8 @@ class AnyUnicodeCharacterParser extends UnicodeCharacterParser {
}

@override
@noBoundsChecksVm
@noBoundsChecksJs
int fastParseOn(String buffer, int position) {
if (position < buffer.length) {
if (_isLeadSurrogate(buffer.codeUnitAt(position++)) &&
Expand Down
3 changes: 3 additions & 0 deletions lib/src/shared/annotations.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
const inlineVm = pragma('vm:prefer-inline');
const inlineJs = pragma('dart2js:tryInline');

const noBoundsChecksVm = pragma('vm:unsafe:no-bounds-checks');
const noBoundsChecksJs = pragma('dart2js:index-bounds:trust');

0 comments on commit eeef1ec

Please sign in to comment.