OILS / test / bug-2123.ysh View on Github | oilshell.org

102 lines, 66 significant
1# Run with _bin/cxx-opt/ysh
2
3proc filter (; predicate) {
4 for line in (io.stdin) {
5 if (io->evalExpr(predicate, vars={_val: fromJson8(line)})) {
6 write -- $line
7 }
8 }
9}
10
11proc filter-continue (; predicate) {
12 for line in (io.stdin) {
13 # BUG FIX: we should GC before 'continue' raises an exception
14 if (not io->evalExpr(predicate, vars={_val: fromJson8(line)})) {
15 continue
16 }
17 }
18}
19
20#
21# Setup
22#
23
24var f = '_tmp/b1'
25
26# Using YSH numbers
27var lines = ${1:-100_000}
28
29for i in (0 ..= lines) {
30
31 json write ({}, space=0)
32} > $f
33
34#
35# Benchmarks
36#
37
38var N = 2
39
40proc with-default {
41 write -- u'get() with default\n'
42 write -- 'BEFORE ------------------------------------------------------'
43
44 for _ in (0 ..< N) {
45 time for line in (io.stdin) {
46 call fromJson8(line)
47 } < $f
48 write
49 }
50
51 echo 'FILTER with default'
52 time filter [get(_val, 'missing-key', 0) === 0] < $f >/dev/null
53
54 write -- 'AFTER -------------------------------------------------------'
55
56 for _ in (0 ..< N) {
57 time for line in (io.stdin) {
58 call fromJson8(line)
59 } < $f
60 write
61 }
62}
63
64proc without-default {
65 write -- u'get() without default\n'
66 write -- 'BEFORE ------------------------------------------------------'
67
68 for _ in (0 ..< N) {
69 time for line in (io.stdin) {
70 call fromJson8(line)
71 } < $f
72 write
73 }
74
75 echo 'FILTER without default'
76
77 for line in (io.stdin) {
78 var d = fromJson8(line)
79 call get(d, 'missing-key')
80 } < $f
81
82 #time filter [get(_val, 'missing-key', 0) === 0] < $f >/dev/null
83
84 echo '****** BEGIN filter'
85 #time filter [get(_val, 'missing-key')] < $f #>/dev/null
86 time filter-continue [get(_val, 'missing-key')] < $f #>/dev/null
87 echo '****** END filter'
88
89 write -- 'AFTER -------------------------------------------------------'
90
91 for _ in (0 ..< N) {
92 time for line in (io.stdin) {
93 call fromJson8(line)
94 } < $f
95 write
96 }
97}
98
99
100with-default
101without-default
102