How can I speed up multiple object joining (Python)? Context:
I am working on a tree generation script and I am pretty happy with how it turned out, but I encountered an issue when adding leaves to my tree. The leaves themselves have no modifiers and for for the sake of this question could be replaced with the default cube(their vertice number does not exceed 30).
The Problem:
Initially I thought that the coping and placing the leaves to their respective branches on the tree took the longest to complete, but after I ran some tests it turns out that actually joining the leaves to the tree is what took the longest. (Almost 3 or 4 times more than copying and moving them around)
I used the below function to join my leaves to the tree:
def JoinTreeObjectsWithTree(tree):
    global treeObjects
    #join leaf with Trunk
    tree.select_set(True)
    bpy.context.view_layer.objects.active = tree
    for treeObject in treeObjects:
        treeObject.select_set(True)

    bpy.ops.object.join()
    bpy.ops.object.select_all(action='DESELECT')
    treeObjects = []

The tree objects array is the actual array where the leaves reside and the tree is well the main body of the tree as an object.
Is there a way to speed this up?
I've been using this, which has featured on stack before.
def merge_objects(name, ob_list):
    ctx = context.copy()
    ob_list[0].name = name
    ctx[&quot;active_object&quot;] = ob_list[0]
    ctx[&quot;selected_editable_objects&quot;] = ob_list
    bpy.ops.object.join(ctx)
    return data.objects[name]

Naming is optional, but requires the tree as the first object in the list to keep its name.
def JoinTreeObjectsWithTree(tree, treeObjects):
    name = tree.name
    treeObjects.insert(0, tree)
    ctx = context.copy()
    ctx[&quot;active_object&quot;] = treeObjects[0]
    ctx[&quot;selected_editable_objects&quot;] = treeObjects
    bpy.ops.object.join(ctx)
    return data.objects[name]

finalOb = JoinTreeObjectsWithTree(tree, treeObjects)