aset and shorts, aset vs aset-int

61 views
Skip to first unread message

bOR_

unread,
May 29, 2011, 9:17:01 AM5/29/11
to clo...@googlegroups.com
Is there something obvious I am missing when aset in clojure 1.3-alpha8 won't work for shorts. aset-shorts does work, but way slower than I'd expect. There is also an order of magnitude difference in speed between aset ^ints and aset-int.

I've looked at the source of amap (which was the first thing to give me problems with shorts), and from there came to writing the expanded amap macro out, and using aset-shorts rather than aset to get shorts working. I'm however stuck at looking at the source of aset and understanding how I can make that work with shorts.

Related (now that I have the floor anyway), should I still be messing with java arrays in clojure 1.3, or would a vector of primitives be as fast?

David Nolen

unread,
May 29, 2011, 1:39:23 PM5/29/11
to clo...@googlegroups.com
aset-xxxx usage is not correct. Always use aset.

(let [^shorts as (make-array Short/TYPE 10)]
  (aset as 0 (short 1)))

Works fine.

Proper use of Java arrays is idiomatic for the forseeable future as far as I can tell. Vectors of primitives are more efficient, but there's work to be done for them to approach Java arrays in perf.

David 

bOR_

unread,
May 29, 2011, 4:35:35 PM5/29/11
to clo...@googlegroups.com
Thanks for the help, appreciated! It helped me figuring out where exactly things go haywire.

This works:
user> (let [^ints as (make-array Integer/TYPE 10)] (aset as 0 (+ (aget as 1) (aget as 2))))                                                                                                     
0     

and this breaks

user> (let [^shorts as (make-array Short/TYPE 10)] (aset as 0 (+ (aget as 1) (aget as 2)))) 
No matching method found: aset   

What am I doing wrong?

David Nolen

unread,
May 29, 2011, 4:42:59 PM5/29/11
to clo...@googlegroups.com
You need to cast to short.

(aset as 0 (short (+ (aget as 1) (aget as 2))))

David

Ken Wesson

unread,
May 29, 2011, 5:26:37 PM5/29/11
to clo...@googlegroups.com
On Sun, May 29, 2011 at 1:39 PM, David Nolen <dnolen...@gmail.com> wrote:
> Vectors of primitives are more efficient,

[1]

> but there's work to be done for them to approach Java
> arrays in perf.

[2]

Aren't your statements [1] and [2] contradictory? Did you mean "more
idiomatic" perhaps in [1], or "more functional"?

--
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

Jonathan Fischer Friberg

unread,
May 29, 2011, 5:31:54 PM5/29/11
to clo...@googlegroups.com
I think he meant that vectors of primitives are more efficient than normal vectors.

Jonathan


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Alan Malloy

unread,
May 29, 2011, 5:32:40 PM5/29/11
to Clojure
More efficient than vectors of non-primitives, one imagines. Or, more
efficient than primitive vectors before 1.3 (I'm not sure if primitive
vectors exist in 1.2).

On May 29, 2:26 pm, Ken Wesson <kwess...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages