steveyen / gtreap

gtreap is an immutable treap implementation in the Go Language
MIT License
89 stars 22 forks source link

Public split and improvements to visit speed #2

Open gburgett opened 9 years ago

gburgett commented 9 years ago

Hi,

I have a use case where we need to store timestamped data, split off everything before time.Now(), then iterate that split in ascending order in its entirety. I've made a couple improvements to the treap to accomplish this. Would you like to merge them back in?

The nil pivot short-circuit around the compare method was yielding about a 35% increase in performance for my particular use case. My comparison function is about as simple as you can compare timestamps:

func compare(a, b interface{}) int {
    pA := a.(myItem)
    pB := b.(myItem)

    if pA.Timestamp.Equal(pB.Timestamp) {
        return 0
    }
    if pA.Timestamp.Before(pB.Timestamp) {
        return -1
    }
    return 1
}