0

Assume that you have loaded a large data structure (like a table index in a database) and now you want to make a change.

If I understand correctly, you need to create a new state (including you change) based on the current state (what you have loaded from the disk) and assign it to the current identity that you have.

This will be pretty slow in a large data structure because runtime needs to duplicate a big chunk of memory. Is there a practical alternative?

7
  • 1
    It's not slow because the new and old structures share data. Not everything is copied. Just use update or assoc . You can use transients to make it faster though. Commented Jun 26, 2017 at 12:56
  • 1
    Do you mean a Java array, or a Clojure vector? There's a difference. Commented Jun 26, 2017 at 13:00
  • 1
    If you're using Clojure, you should really use Clojure data structures whenever possible. Commented Jun 26, 2017 at 13:05
  • 1
    This question is unclear - the way to make changes to a data structure depends on the structure itself. If it is mutable then you can simply mutate it, if it is immutable then you have to create new values. If you have a sequence of changes to apply then you can use transients. Commented Jun 26, 2017 at 13:05
  • 1
    @mahdix Yes, vectors are cheap to modify. If you're asking about Java arrays, it's really the same answer as if you were asking from a Java perspective. You'd use Java's array methods/subscripting to modify it, and I'd expect it would be as efficient as if you were doing it in Java. The performance of modifying an array shouldn't depend on the size of the array though (unless you're "appending" and need to copy it). Overwriting a cell is O(1) afaik. Commented Jun 26, 2017 at 13:10

2 Answers 2

2

Is there a practical alternative?

If the choice of database is within your control, you could take a look at Datomic, which is designed to work with the grain of Clojure. As I understand it (not much, to be honest)

  • differential updates to data you subscribe to are forwarded to you; and
  • the data is maintained immutably and persistently on the client side too: if you keep a handle on what it was, that never changes, even if what it is (with a different handle) is different.

To get these characteristics, you use the peer library, not the client library.


If I'm talking nonsense, knowledgeable reader, please let me know.

Sign up to request clarification or add additional context in comments.

Comments

1

Here are a couple of good overviews of Clojure's immutable data structures:

As the comments say, they don't copy the whole data structure when you make a small change. Instead, the "old" and "new" versions share data for the parts that didn't change, making the "persistent" data structures quite efficient.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.