2

I found out that querying a symbol's name using symbol-name adds the symbol to obarray.

(intern-soft "random-name") ; gives nil
(symbol-name 'random-name) ; adds random-name to obarray, gives "random-name" (string).
(intern-soft "random-name") ; gives random-name

My concern is that this bloats obarray with empty symbols (i.e no value or function definition). Is there

  1. something like symbol-name which doesn't add the to the obarray?
  2. a way to clear the obarray of "useless" (all cells other than the name cell are empty) symbols?

Edit -

I just found out that even evaluating a symbol adds it to the obarray. So I am assuming this language (or any lisp) is designed in a way that you don't have to worry about the size of obarray. Is this correct?

  • 1
    Have you considered using your own obarray for such operations? intern-soft supports that with its optional argument and there's some code making use of that. – wasamasa Sep 27 at 8:15
  • I didn't know that. Does having our own obarray have any side effects? For example, do the symbols evaluate like they always do or do we have to explicitly inform to take the symbol from the new obarray? – nomad Sep 27 at 8:46
  • 1
    The lisp reader interns every symbol it encounters, regardless of what it's used for. You do not have to worry about 'bloat'. – phils Sep 27 at 9:32
  • I wanted to hear that from someone for the sake of assurance. Thanks. – nomad Sep 27 at 9:40
1

C-h f make-symbol:

make-symbol is a built-in function in ‘C source code’.

(make-symbol NAME)

Return a newly allocated uninterned symbol whose name is NAME.

Its value is void, and its function definition and property list are nil. This function does not change global state, including the match data.

And as others have said in comments, you can also intern a symbol in a different obarray. That doesn't solve your problem of obarray bloating. But that should be a non-problem anyway.

You use another obarray as a separate namespace. You can have a symbol foo in multiple obarrays. The symbols are different/separate, with different properties, etc.

You may have an X-Y problem. If you think so, try to instead ask about something you're really trying to do.

E.g., why do you think you want/need to "clear the obarray of 'useless' (all cells other than the name cell are empty) symbols"?

| improve this answer | |
  • You're right about the X-Y problem. The thing is, I sometimes don't realise it until someone points it out. About the "why" part, memory management is crucial to a standard language. But I guess that isn't something to be worried about too much in this case. – nomad Sep 28 at 18:51
  • Right. No memory management (in the usual sense at least, and in general). – Drew Sep 28 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.