我正在使用TatSu python库实现语法。我的语法工作正常,但是有一条规则要花很多时间。在大约3000行的块(较大语法的一部分)上,如果我遵循这条完整规则,则解析整个块大约需要42秒。如果我将此规则简化为几个标记,则运行时间将从42s降至33s(提高了20%)。
规则如下所示,它应与一系列事件相匹配,并用'/'分隔。
import { createStore, applyMiddleware, compose } from 'redux';
import { routerMiddleware } from 'connected-react-router';
import { fromJS } from 'immutable';
import createSagaMiddleware from 'redux-saga';
import createReducer from './reducers'
import sagas from './utils/sagas';
import * as storage from 'redux-storage';
import createEngine from 'redux-storage-engine-localstorage';
const sagaMiddleware = createSagaMiddleware();
export default function configureStore(initialState={}, history){
const engine = createEngine('my-save-key');
const middleware = storage.createMiddleware(engine);
const middlewares = [middleware,sagaMiddleware, routerMiddleware(history)];
const enhancers = [applyMiddleware(...middlewares)];
const composeEnhancers = process.env.NODE_ENV !== 'production'
&& typeof window === 'object'
&& window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
// Prevent recomputing reducers for `replaceReducer`
shouldHotReload: false,
})
: compose;
/* eslint-enable */
const store = createStore(
createReducer(),
fromJS(initialState),
composeEnhancers(...enhancers),
);
// Extensions
sagaMiddleware.run(sagas);
store.injectedReducers = {}; // Reducer registry
store.injectedSagas = {}; // Saga registry
// Make reducers hot reloadable, see http://mxs.is/googmo
/* istanbul ignore next */
if (module.hot) {
module.hot.accept('./reducers', () => {
store.replaceReducer(createReducer(store.injectedReducers));
});
}
const load = storage.createLoader(engine);
load(store)
.then((newState) => console.log('Loaded state:', newState))
.catch((err) => console.log('Failed to load previous state', err));
return store;
}
如果将 event 更改为以下内容,则可以更快地进行解析。
import { reducer as form } from 'redux-form/immutable';
import { combineReducers } from 'redux';
import { connectRouter } from 'connected-react-router';
import history from './utils/history';
import * as storage from 'redux-storage';
//Global Reducers
import authReducer from './modules/reducers/authReducer';
export default function createReducer(injectedReducers={}){
const rootReducer = storage.reducer(combineReducers({
authReducer,
router: connectRouter(history),
...injectedReducers
}))
const mergeWithRouterState = connectRouter(history);
return mergeWithRouterState(rootReducer);
}
那么可以通过某种方式改进上述规则以获得更快的处理速度吗?谢谢你的想法。
答案 0 :(得分:1)
正如您所指出的,对于具有很多仅作为标记的选项的规则,使用模式(正则表达式)会更加有效。
但是运行时最终取决于某些规则如何相互调用。
您可以尝试的一种简单优化方法是添加一个cut(˜
)表达式,这样每个event
最多可以尝试一次(尽管 cut 应该在%
表达式)。
event
=
(
'D' | 'U' | 'Z' | 'P' | 'L' | 'H' | 'x' | 'X' | 'T' | 'V' | 'l' | 'h' | 't' | 'v' | 'N' | 'A' | 'B' | 'F' | '?' | 'G' | 'R' | 'Q' | 'M'
| 'ForceDown' | 'ForceUp' | 'ForceOff' | 'ForcePrior' | 'CompareLow' | 'CompareHigh' | 'CompareUnknown' | 'CompareOff'
| 'CompareValid' | 'CompareLowWindow' | 'CompareHighWindow' | 'CompareOffWindow' | 'CompareValidWindow' | 'ForceUnknown'
| 'LogicLow' | 'LogicHigh' | 'LogicZ' | 'Unknown' | 'ExpectHigh' | 'ExpectLow' | 'ExpectOff' | 'Marker'
) ~
;
这就是说,因为该规则是 lexical 类的大部分,所以我选择正则表达式。
event
=
/(?x)
'D' | 'U' | 'Z' | 'P' | 'L' | 'H' | 'x' | 'X' | 'T' | 'V' | 'l' | 'h' | 't' | 'v' | 'N' | 'A' | 'B' | 'F' | '?' | 'G' | 'R' | 'Q' | 'M'
| 'ForceDown' | 'ForceUp' | 'ForceOff' | 'ForcePrior' | 'CompareLow' | 'CompareHigh' | 'CompareUnknown' | 'CompareOff'
| 'CompareValid' | 'CompareLowWindow' | 'CompareHighWindow' | 'CompareOffWindow' | 'CompareValidWindow' | 'ForceUnknown'
| 'LogicLow' | 'LogicHigh' | 'LogicZ' | 'Unknown' | 'ExpectHigh' | 'ExpectLow' | 'ExpectOff' | 'Marker'
/
;