Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Should Cipher instances be pooled?

Reply
Thread Tools

Should Cipher instances be pooled?

 
 
Rich Carreiro
Guest
Posts: n/a
 
      07-30-2009
Is it OK to call Cipher.getInstance() before each decrypt/encrypt
operation, or is it better to reuse an instance (or, in a
multi-threaded environment, have a pool of them)?

This came up because I'm working on something that uses nCipher's JCE
provider and there appear to be resource leaks internal to nCipher's
code. As part of trying to work around that, I wrote some test code
that can switch between calling Cipher.getInstance() before each
encrypt/decrypt operation or reusing a pool of Cipher instances.

(The encrypts/decrypts are all AES using CBC).

To my surprise, even with the vanilla SunJCE provider, reusing Cipher
instances -- even when having several threads using a Cipher instance
pool as small as a single instance -- ran considerably faster than
calling Cipher.getInstance() each time. (and yes, the calls to
Cipher's init() and doFinal() methods were in a synchronized block
synched on the instance I had gotten from the pool -- so it's not that
I was improperly interlacing method calls).

--
Rich Carreiro http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      07-31-2009
Rich Carreiro wrote:
> Is it OK to call Cipher.getInstance() before each decrypt/encrypt
> operation, or is it better to reuse an instance (or, in a
> multi-threaded environment, have a pool of them)?
>
> This came up because I'm working on something that uses nCipher's JCE
> provider and there appear to be resource leaks internal to nCipher's
> code. As part of trying to work around that, I wrote some test code
> that can switch between calling Cipher.getInstance() before each
> encrypt/decrypt operation or reusing a pool of Cipher instances.
>
> (The encrypts/decrypts are all AES using CBC).
>
> To my surprise, even with the vanilla SunJCE provider, reusing Cipher
> instances -- even when having several threads using a Cipher instance
> pool as small as a single instance -- ran considerably faster than
> calling Cipher.getInstance() each time.


It seems indeed as if the getInstance call is a relative
expensive call.

So if your code need to use hundreds of thousands or millions
of Cipher objects, then reusing may be a good idea.

But I suspect creating that many Cipher objects is not that
common a requirement.

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dicts,instances,containers, slotted instances, et cetera. ocschwar@gmail.com Python 8 01-29-2009 09:52 AM
Cipher jpeg stream terry Java 1 08-01-2004 05:05 AM
list of class instances within a list of a class instances John Wohlbier Python 2 02-22-2004 08:41 AM
Key generation algorithm and Cipher algorithm Ahmed Moustafa Java 0 11-15-2003 06:35 AM
Cipher.unwrap Gary Java 0 08-08-2003 03:24 AM



Advertisments