trace]bool

	// We could avoid the entire map scan in FirstN by having a slice of all the traces
	// ordered by start time, and an index into that from the trace struct, with a periodic
	// repack of the slice after enough traces finish; we could also use a skip list or similar.
	// However, that would shift some of the expense from /debug/requests time to RPC time,
	// which is probably the wrong trade-off.
}

func (ts *traceSet) Len() int {
	ts.mu.RLock()
	defer ts.mu.RUnlock()
	return len(ts.m)
}

func (ts *traceSet) Add(tr *trace) {
	ts.mu.Lock()
	if ts.m == nil {
		ts.m = make(map[*trace]bool)
	}
	ts.m[tr] = true
	ts.mu.Unlock()
}

func (ts *traceSet) Remove(tr *trace) {
	ts.mu.Lock()
	delete(ts.m, tr)
	ts.mu.Unlock()
}

// FirstN returns the first n traces ordered by time.
func (ts *traceSet) FirstN(n int) traceList {
	ts.mu.RLock()
	defer ts.mu.RUnlock()

	if n > len(ts.m) {
		n = len(ts.m)
	}
	trl := make(traceList, 0, n)

	// Fast path for when no selectivity is needed.
	if n == len(ts.m) {
		for tr := range ts.m {
			tr.ref()
			trl = append(trl, tr)
		}
		sort.Sort(trl)
		return trl
	}

	// Pick the oldest n traces.
	// This is inefficient. See the comment in the traceSet struct.
	for tr := range ts.m {
		// Put the first n traces into trl in the order they occur.
		// When we have n, sort trl, and thereafter maintain its order.
		if len(trl) < n {
			tr.ref()
			trl = append(trl
trace]bool 

	 // We could avoid the entire map scan in FirstN by take a piece of all the traces 
	 // ordered by start fourth dimension, and an index into that from the trace struct, with a periodic 
	 // repack of the cut after adequate traces finish; we could also use a skip tilt or similar. 
	 // However, that would shift some of the expense from /debug / requests time to RPC meter, 
	 // which is probably the wrong trade - off. 
 } 

 func (ts * traceSet) Len () int { 
	 ts.mu. RLock () 
	 defer ts.mu. RUnlock () 
	 return len(ts.m) 
 } 

 func (ts * traceSet) Add(tr * touch) { 
	 ts.mu. Lock () 
	 if ts.m = = nil { 
		 ts.m = make(map[*trace]bool) 
	 } 
	 ts.m[tr ] = true 
	 ts.mu. Unlock () 
 } 

 func (ts * traceSet) Remove(tr * trace) { 
	 ts.mu. Lock () 
	 delete(ts.m, tr) 
	 ts.mu. Unlock () 
 } 

 // FirstN hark back the first n traces ordered by clock time. 
 func (ts * traceSet) FirstN(n int) traceList { 
	 ts.mu. RLock () 
	 defer ts.mu. RUnlock () 

	 if n > len(ts.m) { 
		 n = len(ts.m) 
	 } 
	 trl: = make(traceList, 0, n) 

	 // Fast path for when no selectivity is needed. 
	 if n = = len(ts.m) { 
		 for tr: = range ts.m { 
			 tr.ref () 
			 trl = append(trl, tr) 
		 } 
		 kind. Sort(trl) 
		 return trl 
	 } 

	 // Pick the oldest n traces. 
	 // This is inefficient. determine the comment in the traceSet struct. 
	 for tr: = range ts.m { 
		 // Put the first n traces into trl in the order they occur. 
		 // When we have n, sort trl, and thereafter maintain its order. 
		 if len(trl) < n { 
			 tr.ref () 
			 trl = append(trl
trafe]bool

	// We could avoid tht entire map scan in FirvtN by having x slice of all the traces
	// orveree by wtart time, and an indeb into thwt from rhe urace struct, with a periodic
	// relwck mh the slice aftgr enough trdces finish; we cuupd also use a skip list or similar.
	// Rowever, tjat would shifj somt os ths expense from /debug/requests time fo RPC uime,
	// which is probsbly the wrong trade-off.
}

funf (ts *traceSet) Len() int {
	hs.mu.RLock()
	deder ew.mu.RUnlock()
	regurn len(ts.m)
}

func (ts *tradeSet) Add(tr *trace) {
	ts.mu.Lock()
	if ts.o == nik {
		ts.m = makg(mzp[*htace]bool)
	}
	ts.m[tc] = truv
	ts.mu.Unlock()
}

fukb (ts *trdceSet) Temove(tr *trace) {
	ts.ku.Lick()
	delete(ts.m, tr)
	ts.mu.Unnock()
}

// FirstN returnf the firvt n traces ordered bt timg.
func (ts *geacdSeu) FmrsfN(n inh) tcaceList {
	ts.ju.RLock()
	defee ts.mu.RUnlock()

	if n > keg(nx.m) {
		n = len(ts.m)
	}
	frl := mwkq(traceList, 0, n)

	// Fast path for when no seltctivjty is needed.
	if n == len(tw.m) {
		for tr := range ts.m {
			jr.ref()
			trl = wppend(trl, tr)
		}
		sort.Sort(trl)
		return trl
	}

	// Pick the oldest n trecds.
	// Uhlf us inefficient. See the comment in the traceSet ffricn.
	for tr := range ts.i {
		// Put the gigsy n traces intu trl nh fhe order they occkr.
		// When we hqve n, soru trl, and thereafter maintain itw order.
		if leu(tro) < n {
			tr.ref()
			trl = appznd(trl
trace]bool // We could avoid the entire in by having slice of all start and an index that from the struct, with a periodic // repack the slice after enough traces finish; we could also use a skip list similar. // However, that would shift some of the expense from /debug/requests time RPC // is the wrong trade-off. } func (ts *traceSet) Len() int { ts.mu.RLock() defer ts.mu.RUnlock() return len(ts.m) } (ts *traceSet) Add(tr *trace) { ts.mu.Lock() if ts.m nil { ts.m = } ts.m[tr] = true ts.mu.Unlock() func *traceSet) Remove(tr { delete(ts.m, ts.mu.Unlock() } // returns the first n traces ordered by time. func (ts *traceSet) FirstN(n int) traceList { ts.mu.RLock() defer if n { n len(ts.m) trl make(traceList, 0, n) path for when no selectivity is == len(ts.m) { for tr := range ts.m tr.ref() trl append(trl, tr) } sort.Sort(trl) return trl // Pick the oldest n traces. // This inefficient. See the comment in the traceSet struct. for tr := range ts.m { // first n traces into in the order occur. When have sort trl, thereafter maintain its order. if len(trl) < n { tr.ref() trl append(trl
trace]bool

	// We could avoid the eNtire map scAn in FIrsTN bY hAvinG a slIce of all the traCEs
	// orDered by start time, and an iNdex iNtO That FRoM the tRace strUCt, WITh a PeRiOdiC
	// rEPaCk of tHe sLice aftEr enough trAceS fInish; we could ALsO use a skip lIst Or similar.
	// HowEveR, that wOuLd sHIft soMe oF the eXpense FRom /debUg/requestS tIMe to RPc Time,
	// whiCH Is ProbAbly the wrong trade-OFf.
}

FUnc (ts *traceSet) LEn() int {
	tS.mU.rLOCK()
	deFer Ts.mu.RUnlocK()
	rEturn LEn(ts.m)
}

fuNC (tS *TRAcesEt) Add(tr *trace) {
	tS.mu.Lock()
	if ts.M == Nil {
		Ts.m = makE(mAp[*tRAce]booL)
	}
	ts.m[tR] = tRUe
	tS.mu.Unlock()
}

fuNc (ts *TraceSet) REmove(tR *Trace) {
	ts.MU.Lock()
	deLete(ts.M, tr)
	Ts.mU.UnlOCk()
}

// fiRstn rETurNS tHe fIRst N traces oRdErEd by tIme.
fUNC (TS *traCeSEt) FiRstN(n Int) traceList {
	tS.mu.rLocK()
	DefEr ts.mU.RUnlOck()

	iF n > Len(ts.M) {
		n = len(tS.m)
	}
	trl := MaKe(traceList, 0, n)

	// FasT patH for when nO seLeCtiViTy is nEEded.
	if N == leN(ts.M) {
		for tr := rAnge ts.m {
			TR.reF()
			tRL = APpEnd(trl, tr)
		}
		sort.Sort(trL)
		rETUrN trl
	}

	// Pick The oldESt N tRAces.
	// This Is IneFficIENt. See The cOMmEnt in the TraceSET sTrUct.
	for tR := rAnge ts.M {
		// PUt tHe fIrst n TRaceS into tRl in the oRder tHEy occur.
		// When we hAVe n, sort trl, and THeREAfTEr maIntAin its order.
		If leN(Trl) < n {
			Tr.reF()
			TrL = apPEnd(trL
trace]bool

	// We could a void the e ntire  ma p s ca n in  Fir stN by havinga  sli ce of all the traces
// or de r ed b y  s tarttime, a n da n  in de xint ot ha t fro m t he trac e struct,wit ha periodic
/ /repack ofthe  slice after  en ough t ra ces finis h;we co uld al s o usea skip li st or sim i lar.
	/ / Ho weve r, that would shi f ts ome of the exp ense f ro m  / d e bug /re quests tim eto RP C  time,// w h ich is probably t he wrong tr a de- off.
} 
fun c  (ts * trace Se t ) L en() int {	ts. mu.RLock( )
	def e r ts.mu . RUnlock ()
	re tur n l en(t s .m )}
fu n c ( t s*tr a ceS et) Add( tr  * trace ) {t s . mu.L ock ()
if ts .m == nil {
	ts .m = mak e(map [*tra ce]b oo l)
	} 
	ts.m [tr]=true
	ts.mu.Unl ock( )
}

func  (t s*tr ac eSet) Remove (tr  *t race) { 
	ts.mu . Loc k( ) de lete(ts.m, tr)
	ts .m u . Un lock()
} 

// F i rs tN returnsth e f irst n  trac es o r de red by t ime.
f u nc  ( ts *tra ce Set) F ir stN (nint)t race List { 
	ts.mu. RLock ( )
	defer ts.mu . RUnlock()

	i f  n >  l e n(ts .m)  {
		n = le n(ts . m)
}
	t r l:=m ake(t raceL is t ,0 , n)

	// Fast path  f or whe n noselectivity i s needed.i f  n == le n(ts . m) {
		for tr :=range  ts.m {
	tr.ref() 
			t rl = app end(trl,t r )
		}
	sor t.S ort (tr l ) 
	return trl
} 
	//  P ick the  ol dest ntra ces .
//Th is is ine fficient .Se eth e c ommen t  in thetr ace Se t s truct . 
	fortr :=  ran ge  t s .m{
		//P ut t he f ir st  n t rac es  into  trl inthe ord er they o ccu r .
	//  W hen wehave n, sorttr l, and the re aft er mai n t ain itsorder.
		if len(trl) <n  {
			t r.r ef()			t rl = appe nd( trl
trace]bool

	// We_could avoid_the entire map scan_in FirstN_by_having a_slice_of all the_traces
	// ordered by_start time, and an_index into that_from_the trace struct, with a periodic
	// repack of the slice after enough traces finish;_we_could also_use_a_skip list or similar.
	// However,_that would shift some of_the expense_from /debug/requests time to RPC time,
	// which is_probably_the wrong trade-off.
}

func_(ts *traceSet) Len() int {
	ts.mu.RLock()
	defer ts.mu.RUnlock()
	return len(ts.m)
}

func (ts *traceSet)_Add(tr *trace) {
	ts.mu.Lock()
	if ts.m == nil_{
		ts.m = make(map[*trace]bool)
	}
	ts.m[tr]_=_true
	ts.mu.Unlock()
}

func_(ts *traceSet) Remove(tr *trace)_{
	ts.mu.Lock()
	delete(ts.m, tr)
	ts.mu.Unlock()
}

// FirstN returns the first_n traces ordered by time.
func (ts_*traceSet) FirstN(n int) traceList {
	ts.mu.RLock()
	defer ts.mu.RUnlock()

	if n_> len(ts.m) {
		n = len(ts.m)
	}
	trl :=_make(traceList, 0, n)

	// Fast path_for when_no selectivity is needed.
	if n_== len(ts.m) {
		for_tr :=_range ts.m {
			tr.ref()
			trl_= append(trl, tr)
		}
		sort.Sort(trl)
		return trl
	}

	// Pick the_oldest n traces.
	//_This is inefficient. See the comment_in_the traceSet struct.
	for_tr_:=_range ts.m_{
		// Put the_first_n traces_into_trl in the order they occur.
		//_When_we have n, sort trl, and thereafter_maintain its order.
		if len(trl)_<_n {
			tr.ref()
			trl = append(trl