Skip to content

Commit f765882

Browse files
committed
Sunday's Quick Search.
1 parent ff3676f commit f765882

File tree

3 files changed

+83
-81
lines changed

3 files changed

+83
-81
lines changed

sqlite3/libc/libc.wasm

-4 Bytes
Binary file not shown.

sqlite3/libc/libc.wat

Lines changed: 69 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,7 +2769,7 @@
27692769
(local.get $3)
27702770
)
27712771
)
2772-
(local.set $4
2772+
(local.set $5
27732773
(call $memchr
27742774
(local.get $0)
27752775
(i32.load8_s
@@ -2785,27 +2785,27 @@
27852785
)
27862786
(then
27872787
(return
2788-
(local.get $4)
2788+
(local.get $5)
27892789
)
27902790
)
27912791
)
27922792
(if
27932793
(i32.eqz
2794-
(local.get $4)
2794+
(local.get $5)
27952795
)
27962796
(then
27972797
(return
2798-
(local.get $4)
2798+
(local.get $5)
27992799
)
28002800
)
28012801
)
28022802
(br_if $block
28032803
(i32.lt_u
2804-
(local.tee $8
2804+
(local.tee $7
28052805
(i32.add
28062806
(i32.sub
28072807
(local.get $0)
2808-
(local.get $4)
2808+
(local.get $5)
28092809
)
28102810
(local.get $1)
28112811
)
@@ -2818,7 +2818,7 @@
28182818
(i32.and
28192819
(i32.gt_u
28202820
(i32.sub
2821-
(local.get $8)
2821+
(local.get $7)
28222822
(local.get $3)
28232823
)
28242824
(i32.const 15)
@@ -2832,8 +2832,8 @@
28322832
(then
28332833
(return
28342834
(call $__memmem
2835-
(local.get $4)
2836-
(local.get $8)
2835+
(local.get $5)
2836+
(local.get $7)
28372837
(local.get $2)
28382838
(local.get $3)
28392839
(i32.const 0)
@@ -2845,15 +2845,10 @@
28452845
(i32.const 4112)
28462846
(select
28472847
(i32.const -1)
2848-
(local.tee $7
2849-
(i32.sub
2850-
(local.get $3)
2851-
(i32.const 1)
2852-
)
2853-
)
2848+
(local.get $3)
28542849
(local.tee $0
2855-
(i32.gt_s
2856-
(local.get $7)
2850+
(i32.gt_u
2851+
(local.get $3)
28572852
(i32.const 254)
28582853
)
28592854
)
@@ -2863,44 +2858,40 @@
28632858
(block $block1
28642859
(br_if $block1
28652860
(i32.ge_u
2866-
(local.tee $1
2861+
(local.tee $6
28672862
(select
28682863
(i32.sub
28692864
(local.get $3)
2870-
(i32.const 256)
2865+
(i32.const 255)
28712866
)
28722867
(i32.const 0)
28732868
(local.get $0)
28742869
)
28752870
)
2876-
(local.get $7)
2877-
)
2878-
)
2879-
(local.set $5
2880-
(i32.sub
2881-
(i32.sub
2882-
(local.get $3)
2883-
(local.get $1)
2884-
)
2885-
(i32.const 2)
2871+
(local.get $3)
28862872
)
28872873
)
28882874
(if
2889-
(local.tee $6
2875+
(local.tee $4
28902876
(i32.and
2891-
(i32.add
2892-
(i32.xor
2893-
(local.get $1)
2894-
(i32.const -1)
2895-
)
2877+
(i32.sub
28962878
(local.get $3)
2879+
(local.tee $1
2880+
(local.get $6)
2881+
)
28972882
)
28982883
(i32.const 3)
28992884
)
29002885
)
29012886
(then
29022887
(local.set $0
2903-
(local.get $5)
2888+
(i32.add
2889+
(i32.xor
2890+
(local.get $1)
2891+
(i32.const -1)
2892+
)
2893+
(local.get $3)
2894+
)
29042895
)
29052896
(loop $label
29062897
(i32.store8
@@ -2928,9 +2919,9 @@
29282919
)
29292920
)
29302921
(br_if $label
2931-
(local.tee $6
2922+
(local.tee $4
29322923
(i32.sub
2933-
(local.get $6)
2924+
(local.get $4)
29342925
(i32.const 1)
29352926
)
29362927
)
@@ -2939,118 +2930,124 @@
29392930
)
29402931
)
29412932
(br_if $block1
2942-
(i32.lt_u
2943-
(local.get $5)
2944-
(i32.const 3)
2933+
(i32.gt_u
2934+
(i32.sub
2935+
(local.get $6)
2936+
(local.get $3)
2937+
)
2938+
(i32.const -4)
29452939
)
29462940
)
2947-
(local.set $0
2941+
(local.set $9
29482942
(i32.sub
2949-
(local.get $3)
2943+
(i32.const 0)
29502944
(local.get $1)
29512945
)
29522946
)
2953-
(local.set $5
2947+
(local.set $0
2948+
(local.get $3)
2949+
)
2950+
(local.set $6
29542951
(local.get $2)
29552952
)
29562953
(loop $label1
29572954
(i32.store8
29582955
(i32.add
29592956
(i32.load8_u
2960-
(local.tee $9
2957+
(local.tee $4
29612958
(i32.add
29622959
(local.get $1)
2963-
(local.get $5)
2960+
(local.get $6)
29642961
)
29652962
)
29662963
)
29672964
(i32.const 4112)
29682965
)
29692966
(i32.sub
2970-
(local.get $0)
2971-
(i32.const 2)
2967+
(local.tee $8
2968+
(i32.add
2969+
(local.get $0)
2970+
(local.get $9)
2971+
)
2972+
)
2973+
(i32.const 1)
29722974
)
29732975
)
29742976
(i32.store8
29752977
(i32.add
29762978
(i32.load8_u
29772979
(i32.add
2978-
(local.get $9)
2980+
(local.get $4)
29792981
(i32.const 1)
29802982
)
29812983
)
29822984
(i32.const 4112)
29832985
)
29842986
(i32.sub
2985-
(local.get $0)
2986-
(i32.const 3)
2987+
(local.get $8)
2988+
(i32.const 2)
29872989
)
29882990
)
29892991
(i32.store8
29902992
(i32.add
29912993
(i32.load8_u
29922994
(i32.add
2993-
(local.get $9)
2995+
(local.get $4)
29942996
(i32.const 2)
29952997
)
29962998
)
29972999
(i32.const 4112)
29983000
)
2999-
(local.tee $6
3000-
(i32.sub
3001-
(local.get $0)
3002-
(i32.const 4)
3003-
)
3001+
(i32.sub
3002+
(local.get $8)
3003+
(i32.const 3)
30043004
)
30053005
)
30063006
(i32.store8
30073007
(i32.add
30083008
(i32.load8_u
30093009
(i32.add
3010-
(local.get $9)
3010+
(local.get $4)
30113011
(i32.const 3)
30123012
)
30133013
)
30143014
(i32.const 4112)
30153015
)
30163016
(i32.sub
3017-
(local.get $0)
3018-
(i32.const 5)
3017+
(local.get $8)
3018+
(i32.const 4)
30193019
)
30203020
)
3021-
(local.set $5
3021+
(local.set $6
30223022
(i32.add
3023-
(local.get $5)
3023+
(local.get $6)
30243024
(i32.const 4)
30253025
)
30263026
)
3027-
(local.set $0
3028-
(local.get $6)
3029-
)
30303027
(br_if $label1
30313028
(i32.ne
30323029
(local.get $1)
3033-
(local.tee $7
3030+
(local.tee $0
30343031
(i32.sub
3035-
(local.get $7)
3032+
(local.get $0)
30363033
(i32.const 4)
30373034
)
30383035
)
30393036
)
30403037
)
30413038
)
30423039
)
3043-
(local.set $6
3040+
(local.set $4
30443041
(call $__memmem
3045-
(local.get $4)
3046-
(local.get $8)
3042+
(local.get $5)
3043+
(local.get $7)
30473044
(local.get $2)
30483045
(local.get $3)
30493046
(i32.const 4112)
30503047
)
30513048
)
30523049
)
3053-
(local.get $6)
3050+
(local.get $4)
30543051
)
30553052
(func $__memmem (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (param $4 i32) (result i32)
30563053
(local $5 i32)
@@ -3150,7 +3147,7 @@
31503147
(local.set $15
31513148
(i32.add
31523149
(local.get $3)
3153-
(i32.const 14)
3150+
(i32.const 15)
31543151
)
31553152
)
31563153
(local.set $16

sqlite3/libc/string.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,15 @@ size_t strcspn(const char *s, const char *c) {
460460

461461
// For haystacks of known length and large enough needles,
462462
// Boyer-Moore's bad-character rule may be useful,
463-
// as proposed by Horspool and Raita.
463+
// as proposed by Horspool, Sunday and Raita.
464+
//
465+
// We augment the SIMD algorithm with Quick Search's
466+
// bad-character shift. This does NOT depend on the order
467+
// in which the window matched.
468+
//
464469
// https://www-igm.univ-mlv.fr/~lecroq/string/node14.html
465470
// https://www-igm.univ-mlv.fr/~lecroq/string/node18.html
471+
// https://www-igm.univ-mlv.fr/~lecroq/string/node19.html
466472
// https://www-igm.univ-mlv.fr/~lecroq/string/node22.html
467473

468474
static const char *__memmem(const char *haystk, size_t sh,
@@ -511,9 +517,9 @@ static const char *__memmem(const char *haystk, size_t sh,
511517
// Have we reached the end of the haystack?
512518
if (!wasm_i8x16_all_true(blk_fst)) return NULL;
513519
} else {
514-
// Apply the bad-character rule to the rightmost
515-
// character of the window.
516-
if (bmbc) skip += bmbc[(unsigned char)haystk[sn - 1 + 15]];
520+
// Apply the bad-character rule to the character to the right
521+
// of the righmost character of the search window.
522+
if (bmbc) skip += bmbc[(unsigned char)haystk[sn - 1 + sizeof(v128_t)]];
517523
// Have we reached the end of the haystack?
518524
if (__builtin_sub_overflow(sh, skip, &sh)) return NULL;
519525
// Is the needle longer than the haystack?
@@ -560,23 +566,22 @@ void *memmem(const void *vh, size_t sh, const void *vn, size_t sn) {
560566
// Compute Boyer-Moore's bad-character shift function.
561567
// Only the last 255 characters of the needle matter for shifts up to 255,
562568
// which is good enough for most needles.
563-
size_t n = sn - 1;
569+
size_t c = sn;
564570
size_t i = 0;
565-
int c = n;
566571
if (c >= 255) {
572+
i = sn - 255;
567573
c = 255;
568-
i = n - 255;
569574
}
570575

571576
#ifndef _REENTRANT
572577
static
573578
#endif
574579
uint8_t bmbc[256];
575580
memset(bmbc, c, sizeof(bmbc));
576-
for (; i < n; i++) {
581+
for (; i < sn; i++) {
577582
// One less than the usual offset because
578583
// we advance at least one vector at a time.
579-
size_t t = n - i - 1;
584+
size_t t = sn - i - 1;
580585
bmbc[(unsigned char)needle[i]] = t;
581586
}
582587

0 commit comments

Comments
 (0)