Advice requested: class or prototype class?

Hello all.
I am working on the rewriting of my ‘windows’ project, its going ahead well and i am happy with the code cleaness. I am in the ‘print’ part, when i can print text, images or buttons in the windows, and i have an architecture choice to make.Maybe one of you can help?
My problem is: i think that ideally, when i print something in a window i should keep a link the original data so i can copy/paste the data by a long press on it if i want. This is an option for text and images, but it is mandatory for buttons that have methods and multiple images attached to. I think the good way to do that is to define a class for each type of object i can print, with associated data and methods. But if i do so, for each object i print i believe the whole text associated to the methods will be duplicated in the memory by the class call. This will generate unnecessary memory growth in some cases: for instance if someone prints characters one by one, then thousands of them can come in a window, and if the object class is, say 1000 characters long, this will generate several Mega bytes used for this utility, so not available for the main program.
To avoid this, i have in mind to create 1 prototype of each object type in my window, and i will instanciate a printed object with another class, with no methods. Then i will add to this object links to the prototype object methods. The object will then behave exactly as the prototype, except the code in memory will be minimal.
My question: does that make sense? Is there a better way to do this? Is my concern about memory growth not relevant and i am over-optimizing?
Thanks for your feedback.

Lua’s classes are already quite lightweight.

But try it yourself and measure the memory burden of different types:


function allocateMuch()
    local mem = collectgarbage("count")
    local c = {}
    for i = 1, 1000 do
        c[i] = class()
        -- or whatever you want to construct here
        -- c[i] = i
        -- c[i] = {x = i}
    end
    print(collectgarbage("count") - mem)
end

But if i do so, for each object i print i believe the whole text associated to the methods will be duplicated in the memory by the class call.

This depends on how your text is stored. In general, lua is quite good at not duplicating stuff since everything is a pointer. Passing pointers around is cheap.

In particular, if your text is stored as a string then there will be no duplication. Lua stores strings uniquely, and once a string is stored then any reference to the same actual string becomes a pointer to the original version.

Thus in:

a = "hello"
b = "hello"

then lua only stores the string hello once, and a and b are pointers to that single location.

(This doesn’t cause any problems because strings are immutable so any operation that appears to modify a string doesn’t, it merely updates the pointer to the new string.)

Thank you both for your advices.
@andrew by ‘whole text associated to the methods’ i mean ‘the code of the method after is is compiled’ not strings generated by myself.
It seems that you both think my concern about memory might be overkill. Good to know, thanks!

I have checked the weight of one window class: 10ko, while it is huge… I think you are right: i should not worry too much. Btw, i use gcinfo() to check the memory.