Too much allocations

abstract type CVGSurface end

struct Triangle <: CVGSurface
    triangleID :: Int64
    x :: Float64
    y :: Float64
    z :: Float64
end
Triangle() = Triangle(1,1.0,1.0,1.0)
    


struct TrBoundry <:CVGSurface
    triangleID1 :: Int64
    triangleID2 :: Int64
    triangleID3 :: Int64
    boundryID ::Int64
end
TrBoundry() = TrBoundry(1,1,1,1)
    

struct Surface <: CVGSurface
    n_max_vertex :: Int64
    n_vertices :: Int64
    n_triangles :: Int64
    triangles :: Array{Triangle,1}
    surfaces :: Array{TrBoundry,1}
end

function cvgseek2(file)
        filestream = open(file)
    
        fileline = readline(filestream)
        
        n_max_vertex, n_vertices, n_triangles = parse.(Int,split(fileline))
    
        triangles = Vector{Triangle}(undef,n_vertices)
        trBoundry = Vector{TrBoundry}(undef,n_triangles)
    
        for i in 1:n_vertices
            pos = position(filestream)
            seek(filestream, pos)
            string_triangleID, string_xyz... = cvg_split(filestream)
            triangleID = parse(Int,string_triangleID)
            x, y, z =  parse.(Float64,string_xyz)
            triangles[i] = Triangle(triangleID, x, y, z)
        end
    
        for i in 1:n_triangles
            pos = position(filestream)
            seek(filestream, pos)
            triangleID1, triangleID2, triangleID3, boundryID = parse.(Int, cvg_split(filestream))
            trBoundry[i]  = TrBoundry(triangleID1, triangleID2, triangleID3, boundryID)
        end
    
        return Surface(n_max_vertex, n_vertices, n_triangles, triangles, trBoundry)
     
    end

“Files” like this


julia> @btime cvgseek2(files)
123.623 ms (144035 allocations: 15.89 MiB)

How to reduce allocation?

Did you read Performance Tips · The Julia Language yet? It has some good tips.

Also, you can use @time to see what lines in your code that allocate and figure out which ones of them are unnecessary.

You also haven’t provided all the functions needed to run the code and only an image of the input file.

I would try to reduce the code to a much smaller example and see if you can figure out what allocates. Dumping a big function into discourse and asking for someone to “clean it up” for you tends to not work very well.

Also, the

pos = position(filestream)
seek(filestream, pos)

looks like they don’t do anything, or?

3 Likes

Besides the performance tips pointed out by @kristoffer.carlsson , consider reusing other projects that already have a lot of the functionality you seem to be building. For example, we are working intensively everyday in Meshes.jl to provide a good set of mesh types for use across Julia ecosystems: GitHub - JuliaGeometry/Meshes.jl: Computational geometry and meshing algorithms in Julia

Here are some notes to help tracking the allocations: Tracking allocations · JuliaNotes.jl

1 Like

This is the url。