Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0e96924

Browse files
authored
Type relations cache: optimize key generation (#19120)
1 parent5a67a28 commit0e96924

File tree

2 files changed

+120
-74
lines changed

2 files changed

+120
-74
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
###Fixed
2+
3+
* Type relations cache: optimize key generation ([Issue#19116](https://github.com/dotnet/fsharp/issues/18767)) ([PR#19120](https://github.com/dotnet/fsharp/pull/19120))

‎src/Compiler/Utilities/TypeHashing.fs‎

Lines changed: 117 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -389,147 +389,190 @@ module StructuralUtilities =
389389

390390
[<Struct; NoComparison; RequireQualifiedAccess>]
391391
typeTypeToken=
392-
| Stampofstamp:Stamp
393-
| UCaseofname:string
394-
| Nullnessofnullness:NullnessInfo
392+
| Stampofint
393+
| UCaseofint
394+
| Nullnessofint
395395
| NullnessUnsolved
396-
| TupInfoofb:bool
396+
| TupInfoofint
397397
| Forallofint
398398
| MeasureOne
399-
| MeasureRationalofint*int
399+
| MeasureDenominatorofint
400+
| MeasureNumeratorofint
400401
| Solvedofint
401402
| Unsolvedofint
402403
| Rigidofint
403404

404405
typeTypeStructure=
405406
| StableofTypeToken[]
407+
// Unstable means that the type structure of a given TType may change because of constraint solving or Trace.Undo.
406408
| UnstableofTypeToken[]
407409
| PossiblyInfinite
408410

409-
typeprivateEmitContext=
410-
{
411-
typarMap:System.Collections.Generic.Dictionary<Stamp, int>
412-
emitNullness:bool
413-
mutable stable:bool
414-
}
411+
typeprivateGenerationContext()=
412+
member valTyparMap= System.Collections.Generic.Dictionary<Stamp, int>(4)
413+
member valTokens= ResizeArray<TypeToken>(256)
414+
member valEmitNullness=falsewith get, set
415+
member valStable=truewith get, set
415416

416-
letprivateemitNullness env(n:Nullness)=
417-
seq{
418-
if env.emitNullnessthen
419-
env.stable<-false//
417+
memberthis.Reset()=
418+
this.TyparMap.Clear()
419+
this.Tokens.Clear()
420+
this.EmitNullness<-false
421+
this.Stable<-true
420422

423+
letprivatecontext=
424+
new System.Threading.ThreadLocal<GenerationContext>(fun()-> GenerationContext())
425+
426+
letprivategetContext()=
427+
letctx= context.Value
428+
ctx.Reset()
429+
ctx
430+
431+
let inlineprivateencodeNullness(n:NullnessInfo)=
432+
match nwith
433+
| NullnessInfo.AmbivalentToNull->0
434+
| NullnessInfo.WithNull->1
435+
| NullnessInfo.WithoutNull->2
436+
437+
letprivateemitNullness(ctx:GenerationContext)(n:Nullness)=
438+
if ctx.EmitNullnessthen
439+
ctx.Stable<-false
440+
441+
letout= ctx.Tokens
442+
443+
if out.Count<256then
421444
match n.TryEvaluate()with
422-
| ValueSome k-> TypeToken.Nullness k
423-
| ValueNone-> TypeToken.NullnessUnsolved
424-
}
445+
| ValueSome k-> out.Add(TypeToken.Nullness(encodeNullness k))
446+
| ValueNone-> out.Add(TypeToken.NullnessUnsolved)
447+
448+
let inlineprivateemitStamp(ctx:GenerationContext)(stamp:Stamp)=
449+
letout= ctx.Tokens
450+
451+
if out.Count<256then
452+
// Emit low 32 bits first
453+
letlo= int(stamp&&&0xFFFFFFFFL)
454+
out.Add(TypeToken.Stamp lo)
455+
// If high 32 bits are non-zero, emit them as another token
456+
lethi64= stamp>>>32
457+
458+
if hi64<>0L&& out.Count<256then
459+
out.Add(TypeToken.Stamp(int hi64))
425460

426-
let recprivateemitMeasure(m:Measure)=
427-
seq{
461+
let recprivateemitMeasure(ctx:GenerationContext)(m:Measure)=
462+
letout= ctx.Tokens
463+
464+
if out.Count>=256then
465+
()
466+
else
428467
match mwith
429-
| Measure.Var mv->TypeToken.Stamp mv.Stamp
430-
| Measure.Const(tcref,_)->TypeToken.Stamp tcref.Stamp
468+
| Measure.Var mv->emitStamp ctx mv.Stamp
469+
| Measure.Const(tcref,_)->emitStamp ctx tcref.Stamp
431470
| Measure.Prod(m1, m2,_)->
432-
yield!emitMeasure m1
433-
yield!emitMeasure m2
434-
| Measure.Inv m1->yield!emitMeasure m1
435-
| Measure.One_-> TypeToken.MeasureOne
471+
emitMeasure ctx m1
472+
emitMeasure ctx m2
473+
| Measure.Inv m1-> emitMeasure ctx m1
474+
| Measure.One_->out.Add(TypeToken.MeasureOne)
436475
| Measure.RationalPower(m1, r)->
437-
yield! emitMeasure m1
438-
TypeToken.MeasureRational(GetNumerator r, GetDenominator r)
439-
}
476+
emitMeasure ctx m1
477+
478+
if out.Count<256then
479+
out.Add(TypeToken.MeasureNumerator(GetNumerator r))
480+
out.Add(TypeToken.MeasureDenominator(GetDenominator r))
481+
482+
let recprivateemitTType(ctx:GenerationContext)(ty:TType)=
483+
letout= ctx.Tokens
440484

441-
andprivateemitTType(env:EmitContext)(ty:TType)=
442-
seq{
485+
if out.Count>=256then
486+
()
487+
else
443488
match tywith
444489
| TType_ucase(u, tinst)->
445-
TypeToken.Stamp u.TyconRef.Stamp
446-
TypeToken.UCase u.CaseName
490+
emitStamp ctx u.TyconRef.Stamp
491+
492+
if out.Count<256then
493+
out.Add(TypeToken.UCase(hashText u.CaseName))
447494

448495
for argin tinstdo
449-
yield!emitTTypeenv arg
496+
emitTTypectx arg
450497

451498
| TType_app(tcref, tinst, n)->
452-
TypeToken.Stamp tcref.Stamp
453-
yield!emitNullnessenv n
499+
emitStamp ctx tcref.Stamp
500+
emitNullnessctx n
454501

455502
for argin tinstdo
456-
yield!emitTTypeenv arg
503+
emitTTypectx arg
457504

458505
| TType_anon(info, tys)->
459-
TypeToken.Stamp info.Stamp
506+
emitStamp ctx info.Stamp
460507

461508
for argin tysdo
462-
yield!emitTTypeenv arg
509+
emitTTypectx arg
463510

464511
| TType_tuple(tupInfo, tys)->
465-
TypeToken.TupInfo(evalTupInfoIsStruct tupInfo)
512+
out.Add(TypeToken.TupInfo(ifevalTupInfoIsStruct tupInfothen1else0))
466513

467514
for argin tysdo
468-
yield!emitTTypeenv arg
515+
emitTTypectx arg
469516

470517
| TType_forall(tps, tau)->
471518
for tpin tpsdo
472-
env.typarMap.[tp.Stamp]<-env.typarMap.Count
519+
ctx.TyparMap.[tp.Stamp]<-ctx.TyparMap.Count
473520

474-
TypeToken.Forall tps.Length
521+
out.Add(TypeToken.Forall tps.Length)
475522

476-
yield!emitTTypeenv tau
523+
emitTTypectx tau
477524

478525
| TType_fun(d, r, n)->
479-
yield!emitTTypeenv d
480-
yield!emitTTypeenv r
481-
yield!emitNullnessenv n
526+
emitTTypectx d
527+
emitTTypectx r
528+
emitNullnessctx n
482529

483530
| TType_var(r, n)->
484-
yield!emitNullnessenv n
531+
emitNullnessctx n
485532

486533
lettyparId=
487-
matchenv.typarMap.TryGetValue r.Stampwith
534+
matchctx.TyparMap.TryGetValue r.Stampwith
488535
|true, idx-> idx
489536
|_->
490-
letidx=env.typarMap.Count
491-
env.typarMap.[r.Stamp]<- idx
537+
letidx=ctx.TyparMap.Count
538+
ctx.TyparMap.[r.Stamp]<- idx
492539
idx
493540

494541
// Solved may become unsolved, in case of Trace.Undo.
495-
env.stable<-false
542+
ifnot r.IsFromErrorthen
543+
ctx.Stable<-false
496544

497545
match r.Solutionwith
498-
| Some ty->yield!emitTTypeenv ty
546+
| Some ty-> emitTTypectx ty
499547
| None->
500-
if r.Rigidity= TyparRigidity.Rigidthen
501-
TypeToken.Rigid typarId
502-
else
503-
TypeToken.Unsolved typarId
504-
505-
| TType_measure m->yield! emitMeasure m
506-
}
548+
if out.Count<256then
549+
if r.Rigidity= TyparRigidity.Rigidthen
550+
out.Add(TypeToken.Rigid typarId)
551+
else
552+
out.Add(TypeToken.Unsolved typarId)
507553

508-
letprivategetTypeStructureOfStrippedType(ty:TType)=
554+
| TType_measure m-> emitMeasure ctx m
509555

510-
letenv=
511-
{
512-
typarMap= System.Collections.Generic.Dictionary<Stamp, int>()
513-
emitNullness=false
514-
stable=true
515-
}
556+
letprivategetTypeStructureOfStrippedTypeUncached(ty:TType)=
557+
letctx= getContext()
558+
emitTType ctx ty
516559

517-
lettokens=emitTType env ty|> Seq.truncate256|> Seq.toArray
560+
letout=ctx.Tokens
518561

519562
// If the sequence got too long, just drop it, we could be dealing with an infinite type.
520-
iftokens.Length=256then PossiblyInfinite
521-
elifnotenv.stablethen Unstable tokens
522-
else Stable tokens
563+
ifout.Count>=256then PossiblyInfinite
564+
elifnotctx.Stablethen Unstable(out.ToArray())
565+
else Stable(out.ToArray())
523566

524567
// Speed up repeated calls by memoizing results for types that yield a stable structure.
525-
letprivatememoize=
568+
letprivategetTypeStructureOfStrippedType=
526569
WeakMap.cacheConditionally
527570
(function
528571
| Stable_->true
529572
|_->false)
530-
getTypeStructureOfStrippedType
573+
getTypeStructureOfStrippedTypeUncached
531574

532575
lettryGetTypeStructureOfStrippedType ty=
533-
matchmemoize tywith
576+
matchgetTypeStructureOfStrippedType tywith
534577
| PossiblyInfinite-> ValueNone
535578
| ts-> ValueSome ts

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp