0

I need to write a program that will measure the execution time of various functions. Functions must be entered by the user. I looked at existing libraries for parsing mathematical formulas (expressions, function_tree, math_expressions), but they are not suitable because the execution time of the resulting formulas is ten times higher than the execution of the compiled function, so I decided to insert formulas into the code and execute it. At first, I implemented it via Isolate.spawnUri(), but it turned out that this does not work in aot-snapshot (the program must be compiled into an executable file). Then I came across the dart_eval library, but when executing this code:

final source = '''
import 'dart:core';

double f(double x) {
  return x * x;
}

int main(List<String> args, int count) {
  var start = DateTime.now();
  for (double i = 0; i < count; i++) {
    f(i);
  }
  return start.difference(DateTime.now()).inMicroseconds;
}
''';
  final compiler = Compiler();
  print('compiling');
  final program = compiler.compile({
    'default': {
      'main.dart': source,
    },
  });
  print('compiled');
  final runtime = Runtime.ofProgram(program);
  runtime.setup();
  print('setup');
  print(runtime.executeLib('package:default/main.dart', 'main'));

the program crashes with this error:

Null check operator used on a null value
#0      _invokeWithTarget (package:dart_eval/src/eval/compiler/expression/method_invocation.dart:158:45)
#1      compileMethodInvocation (package:dart_eval/src/eval/compiler/expression/method_invocation.dart:28:12)
#2      compileExpression (package:dart_eval/src/eval/compiler/expression/expression.dart:31:12)
#3      compilePropertyAccess (package:dart_eval/src/eval/compiler/expression/property_access.dart:8:13)
#4      compileExpression (package:dart_eval/src/eval/compiler/expression/expression.dart:37:12)
#5      compileReturn (package:dart_eval/src/eval/compiler/statement/return.dart:43:17)
#6      compileStatement (package:dart_eval/src/eval/compiler/statement/statement.dart:24:12)
#7      compileBlock (package:dart_eval/src/eval/compiler/statement/block.dart:16:20)
#8      compileFunctionDeclaration (package:dart_eval/src/eval/compiler/declaration/function.dart:51:14)
#9      compileDeclaration (package:dart_eval/src/eval/compiler/declaration/declaration.dart:20:5)
#10     Compiler.compileSources.<anonymous closure>.<anonymous closure> (package:dart_eval/src/eval/compiler/compiler.dart:339:11)
#11     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:617:13)
#12     Compiler.compileSources.<anonymous closure> (package:dart_eval/src/eval/compiler/compiler.dart:328:15)
#13     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:617:13)
#14     Compiler.compileSources (package:dart_eval/src/eval/compiler/compiler.dart:325:32)
#15     Compiler.compile (package:dart_eval/src/eval/compiler/compiler.dart:131:12)
#16     main (package:rand_gen/main.dart:31:28)
#17     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#18     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

The same error appears when using Stopwatch. Importing dart:core does not fix the problem

  • Hi, I'm the author of dart_eval. The DateTime.difference() and the Duration.inMicroseconds are not supported yet. You can file a bug on the dart_eval github for this. In the meantime, you can use DateTime millisecondsSinceEpoch() which is supported to accomplish your goal. – qualverse Feb 09 '23 at 21:09

0 Answers0