Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected allocation in logical operators #386

Closed
dolejm1 opened this issue Oct 15, 2015 · 2 comments
Closed

Unexpected allocation in logical operators #386

dolejm1 opened this issue Oct 15, 2015 · 2 comments

Comments

@dolejm1
Copy link

dolejm1 commented Oct 15, 2015

There is a lot off allocations in the function:
function ensureConectivity1{T,N}(labels::Image{T,N,Array{T,N}})
nlabels = copy(labels)
nlabel = one(T)
for i_2 = 1: size(labels, 2)
for i_1 = 1: size(labels, 1)
if (zero(T) < nlabels[i_1, i_2])
nlabels[i_1, i_2] = nlabel
end
end
end
return nlabels, nlabel - one(T)
end

but if I change the nlabels in the is less statement to labels, all the allocations vanish
The changed function is:
function ensureConectivity1{T,N}(labels::Image{T,N,Array{T,N}})
nlabels = copy(labels)
nlabel = one(T)
for i_2 = 1: size(labels, 2)
for i_1 = 1: size(labels, 1)
if (zero(T) < labels[i_1, i_2])
nlabels[i_1, i_2] = nlabel
end
end
end
return nlabels, nlabel - one(T)
end

I don't see the allocations if the function operates on Array instead of Image:
function ensureConectivity1{T,N}(labels::Array{T,N})
nlabels = copy(labels)
nlabel = one(T)
for i_2 = 1: size(labels, 2)
for i_1 = 1: size(labels, 1)
if (zero(T) < nlabels[i_1, i_2])
nlabels[i_1, i_2] = nlabel
end
end
end
return nlabels, nlabel - one(T)
end

@timholy
Copy link
Member

timholy commented Oct 15, 2015

This comes down to a julia bug, unfortunately. This would be fixed by #370, but I had to revert that because of JuliaLang/julia#13327.

Probably your best fix is to have the version for images call a version for arrays:

ensureConnectivity1(img::AbstractImageDirect) = copyproperties(img, ensureConnectivity1(data(img)))

function ensureConnectivity(labels::AbstractMatrix)
    # code goes here
end

(Given your code, you should insist that the input be two-dimensional, but there is no need to specify types any more concretely than that.)

@timholy
Copy link
Member

timholy commented Jan 30, 2017

Closed by #577

@timholy timholy closed this as completed Jan 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants