orlandos-nl / BSON

Native Swift library for BSON (http://bsonspec.org)
https://orlandos.nl/docs/mongokitten/articles/bson
MIT License
110 stars 35 forks source link

Issues with the performance of `debugDescription` #60

Closed JoachimM7 closed 2 years ago

JoachimM7 commented 4 years ago

I use BSON in MongoKitten and I have a aggregation, that returns two Document. The query itself is very fast, it just needs 0.009 seconds. But printing out the documents increases the time to 0.22 seconds!

This is my code:

let start = DispatchTime.now()

let pipeline = benutzerCollection.aggregate([
    .lookup(from: "userGroups", localField: "userGroupID", foreignField: "_id", as: "group"),
    .unwind(fieldPath: "$group"),
    match("group.hasMagic" == true)
])

pipeline.execute().whenComplete { result in
    switch result {
    case .success(let finalizedCursor):
        finalizedCursor.cursor.forEach { document in
        print(document)
    }

    let end = DispatchTime.now()

    let nanoTime = end.uptimeNanoseconds - start.uptimeNanoseconds
    let timeInterval = Double(nanoTime) / 1_000_000_000 // Technically could overflow for long running tests

    print("Time to evaluate: \(timeInterval) seconds")
...

I run the example and took a look at Instruments. Please have a look:

screen

Is there any way how to improve this performance? Maybe I have to tell, that the documents are not very small and have a lot of key-value-properties (mostly boolean). But running the same query for example in Robo3T immediatly prints out the JSON string.

JoachimM7 commented 4 years ago

By the way: I took the result of my query and I wrote a Swift Dictionary with [String: Any]. It took 0.04 seconds to print this out, what is still not very fast, but way faster then printing the BSON Document ;)

P.S.: Please don't get me wrong, printing out result is not necessary, but I think that there may be some other performance issues with the DocumentPairIterator.