Show
Ignore:
Timestamp:
02/18/07 15:56:10 (21 months ago)
Author:
audreyt
Message:

* Convert VStr from String to ByteString?, and Syn from

String to ID, in Pugs. Benchmark shows the perf gain is 5%,
so it's not worth the trouble of writing a GHC 6.7-compatible
processor. The next commit will revert this commit.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Prim/Match.hs

    r15165 r15296  
    1 {-# OPTIONS_GHC -fglasgow-exts -fno-warn-orphans -fallow-overlapping-instances #-} 
     1{-# OPTIONS_GHC -fglasgow-exts -fno-warn-orphans -fallow-overlapping-instances -foverloaded-strings #-} 
    22 
    33module Pugs.Prim.Match ( 
     
    1616-- XXX - kluge: before we figure out the parrot calling convention, 
    1717--       we'll simply inline the adverbs into the regex. 
    18 ruleWithAdverbs :: VRule -> Eval VStr 
     18ruleWithAdverbs :: VRule -> Eval String 
    1919ruleWithAdverbs MkRulePGE{ rxRule = re, rxAdverbs = advs } = do 
    2020    when (null re) $ 
     
    2626            VBool False -> return "0" 
    2727            _           -> fromVal v 
    28         return $ \x -> ":" ++ k ++ "(" ++ str ++ ")[" ++ x ++ "]" 
    29     return $ combine advs re 
     28        return $ \x -> ":" ++ cast k ++ "(" ++ str ++ ")[" ++ x ++ "]" 
     29    return $ combine advs (cast re) 
    3030ruleWithAdverbs _ = fail "PCRE regexes can't be compiled to PGE regexes" 
    3131 
     
    5555    rv  <- tryIO Nothing $ fmap Just (readIO $ decodeUTF8 pge) 
    5656    let matchToVal PGE_Fail = VMatch mkMatchFail 
    57         matchToVal (PGE_String str) = VStr str 
     57        matchToVal (PGE_String str) = _VStr str 
    5858        matchToVal (PGE_Array ms) = VList (map matchToVal ms) 
    5959        matchToVal (PGE_Match from to pos named) = VMatch $ 
    60             mkMatchOk from to substr pos' named' 
     60            mkMatchOk from to (cast substr) pos' named' 
    6161            where 
    6262            substr  = genericTake (to - from) (genericDrop from cs) 
     
    7777            VMatch $ if fBytes == -1 then mkMatchFail else mkMatchOk 
    7878                fChars (fChars + lChars) 
    79                 (substr csChars fChars lChars) 
     79                (cast $ substr csChars fChars lChars) 
    8080                [] Map.empty 
    8181            | (fBytes, lBytes) <- subs 
     
    8787        chars = genericLength . decodeUTF8 
    8888 
    89     return $ mkMatchOk fromChars (fromChars + lenChars) (substr csChars fromChars lenChars) subsMatch Map.empty 
     89    return $ mkMatchOk fromChars (fromChars + lenChars) (cast $ substr csChars fromChars lenChars) subsMatch Map.empty 
    9090    where 
    9191    csBytes = encodeUTF8 csChars 
    9292 
    9393matchFromMR :: MatchResult Char -> Val 
    94 matchFromMR mr = VMatch $ mkMatchOk 0 0 (decodeUTF8 all) subsMatch Map.empty 
     94matchFromMR mr = VMatch $ mkMatchOk 0 0 (cast $ decodeUTF8 all) subsMatch Map.empty 
    9595    where 
    9696    (all:subs) = elems $ mrSubs mr 
    97     subsMatch = [ VMatch $ mkMatchOk 0 0 (decodeUTF8 sub) [] Map.empty | sub <- subs ] 
     97    subsMatch = [ VMatch $ mkMatchOk 0 0 (cast $ decodeUTF8 sub) [] Map.empty | sub <- subs ] 
    9898 
    9999-- Used in op2Match 
     
    108108op2Match :: Val -> Val -> Eval Val 
    109109 
    110 op2Match x y@(VCode _) = do 
     110op2Match x y@VCode{} = do 
    111111    (arity :: Int) <- fromVal =<< op1CodeArity y 
    112112    res <- fromVal =<< case arity of 
    113113        0 -> do 
    114             writeVar (cast "$*_") x 
     114            writeVar (_cast "$*_") x 
    115115            evalExp $ App (Val y) Nothing [] 
    116116        1 -> do 
    117117            evalExp $ App (Val y) Nothing [Val x] 
    118         _ -> fail ("Unexpected arity in smart match: " ++ (show arity)) 
     118        _ -> fail ("Unexpected arity in smart match: " ++ show arity) 
    119119    return $ VBool $ res 
    120120 
     
    127127    (k, v)  <- pair_fetch pv 
    128128    isTrue  <- fromVal v :: Eval Bool 
    129     testOp  <- fromVal k 
     129    testOp  <- fromVal k :: Eval VStr 
    130130    file    <- fromVal x 
    131131    rv      <- ($ file) $ case testOp of 
     
    178178    if cnt == 0 then return (VBool False) else do 
    179179    ref     <- fromVal x 
    180     writeRef ref $ VStr str' 
     180    writeRef ref $ _VStr str' 
    181181    return $ castV cnt 
    182182    where 
     
    186186        if not (matchOk match) then return (str, ok) else do 
    187187        glob    <- askGlobal 
    188         matchSV <- findSymRef (cast "$/") glob 
     188        matchSV <- findSymRef varMatch glob 
    189189        writeRef matchSV (VMatch match) 
    190190        str'    <- fromVal =<< evalExp subst 
     
    205205    if not (matchOk match) then return (VBool False) else do 
    206206    glob    <- askGlobal 
    207     matchSV <- findSymRef (cast "$/") glob 
     207    matchSV <- findSymRef varMatch glob 
    208208    writeRef matchSV (VMatch match) 
    209209    str'    <- fromVal =<< evalExp subst 
    210     writeRef ref . VStr $ concat 
     210    writeRef ref . _VStr $ concat 
    211211        [ genericTake (matchFrom match) str 
    212212        , str' 
     
    252252    match   <- str `doMatch` rx 
    253253    glob    <- askGlobal 
    254     matchSV <- findSymRef (cast "$/") glob 
     254    matchSV <- findSymRef varMatch glob 
    255255    writeRef matchSV (VMatch match) 
    256256    ifListContext 
     
    290290rxSplit rx str = do 
    291291    match <- str `doMatch` rx 
    292     if not (matchOk match) then return [VStr str] else do 
     292    if not (matchOk match) then return [_VStr str] else do 
    293293    if matchFrom match == matchTo match 
    294294        then do 
    295295            let (c:cs) = str 
    296296            rest <- rxSplit rx cs 
    297             return (VStr [c]:rest) 
     297            return (_VStr [c]:rest) 
    298298        else do 
    299299            let before = genericTake (matchFrom match) str 
    300300                after  = genericDrop (matchTo match) str 
    301301            rest <- rxSplit rx after 
    302             return $ (VStr before:matchSubPos match) ++ rest 
     302            return $ (_VStr before:matchSubPos match) ++ rest 
    303303 
    304304-- duplicated for now, pending über-Haskell-fu 
     
    308308rxSplit_n rx str n = do 
    309309    match <- str `doMatch` rx 
    310     if or [ ( n == 1 ), ( not (matchOk match) ) ] then return [VStr str] else do 
     310    if or [ ( n == 1 ), ( not (matchOk match) ) ] then return [_VStr str] else do 
    311311    if matchFrom match == matchTo match 
    312312        then do 
    313313            let (c:cs) = str 
    314314            rest <- rxSplit_n rx (cs) (n-1) 
    315             return (VStr [c]:rest) 
     315            return (_VStr [c]:rest) 
    316316        else do 
    317317            let before = genericTake (matchFrom match) str 
    318318                after  = genericDrop (matchTo match) str 
    319319            rest <- rxSplit_n rx after (n-1) 
    320             return $ (VStr before:matchSubPos match) ++ rest 
    321  
    322 pkgParents :: VStr -> Eval [VStr] 
     320            return $ (_VStr before:matchSubPos match) ++ rest 
     321 
     322pkgParents :: String -> Eval [String] 
    323323pkgParents pkg = do 
    324324    ref     <- readVar $ cast (':':'*':pkg) 
     
    332332 
    333333-- XXX - copy and paste code; merge with above! 
    334 pkgParentClasses :: VStr -> Eval [VStr] 
     334pkgParentClasses :: String -> Eval [String] 
    335335pkgParentClasses pkg = do 
    336336    ref     <- readVar $ cast (':':'*':pkg) 
     
    341341    pkgs    <- mapM pkgParentClasses attrs 
    342342    return $ nub (pkg:concat pkgs) 
     343 
     344-- | the match variable $/ 
     345varMatch :: Var 
     346varMatch = _cast "$/"