Skip to content

Commit 669bcf3

Browse files
committed
Avoid reading whole ttcs unnecessarily
Sometimes we have to re-read, if we've previously imported and then imported public, but on the second read we don't need to read the whole thing, just the header.
1 parent 2f66f3e commit 669bcf3

File tree

2 files changed

+37
-31
lines changed

2 files changed

+37
-31
lines changed

src/Core/Binary.idr

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Core.Context
44
import Core.Context.Log
55
import Core.Core
66
import Core.Hash
7+
import Core.Name.Namespace
78
import Core.Normalise
89
import Core.Options
910
import Core.TT
@@ -31,7 +32,7 @@ import Data.Buffer
3132
-- TTC files can only be compatible if the version number is the same
3233
export
3334
ttcVersion : Int
34-
ttcVersion = 52
35+
ttcVersion = 53
3536

3637
export
3738
checkTTCVersion : String -> Int -> Int -> Core ()
@@ -177,11 +178,12 @@ writeTTCFile b file_in
177178
toBuf b (version file)
178179
toBuf b (ifaceHash file)
179180
toBuf b (importHashes file)
181+
toBuf b (imported file)
182+
toBuf b (extraData file)
180183
toBuf b (context file)
181184
toBuf b (userHoles file)
182185
toBuf b (autoHints file)
183186
toBuf b (typeHints file)
184-
toBuf b (imported file)
185187
toBuf b (nextVar file)
186188
toBuf b (currentNS file)
187189
toBuf b (nestedNS file)
@@ -191,41 +193,45 @@ writeTTCFile b file_in
191193
toBuf b (namedirectives file)
192194
toBuf b (cgdirectives file)
193195
toBuf b (transforms file)
194-
toBuf b (extraData file)
195196

196197
readTTCFile : TTC extra =>
197198
{auto c : Ref Ctxt Defs} ->
198-
String -> Maybe (Namespace) ->
199+
Bool -> String -> Maybe (Namespace) ->
199200
Ref Bin Binary -> Core (TTCFile extra)
200-
readTTCFile file as b
201+
readTTCFile readall file as b
201202
= do hdr <- fromBuf b
202203
chunk <- get Bin
203204
when (hdr /= "TT2") $ corrupt ("TTC header in " ++ file ++ " " ++ show hdr)
204205
ver <- fromBuf b
205206
checkTTCVersion file ver ttcVersion
206207
ifaceHash <- fromBuf b
207208
importHashes <- fromBuf b
208-
defs <- fromBuf b
209-
uholes <- fromBuf b
210-
autohs <- fromBuf b
211-
typehs <- fromBuf b
212-
-- coreLift $ putStrLn ("Hints: " ++ show typehs)
213-
-- coreLift $ putStrLn $ "Read " ++ show (length (map fullname defs)) ++ " defs"
214209
imp <- fromBuf b
215-
nextv <- fromBuf b
216-
cns <- fromBuf b
217-
nns <- fromBuf b
218-
pns <- fromBuf b
219-
rws <- fromBuf b
220-
prims <- fromBuf b
221-
nds <- fromBuf b
222-
cgds <- fromBuf b
223-
trans <- fromBuf b
224210
ex <- fromBuf b
225-
pure (MkTTCFile ver ifaceHash importHashes
226-
defs uholes
227-
autohs typehs imp nextv cns nns
228-
pns rws prims nds cgds trans ex)
211+
if not readall
212+
then pure (MkTTCFile ver ifaceHash importHashes [] [] [] [] []
213+
0 (mkNamespace "") [] Nothing
214+
Nothing
215+
(MkPrimNs Nothing Nothing Nothing Nothing)
216+
[] [] [] ex)
217+
else do
218+
defs <- fromBuf b
219+
uholes <- fromBuf b
220+
autohs <- fromBuf b
221+
typehs <- fromBuf b
222+
nextv <- fromBuf b
223+
cns <- fromBuf b
224+
nns <- fromBuf b
225+
pns <- fromBuf b
226+
rws <- fromBuf b
227+
prims <- fromBuf b
228+
nds <- fromBuf b
229+
cgds <- fromBuf b
230+
trans <- fromBuf b
231+
pure (MkTTCFile ver ifaceHash importHashes
232+
defs uholes
233+
autohs typehs imp nextv cns nns
234+
pns rws prims nds cgds trans ex)
229235

230236
-- Pull out the list of GlobalDefs that we want to save
231237
getSaveDefs : List Name -> List (Name, Binary) -> Defs ->
@@ -422,15 +428,17 @@ readFromTTC nestedns loc reexp fname modNS importAs
422428
let as = if importAs == miAsNamespace modNS
423429
then Nothing
424430
else Just importAs
425-
ttc <- readTTCFile fname as bin
426431

427432
-- If it's already imported, but without reexporting, then all we're
428433
-- interested in is returning which other modules to load.
429434
-- Otherwise, add the data
430-
let ex = extraData ttc
431435
if alreadyDone modNS importAs (allImported defs)
432-
then pure (Just (ex, ifaceHash ttc, imported ttc))
436+
then do ttc <- readTTCFile False fname as bin
437+
let ex = extraData ttc
438+
pure (Just (ex, ifaceHash ttc, imported ttc))
433439
else do
440+
ttc <- readTTCFile True fname as bin
441+
let ex = extraData ttc
434442
traverse_ (addGlobalDef modNS as) (context ttc)
435443
traverse_ addUserHole (userHoles ttc)
436444
setNS (currentNS ttc)

src/Core/Context.idr

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,7 @@ lookupCtxtExactI (Resolved idx) ctxt
492492
Just val =>
493493
pure $ returnDef (inlineOnly ctxt) idx !(decode ctxt idx True val)
494494
Nothing =>
495-
do let a = content ctxt
496-
arr <- get Arr
495+
do arr <- get Arr @{content ctxt}
497496
Just def <- coreLift (readArray arr idx)
498497
| Nothing => pure Nothing
499498
pure $ returnDef (inlineOnly ctxt) idx !(decode ctxt idx True def)
@@ -512,8 +511,7 @@ lookupCtxtExact (Resolved idx) ctxt
512511
Nothing => pure Nothing
513512
Just (_, def) => pure (Just def)
514513
Nothing =>
515-
do let a = content ctxt
516-
arr <- get Arr
514+
do arr <- get Arr @{content ctxt}
517515
Just res <- coreLift (readArray arr idx)
518516
| Nothing => pure Nothing
519517
def <- decode ctxt idx True res

0 commit comments

Comments
 (0)