Mercurial > urweb
comparison src/sqlcache.sml @ 2300:57f6473b1469
More work on heuristics.
author | Ziv Scully <ziv@mit.edu> |
---|---|
date | Thu, 19 Nov 2015 18:13:01 -0500 |
parents | 47d5c94aeeb8 |
children | 8d772fbf59c1 |
comparison
equal
deleted
inserted
replaced
2299:47d5c94aeeb8 | 2300:57f6473b1469 |
---|---|
91 | 91 |
92 val cacheRef = ref LruCache.cache | 92 val cacheRef = ref LruCache.cache |
93 fun setCache c = cacheRef := c | 93 fun setCache c = cacheRef := c |
94 fun getCache () = !cacheRef | 94 fun getCache () = !cacheRef |
95 | 95 |
96 datatype heuristic = Always | Never | NoPureAll | NoPureOne | NoCombo | 96 datatype heuristic = SmartEq (* | SmartSub *) | Always | Never | NoPureAll | NoPureOne | NoCombo |
97 | 97 |
98 val heuristicRef = ref Always | 98 val heuristicRef = ref Always |
99 fun setHeuristic h = heuristicRef := (case h of | 99 fun setHeuristic h = heuristicRef := (case h of |
100 "always" => Always | 100 "smarteq" => SmartEq |
101 (* | "smartsub" => SmartSub *) | |
102 | "always" => Always | |
101 | "never" => Never | 103 | "never" => Never |
102 | "nopureall" => NoPureAll | 104 | "nopureall" => NoPureAll |
103 | "nopureone" => NoPureOne | 105 | "nopureone" => NoPureOne |
104 | "nocombo" => NoCombo | 106 | "nocombo" => NoCombo |
105 | _ => raise Fail "Sqlcache: setHeuristic") | 107 | _ => raise Fail "Sqlcache: setHeuristic") |
611 IM.empty | 613 IM.empty |
612 (varsOfQuery q))] | 614 (varsOfQuery q))] |
613 | 615 |
614 val union = op@ | 616 val union = op@ |
615 | 617 |
618 fun addToSqlArgsMap ((q, subst), acc) = | |
619 IM.foldl (fn (arg, acc) => AM.insert (acc, arg, ())) acc subst | |
620 | |
616 fun sqlArgsMap (qs : t) = | 621 fun sqlArgsMap (qs : t) = |
617 let | 622 let |
618 val args = | 623 val args = |
619 List.foldl (fn ((q, subst), acc) => | 624 List.foldl addToSqlArgsMap AM.empty qs |
620 IM.foldl (fn (arg, acc) => AM.insert (acc, arg, ())) acc subst) | |
621 AM.empty | |
622 qs | |
623 val countRef = ref (~1) | 625 val countRef = ref (~1) |
624 fun count () = (countRef := !countRef + 1; !countRef) | 626 fun count () = (countRef := !countRef + 1; !countRef) |
625 in | 627 in |
626 (* Maps each arg to a different consecutive integer, starting from 0. *) | 628 (* Maps each arg to a different consecutive integer, starting from 0. *) |
627 AM.map count args | 629 AM.map count args |
645 val invalPaths = List.foldl PS.union PS.empty (map freePaths args) | 647 val invalPaths = List.foldl PS.union PS.empty (map freePaths args) |
646 (* TODO: make sure these variables are okay to remove from the argument list. *) | 648 (* TODO: make sure these variables are okay to remove from the argument list. *) |
647 val pureArgs = PS.difference (paths, invalPaths) | 649 val pureArgs = PS.difference (paths, invalPaths) |
648 val shouldCache = | 650 val shouldCache = |
649 case getHeuristic () of | 651 case getHeuristic () of |
650 Always => true | 652 SmartEq => |
651 | Never => (case qs of [_] => true | _ => false) | 653 (case (qs, PS.numItems pureArgs) of |
654 ((q::qs), 0) => | |
655 let | |
656 val m = addToSqlArgsMap (q, AM.empty) | |
657 val ms = map (fn q => addToSqlArgsMap (q, AM.empty)) qs | |
658 fun test (m, acc) = | |
659 acc | |
660 <\obind\> | |
661 (fn m' => | |
662 let | |
663 val mm = AM.unionWith #1 (m, m') | |
664 in | |
665 AM.numItems m = AM.numItems mm | |
666 <\oguard\> | |
667 (fn _ => SOME mm) | |
668 end) | |
669 in | |
670 case List.foldl test (SOME m) ms of | |
671 NONE => false | |
672 | SOME _ => true | |
673 end | |
674 | _ => false) | |
675 | Always => true | |
676 | Never => (case qs of [_] => PS.numItems pureArgs = 0 | _ => false) | |
652 | NoPureAll => (case qs of [] => false | _ => true) | 677 | NoPureAll => (case qs of [] => false | _ => true) |
653 | NoPureOne => (case qs of [] => false | _ => PS.numItems pureArgs = 0) | 678 | NoPureOne => (case qs of [] => false | _ => PS.numItems pureArgs = 0) |
654 | NoCombo => PS.numItems pureArgs = 0 orelse AM.numItems argsMap = 0 | 679 | NoCombo => PS.numItems pureArgs = 0 orelse AM.numItems argsMap = 0 |
655 in | 680 in |
656 (* Put arguments we might invalidate by first. *) | 681 (* Put arguments we might invalidate by first. *) |