comparison src/lru_cache.sml @ 2279:32a407902d3b

Rewrite LRU cache. Now uses one big hash table and is less buggy.
author Ziv Scully <ziv@mit.edu>
date Wed, 11 Nov 2015 20:01:48 -0500
parents a647a1560628
children 75cb60a7f6f1
comparison
equal deleted inserted replaced
2278:b7615e0ac4b0 2279:32a407902d3b
60 60
61 val typedArgs = paramRepeatInit (fn p => "uw_Basis_string p" ^ p) ", " 61 val typedArgs = paramRepeatInit (fn p => "uw_Basis_string p" ^ p) ", "
62 62
63 val revArgs = paramRepeatRev (fn p => "p" ^ p) ", " 63 val revArgs = paramRepeatRev (fn p => "p" ^ p) ", "
64 64
65 val numArgs = Int.toString params
66
65 in 67 in
66 Print.box 68 Print.box
67 [string ("static uw_Sqlcache_Cache cacheStruct" ^ i ^ " = {"), 69 [string ("static uw_Sqlcache_Cache cacheStruct" ^ i ^ " = {"),
68 newline, 70 newline,
69 string " .table = NULL,", 71 string " .table = NULL,",
70 newline, 72 newline,
71 string " .timeInvalid = 0,", 73 string " .timeInvalid = 0,",
72 newline, 74 newline,
73 string " .lru = NULL,", 75 string " .timeNow = 0};",
74 newline,
75 string (" .height = " ^ Int.toString (params - 1) ^ "};"),
76 newline, 76 newline,
77 string ("static uw_Sqlcache_Cache *cache" ^ i ^ " = &cacheStruct" ^ i ^ ";"), 77 string ("static uw_Sqlcache_Cache *cache" ^ i ^ " = &cacheStruct" ^ i ^ ";"),
78 newline, 78 newline,
79 newline, 79 newline,
80 80
81 string ("static uw_Basis_string uw_Sqlcache_check" ^ i), 81 string ("static uw_Basis_string uw_Sqlcache_check" ^ i),
82 string ("(uw_context ctx" ^ typedArgs ^ ") {"), 82 string ("(uw_context ctx" ^ typedArgs ^ ") {"),
83 newline, 83 newline,
84 string (" char *ks[] = {" ^ revArgs ^ "};"), 84 string (" char *ks[] = {" ^ revArgs ^ "};"),
85 newline, 85 newline,
86 string (" uw_Sqlcache_CacheValue *v = uw_Sqlcache_check(cache" ^ i ^ ", ks);"), 86 string " uw_Sqlcache_Value *v = ",
87 string ("uw_Sqlcache_check(cache" ^ i ^ ", ks, " ^ numArgs ^ ");"),
87 newline, 88 newline,
88 (* If the output is null, it means we had too much recursion, so it's a miss. *) 89 (* If the output is null, it means we had too much recursion, so it's a miss. *)
89 string " if (v && v->output != NULL) {", 90 string " if (v && v->output != NULL) {",
90 newline, 91 newline,
91 string (" puts(\"SQLCACHE: hit " ^ i ^ ".\");"), 92 string (" puts(\"SQLCACHE: hit " ^ i ^ ".\");"),
111 string ("static uw_unit uw_Sqlcache_store" ^ i), 112 string ("static uw_unit uw_Sqlcache_store" ^ i),
112 string ("(uw_context ctx, uw_Basis_string s" ^ typedArgs ^ ") {"), 113 string ("(uw_context ctx, uw_Basis_string s" ^ typedArgs ^ ") {"),
113 newline, 114 newline,
114 string (" char *ks[] = {" ^ revArgs ^ "};"), 115 string (" char *ks[] = {" ^ revArgs ^ "};"),
115 newline, 116 newline,
116 string (" uw_Sqlcache_CacheValue *v = malloc(sizeof(uw_Sqlcache_CacheValue));"), 117 string (" uw_Sqlcache_Value *v = malloc(sizeof(uw_Sqlcache_Value));"),
117 newline, 118 newline,
118 string " v->result = strdup(s);", 119 string " v->result = strdup(s);",
119 newline, 120 newline,
120 string " v->output = uw_recordingRead(ctx);", 121 string " v->output = uw_recordingRead(ctx);",
121 newline, 122 newline,
122 string (" puts(\"SQLCACHE: stored " ^ i ^ ".\");"), 123 string (" puts(\"SQLCACHE: stored " ^ i ^ ".\");"),
123 newline, 124 newline,
124 string (" uw_Sqlcache_store(cache" ^ i ^ ", ks, v);"), 125 string (" uw_Sqlcache_store(cache" ^ i ^ ", ks, " ^ numArgs ^ ", v);"),
125 newline, 126 newline,
126 string " return uw_unit_v;", 127 string " return uw_unit_v;",
127 newline, 128 newline,
128 string "}", 129 string "}",
129 newline, 130 newline,
132 string ("static uw_unit uw_Sqlcache_flush" ^ i), 133 string ("static uw_unit uw_Sqlcache_flush" ^ i),
133 string ("(uw_context ctx" ^ typedArgs ^ ") {"), 134 string ("(uw_context ctx" ^ typedArgs ^ ") {"),
134 newline, 135 newline,
135 string (" char *ks[] = {" ^ revArgs ^ "};"), 136 string (" char *ks[] = {" ^ revArgs ^ "};"),
136 newline, 137 newline,
137 string (" uw_Sqlcache_flush(cache" ^ i ^ ", ks);"), 138 string (" uw_Sqlcache_flush(cache" ^ i ^ ", ks, " ^ numArgs ^ ");"),
138 newline, 139 newline,
139 string " return uw_unit_v;", 140 string " return uw_unit_v;",
140 newline, 141 newline,
141 string "}", 142 string "}",
142 newline, 143 newline,