Help with Type Hint
I'm trying to optimize some code I've written, and I have set warn on reflection as advised. I'm having a hard time getting a simple statement to avoid reflection. user= (== (byte 0x1) (byte 0x1)) Reflection warning, line: 33 - call to equiv can't be resolved. Can you use type hints on primitive types? I've tried obvious stuff like: user= (== #^byte (byte 0x1) #^byte (byte 0x1)) Reflection warning, line: 4 - call to equiv can't be resolved. and other variations without success. Thanks, Travis --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Help with Type Hint
If you're going to do that you're going to need to create a let binding which type-hints coll to bytes in your byte-array-contains? If you're going to be doing this a lot in your code I'd recommend making a helper class in Java. loop/recur is fast, but for absolute speed, you just can't beat putting your tight loops in Java. On Thu, May 14, 2009 at 12:00 PM, tmountain tinymount...@gmail.com wrote: NM, huge difference in performance even with the cast. Old version: (time (dotimes [_ 1000] (byte-array-contains? header 0xFE))) Elapsed time: 337.312 msecs New version: (time (dotimes [_ 1000] (byte-array-contains? header 0xFE))) Elapsed time: 4.278 msecs On that note, is there a native way to check whether a byte array contains a given value? The contains? function seems to indicate whether a given index exists in the array rather than inspecting the actual value? My best stab at the moment is as follows: (defn byte-array-contains? [coll key] scans a byte array for a given value (loop [i 0] (if ( i (count coll)) (if (== (int (byte key)) (int (byte (aget coll i true (recur (inc i)) Travis On May 14, 11:40 am, tmountain tinymount...@gmail.com wrote: If that's the case, would I even get a performance increase, or would the cast overhead cost more than the implicit reflection? On May 14, 11:32 am, David Nolen dnolen.li...@gmail.com wrote: This baffled me as well. You need to cast to int. On Thu, May 14, 2009 at 11:22 AM, tmountain tinymount...@gmail.com wrote: I'm trying to optimize some code I've written, and I have set warn on reflection as advised. I'm having a hard time getting a simple statement to avoid reflection. user= (== (byte 0x1) (byte 0x1)) Reflection warning, line: 33 - call to equiv can't be resolved. Can you use type hints on primitive types? I've tried obvious stuff like: user= (== #^byte (byte 0x1) #^byte (byte 0x1)) Reflection warning, line: 4 - call to equiv can't be resolved. and other variations without success. Thanks, Travis --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Help with Type Hint
Thanks for all the helpful advice. I may consider rewriting key portions in Java if performance becomes an issue. Travis On May 14, 12:10 pm, David Nolen dnolen.li...@gmail.com wrote: If you're going to do that you're going to need to create a let binding which type-hints coll to bytes in your byte-array-contains? If you're going to be doing this a lot in your code I'd recommend making a helper class in Java. loop/recur is fast, but for absolute speed, you just can't beat putting your tight loops in Java. On Thu, May 14, 2009 at 12:00 PM, tmountain tinymount...@gmail.com wrote: NM, huge difference in performance even with the cast. Old version: (time (dotimes [_ 1000] (byte-array-contains? header 0xFE))) Elapsed time: 337.312 msecs New version: (time (dotimes [_ 1000] (byte-array-contains? header 0xFE))) Elapsed time: 4.278 msecs On that note, is there a native way to check whether a byte array contains a given value? The contains? function seems to indicate whether a given index exists in the array rather than inspecting the actual value? My best stab at the moment is as follows: (defn byte-array-contains? [coll key] scans a byte array for a given value (loop [i 0] (if ( i (count coll)) (if (== (int (byte key)) (int (byte (aget coll i true (recur (inc i)) Travis On May 14, 11:40 am, tmountain tinymount...@gmail.com wrote: If that's the case, would I even get a performance increase, or would the cast overhead cost more than the implicit reflection? On May 14, 11:32 am, David Nolen dnolen.li...@gmail.com wrote: This baffled me as well. You need to cast to int. On Thu, May 14, 2009 at 11:22 AM, tmountain tinymount...@gmail.com wrote: I'm trying to optimize some code I've written, and I have set warn on reflection as advised. I'm having a hard time getting a simple statement to avoid reflection. user= (== (byte 0x1) (byte 0x1)) Reflection warning, line: 33 - call to equiv can't be resolved. Can you use type hints on primitive types? I've tried obvious stuff like: user= (== #^byte (byte 0x1) #^byte (byte 0x1)) Reflection warning, line: 4 - call to equiv can't be resolved. and other variations without success. Thanks, Travis --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Help with Type Hint
Please correct me if I'm wrong, but my understanding is that Clojure compiler can produce bytecode equivalent to compiled Java code. I think the right approach would be to figure out how to do this in Clojure for the cases like this. Rich? Boris On Thu, May 14, 2009 at 2:25 PM, tmountain tinymount...@gmail.com wrote: Thanks for all the helpful advice. I may consider rewriting key portions in Java if performance becomes an issue. Travis On May 14, 12:10 pm, David Nolen dnolen.li...@gmail.com wrote: If you're going to do that you're going to need to create a let binding which type-hints coll to bytes in your byte-array-contains? If you're going to be doing this a lot in your code I'd recommend making a helper class in Java. loop/recur is fast, but for absolute speed, you just can't beat putting your tight loops in Java. On Thu, May 14, 2009 at 12:00 PM, tmountain tinymount...@gmail.com wrote: NM, huge difference in performance even with the cast. Old version: (time (dotimes [_ 1000] (byte-array-contains? header 0xFE))) Elapsed time: 337.312 msecs New version: (time (dotimes [_ 1000] (byte-array-contains? header 0xFE))) Elapsed time: 4.278 msecs On that note, is there a native way to check whether a byte array contains a given value? The contains? function seems to indicate whether a given index exists in the array rather than inspecting the actual value? My best stab at the moment is as follows: (defn byte-array-contains? [coll key] scans a byte array for a given value (loop [i 0] (if ( i (count coll)) (if (== (int (byte key)) (int (byte (aget coll i true (recur (inc i)) Travis On May 14, 11:40 am, tmountain tinymount...@gmail.com wrote: If that's the case, would I even get a performance increase, or would the cast overhead cost more than the implicit reflection? On May 14, 11:32 am, David Nolen dnolen.li...@gmail.com wrote: This baffled me as well. You need to cast to int. On Thu, May 14, 2009 at 11:22 AM, tmountain tinymount...@gmail.com wrote: I'm trying to optimize some code I've written, and I have set warn on reflection as advised. I'm having a hard time getting a simple statement to avoid reflection. user= (== (byte 0x1) (byte 0x1)) Reflection warning, line: 33 - call to equiv can't be resolved. Can you use type hints on primitive types? I've tried obvious stuff like: user= (== #^byte (byte 0x1) #^byte (byte 0x1)) Reflection warning, line: 4 - call to equiv can't be resolved. and other variations without success. Thanks, Travis --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Help with Type Hint
This is the best I was able to come up with in Clojure: (defn byte-array-contains? [coll key] scans a byte array for a given value (let [c (int (count coll)) k (byte key)] (loop [i (int 0)] (if ( i c) (if (= k (aget coll i)) true (recur (unchecked-inc i))) On May 14, 8:22 am, tmountain tinymount...@gmail.com wrote: I'm trying to optimize some code I've written, and I have set warn on reflection as advised. I'm having a hard time getting a simple statement to avoid reflection. user= (== (byte 0x1) (byte 0x1)) Reflection warning, line: 33 - call to equiv can't be resolved. Can you use type hints on primitive types? I've tried obvious stuff like: user= (== #^byte (byte 0x1) #^byte (byte 0x1)) Reflection warning, line: 4 - call to equiv can't be resolved. and other variations without success. Thanks, Travis --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Help with Type Hint
so I took a look at with this code: http://gist.github.com/111935 output: :original Elapsed time: 369.683 msecs :redux-1 Elapsed time: 11672.329 msecs :redux-2 Elapsed time: 74.233 msecs as to why there is such a huge difference between your code and redux-2 I am not sure. I would definitely take a look at http://clojure.org/java_interop#toc46 if you haven't yet. On Thu, May 14, 2009 at 1:19 PM, Dimiter malkia Stanev mal...@gmail.com wrote: This is the best I was able to come up with in Clojure: (defn byte-array-contains? [coll key] scans a byte array for a given value (let [c (int (count coll)) k (byte key)] (loop [i (int 0)] (if ( i c) (if (= k (aget coll i)) true (recur (unchecked-inc i))) On May 14, 8:22 am, tmountain tinymount...@gmail.com wrote: I'm trying to optimize some code I've written, and I have set warn on reflection as advised. I'm having a hard time getting a simple statement to avoid reflection. user= (== (byte 0x1) (byte 0x1)) Reflection warning, line: 33 - call to equiv can't be resolved. Can you use type hints on primitive types? I've tried obvious stuff like: user= (== #^byte (byte 0x1) #^byte (byte 0x1)) Reflection warning, line: 4 - call to equiv can't be resolved. and other variations without success. Thanks, Travis -- And what is good, Phaedrus, And what is not good— Need we ask anyone to tell us these things? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---