Skip to content

Commit 43ec938

Browse files
addaleaxtargos
authored andcommitted
src: remove static variables from string_search
These variables can as well be stack-allocated. This avoids relying on global state that is not protected by mutexes. Thanks to Stephen Belanger for reviewing this change in its original PR. Refs: ayojs/ayo#82 PR-URL: #20541 Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent f69a823 commit 43ec938

File tree

3 files changed

+9
-41
lines changed

3 files changed

+9
-41
lines changed

node.gyp

-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@
344344
'src/spawn_sync.cc',
345345
'src/string_bytes.cc',
346346
'src/string_decoder.cc',
347-
'src/string_search.cc',
348347
'src/stream_base.cc',
349348
'src/stream_pipe.cc',
350349
'src/stream_wrap.cc',

src/string_search.cc

-11
This file was deleted.

src/string_search.h

+9-29
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ class StringSearchBase {
8080
static const int kBMMinPatternLength = 8;
8181

8282
// Store for the BoyerMoore(Horspool) bad char shift table.
83-
static int kBadCharShiftTable[kUC16AlphabetSize];
83+
int bad_char_shift_table_[kUC16AlphabetSize];
8484
// Store for the BoyerMoore good suffix shift table.
85-
static int kGoodSuffixShiftTable[kBMMaxShift + 1];
85+
int good_suffix_shift_table_[kBMMaxShift + 1];
8686
// Table used temporarily while building the BoyerMoore good suffix
8787
// shift table.
88-
static int kSuffixTable[kBMMaxShift + 1];
88+
int suffix_table_[kBMMaxShift + 1];
8989
};
9090

9191
template <typename Char>
@@ -152,26 +152,6 @@ class StringSearch : private StringSearchBase {
152152
return bad_char_occurrence[equiv_class];
153153
}
154154

155-
// Store for the BoyerMoore(Horspool) bad char shift table.
156-
// Return a table covering the last kBMMaxShift+1 positions of
157-
// pattern.
158-
int* bad_char_table() { return kBadCharShiftTable; }
159-
160-
// Store for the BoyerMoore good suffix shift table.
161-
int* good_suffix_shift_table() {
162-
// Return biased pointer that maps the range [start_..pattern_.length()
163-
// to the kGoodSuffixShiftTable array.
164-
return kGoodSuffixShiftTable - start_;
165-
}
166-
167-
// Table used temporarily while building the BoyerMoore good suffix
168-
// shift table.
169-
int* suffix_table() {
170-
// Return biased pointer that maps the range [start_..pattern_.length()
171-
// to the kSuffixTable array.
172-
return kSuffixTable - start_;
173-
}
174-
175155
// The pattern to search for.
176156
Vector pattern_;
177157
// Pointer to implementation of the search.
@@ -345,8 +325,8 @@ size_t StringSearch<Char>::BoyerMooreSearch(
345325
// Only preprocess at most kBMMaxShift last characters of pattern.
346326
size_t start = start_;
347327

348-
int* bad_char_occurrence = bad_char_table();
349-
int* good_suffix_shift = good_suffix_shift_table();
328+
int* bad_char_occurrence = bad_char_shift_table_;
329+
int* good_suffix_shift = good_suffix_shift_table_ - start_;
350330

351331
Char last_char = pattern_[pattern_length - 1];
352332
size_t index = start_index;
@@ -397,8 +377,8 @@ void StringSearch<Char>::PopulateBoyerMooreTable() {
397377

398378
// Biased tables so that we can use pattern indices as table indices,
399379
// even if we only cover the part of the pattern from offset start.
400-
int* shift_table = good_suffix_shift_table();
401-
int* suffix_table = this->suffix_table();
380+
int* shift_table = good_suffix_shift_table_ - start_;
381+
int* suffix_table = suffix_table_ - start_;
402382

403383
// Initialize table.
404384
for (size_t i = start; i < pattern_length; i++) {
@@ -462,7 +442,7 @@ size_t StringSearch<Char>::BoyerMooreHorspoolSearch(
462442
size_t start_index) {
463443
const size_t subject_length = subject.length();
464444
const size_t pattern_length = pattern_.length();
465-
int* char_occurrences = bad_char_table();
445+
int* char_occurrences = bad_char_shift_table_;
466446
int64_t badness = -pattern_length;
467447

468448
// How bad we are doing without a good-suffix table.
@@ -511,7 +491,7 @@ template <typename Char>
511491
void StringSearch<Char>::PopulateBoyerMooreHorspoolTable() {
512492
const size_t pattern_length = pattern_.length();
513493

514-
int* bad_char_occurrence = bad_char_table();
494+
int* bad_char_occurrence = bad_char_shift_table_;
515495

516496
// Only preprocess at most kBMMaxShift last characters of pattern.
517497
const size_t start = start_;

0 commit comments

Comments
 (0)