# Pushing elements into a view

**URL:** <https://discourse.julialang.org/t/pushing-elements-into-a-view/119733>\
**Category:** Performance\
**Tags:** memory-allocation, views\
**Created:** [September 23, 2024, 8:18am UTC](https://discourse.julialang.org/t/pushing-elements-into-a-view/119733 "2024-09-23T08:18:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fmor/32/211337_2.png) [@fmor](https://discourse.julialang.org/u/fmor)\
**Post date:** [September 23, 2024, 8:18am UTC](https://discourse.julialang.org/t/pushing-elements-into-a-view/119733/1 "2024-09-23T08:18:34Z")

</div>

I’m working on a clustering model, and to compute cluster weights I have to compare the cohesions of the current cluster k to the one of cluster k plus unit j (respectively old (o) and new (n) in the code). To do so I thought about using views for the current cluster k, but I haven’ figured out a way to avoid the copy in order to also add the unit j’s information.

Is there a way to define also the new variables (`s1n`, `s2n`, etc) in terms of views? since I guess all the copies are affecting a bit the performance, since also I am doing this computations thousands of times during the fit. But maybe it’s impossible since those new variables are obtained merging the information of two different arrays (eg `sp1_red` and `sp1` for `s1n`).

```julia
## stuff for minimal working example
Si_red = rand((-2:2),10)
j = 1; k=1
sp1_red = rand(10); sp2_red = rand(10)
sp1 = rand(10); sp2 = rand(10)
spatial_cohesion(s1,s2) = sum(s1)+sum(s2)
covariate_similarity(X) = sum(X)
sPPM = true
cl_xPPM = true
p_cl = 2
t=1
Xcl_covariates_red = rand(10,2)
Xcl_covariates = rand(10,2,10)

aux_idxs = findall(Si_red .== k)
if sPPM
	s1o = sp1_red[aux_idxs]
	s2o = sp2_red[aux_idxs]
	s1n = copy(s1o); push!(s1n, sp1[j])
	s2n = copy(s2o); push!(s2n, sp2[j])
	lCo = spatial_cohesion(s1o, s2o)
	lCn = spatial_cohesion(s1n, s2n)
end
if cl_xPPM
	lSo = 0.; lSn = 0.
	for p in 1:p_cl
		Xo = Xcl_covariates_red[aux_idxs,p]
		Xn = copy(Xo); push!(Xn,Xcl_covariates[j,p,t])
		lSo += covariate_similarity(Xo)
		lSn += covariate_similarity(Xn)
	end
end

```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [September 23, 2024, 9:03am UTC](https://discourse.julialang.org/t/pushing-elements-into-a-view/119733/2 "2024-09-23T09:03:31Z")

</div>

Maybe you could keep around another dataset of size `n+1` and only replace the last element dynamically?

---

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [September 23, 2024, 9:09am UTC](https://discourse.julialang.org/t/pushing-elements-into-a-view/119733/3 "2024-09-23T09:09:24Z")

</div>

Haven’t looked close in your code logic. Just noticed you were talking about views but were not using them. So know you can create a view in the very easy following way:

```julia
ulia> a = [1,2,3,4,5]
5-element Vector{Int64}:
 1
 2
 3
 4
 5

julia> view(a, 2:4)
3-element view(::Vector{Int64}, 2:4) with eltype Int64:
 2
 3
 4

```

Pushing however should happen in the full vector `a`. So maybe keep some indices around and use `insert!` on the parent vector ?

---

<div class="post-metadata">

**Author:** ![fmor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fmor/32/211337_2.png) [@fmor](https://discourse.julialang.org/u/fmor)\
**Post date:** [September 23, 2024, 9:16am UTC](https://discourse.julialang.org/t/pushing-elements-into-a-view/119733/4 "2024-09-23T09:16:28Z")

</div>

Ah yes sorry, I forgot the part where I define the reduced indexes. Before that code I wrote in the other post I had this:

```julia
for j in 1:n
    # indexes = ...
	# get also the reduced spatial info if sPPM model
	if sPPM
		sp1_red = @view sp1[indexes]
		sp2_red = @view sp2[indexes]
	end
	# and the reduced covariates info if cl_xPPM model
	if cl_xPPM
		Xcl_covariates_red = @view Xcl_covariates[indexes,:,t]
	end
    # other stuff and the code I included before

```

---

<div class="post-metadata">

**Author:** ![fmor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fmor/32/211337_2.png) [@fmor](https://discourse.julialang.org/u/fmor)\
**Post date:** [September 23, 2024, 9:29am UTC](https://discourse.julialang.org/t/pushing-elements-into-a-view/119733/5 "2024-09-23T09:29:19Z")

</div>

Yes I think this is the only option. I will try it.
