Пример #1
0
// mergeWriteBytes merges adjacent opWB operations into a single WB
func (p *program) mergeWriteBytes() {
	removedReferences := make(map[valType]struct{})
	for name, code := range p.code {
		wb := -1
		ii := 0
		checkWBMerge := func() {
			if wb >= 0 {
				// We had a WB sequence, check its length
				// and merge it if appropriate
				count := ii - wb
				if count > 1 {
					var buf bytes.Buffer
					var refs []valType
					for c := wb; c < ii; c++ {
						wbInst := code[c]
						removedReferences[wbInst.val] = struct{}{}
						refs = append(refs, wbInst.val)
						buf.Write(p.bytesValue(wbInst.val))
					}
					repl := []inst{
						inst{op: opWB, val: p.internBytes(buf.Bytes())},
					}
					code = instructions(code).replace(wb, count, repl)
					compilerDebugf("merged %d WB instructions with byte slice refs %v at PC %d (new ref %d)\n",
						count, refs, wb, repl[0].val)
				}
			}
		}
		for ; ii < len(code); ii++ {
			v := code[ii]
			if v.op == opWB {
				if wb < 0 {
					wb = ii
				}
			} else {
				checkWBMerge()
				wb = -1
			}
		}
		checkWBMerge()
		p.code[name] = code
	}
	// Sort references from higher to lower, so we
	// don't invalidate them while they're being removed
	sortedReferences := make([]valType, 0, len(removedReferences))
	for r := range removedReferences {
		sortedReferences = append(sortedReferences, r)
	}
	generic.SortFunc(sortedReferences, func(a, b valType) bool {
		return a > b
	})
	compilerDebugln("candidates for byte slice removal", sortedReferences)
	for _, r := range sortedReferences {
		if !p.referencesBytes(r) {
			compilerDebugln("will remove byte slice reference", r)
			p.removeInternedBytes(r)
		}
	}
}
Пример #2
0
func sortArticles(articles []*article.Article) {
	generic.SortFunc(articles, func(a1, a2 *article.Article) bool {
		if a1.Priority < a2.Priority {
			return true
		}
		if a1.Created().Sub(a2.Created()) > 0 {
			return true
		}
		return a1.Title() < a2.Title()
	})
}
Пример #3
0
func (d *appData) enabledSocialAccountTypes() []*socialAccountType {
	if d.socialAccountTypes == nil {
		st := make([]*socialAccountType, 0)
		if d.opts.FacebookApp != nil {
			st = append(st, socialAccountTypes[SocialAccountTypeFacebook])
		}
		if d.opts.TwitterApp != nil {
			st = append(st, socialAccountTypes[SocialAccountTypeTwitter])
		}
		if d.opts.GoogleApp != nil {
			st = append(st, socialAccountTypes[SocialAccountTypeGoogle])
		}
		if d.opts.GithubApp != nil {
			st = append(st, socialAccountTypes[SocialAccountTypeGithub])
		}
		positions := make(map[SocialAccountType]int)
		for k := range socialAccountTypes {
			positions[k] = -1
		}
		for ii, k := range d.opts.SocialAccountsOrder {
			positions[k] = ii
		}
		generic.SortFunc(st, func(s1, s2 *socialAccountType) bool {
			p1, p2 := positions[s1.Name], positions[s2.Name]
			if p1 == -1 {
				return false
			}
			if p2 == -1 {
				return true
			}
			return p1 < p2
		})
		d.socialAccountTypes = st
	}
	return d.socialAccountTypes
}