Usage of symbol instead of string for signal names?

Hi,

I’m just wondering why strings are used for signal names.
What I have learnt from Ruby is that symbols are more efficient than
strings.
Symbols are typically used for keys in a hash …

Anyway, Ruby/GTK2 is great.

CM.

On 2/8/07, eclipse survey [email protected] wrote:

I’m just wondering why strings are used for signal names.
What I have learnt from Ruby is that symbols are more efficient than
strings.
Symbols are typically used for keys in a hash …

Symbols are also strings that are never freed. And the GObject way of
sending signals is by passing strings around anyway, so we’re really
not saving ourselves much by using symbols instead.

nikolai


Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

Nikolai,

From the Ruby side :

  • for a given string literal, there is only one corresponding symbol.
  • if you call 3 times signal_connect(“a_signal”) you create 3
    different strings with the same value of “a_signal”
  • Conversely, if you call 3 times signal_connect(:a_signal) you refer
    to the same instance of symbol a_signal
  • Most of signal names correspond to GTK events, so the fact that a
    symbol is a string that is never freed and exists only once (for a given
    value) is a serious advantage.

I don’t know how you can take advantage of this in the context of
GTK/GLIB.

CM.

Nikolai W. [email protected] wrote:
On 2/8/07, eclipse survey wrote:

I’m just wondering why strings are used for signal names.
What I have learnt from Ruby is that symbols are more efficient than
strings.
Symbols are typically used for keys in a hash …

Symbols are also strings that are never freed. And the GObject way of
sending signals is by passing strings around anyway, so we’re really
not saving ourselves much by using symbols instead.

nikolai


Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

On 2/9/07, eclipse survey [email protected] wrote:

Can you please quote my response in a more mail-friendly manner?

  • if you call 3 times signal_connect(“a_signal”) you create 3 different
    strings with the same value of “a_signal”

If you’re worried about creating strings you can always create a
constant.

  • Most of signal names correspond to GTK events, so the fact that a symbol
    is a string that is never freed and exists only once (for a given value) is
    a serious advantage.

Why? Just to make my point about symbols absolutely clear: a symbol
will /never/ be freed after it’s been created during the execution of
the program.

As Masao already pointed out, symbols don’t really save you anything.

nikolai


Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

Hi,

On Thu, 8 Feb 2007 09:35:37 -0800 (PST)
eclipse survey [email protected] wrote:

Hi,

I’m just wondering why strings are used for signal names.

Did you try it?

require ‘gtk2’

button = Gtk::Button.new(“click”)
button.signal_connect(:clicked) do
p “Hello World”
end
win = Gtk::Window.new.set_default_size(100, 100)
win.add(button).show_all
win.signal_connect(:destroy) do
p “destroy”
Gtk.main_quit
end
Gtk.main

What I have learnt from Ruby is that symbols are more efficient than strings.
Symbols are typically used for keys in a hash …

In Ruby-GNOME2, symbol is converted to char* to call a GLib function.
#Then the char* string is converted to guint and GQuark by GLib.

So it’s not efficient way to use symbols instead of strings.
That’s the reason why I don’t recommand to use symbols in this cases.


Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

Nikolai W. [email protected] wrote:

Can you please quote my response in a more mail-friendly manner?

Sorry. I think it’s better now.

If you’re worried about creating strings you can always create a
constant.

You have answered to my concern : - ) !

Is it possible to have within Ruby/GTK all the string constants used
by “standard” GTK signals. It would be great.

CM.

On 2/12/07, eclipse survey [email protected] wrote:

Nikolai W. [email protected] wrote:

Can you please quote my response in a more mail-friendly manner?

Sorry. I think it’s better now.

Yes, thank you.

If you’re worried about creating strings you can always create a constant.

You have answered to my concern : - ) !

Is it possible to have within Ruby/GTK all the string constants used by
“standard” GTK signals. It would be great.

Well, the whole reason for using string constants is so that one
doesn’t have to commit to some specific scheme, and strings are more
unique than integers. But no, there’s no way to get the string
constants used by Gtk. And there’s really no reason for doing this.
If you’re worrying about these kinds of things, then you may want to
consider all the layers of code that a call to a Ruby/Gtk method goes
through. It’s a lot more than the creation of a string.

nikolai


Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

Nikolai W. [email protected] wrote:

If you’re worried about creating strings you can always create a
constant.

You have answered to my concern : - ) !

Is it possible to have within Ruby/GTK all the string constants used by
“standard” GTK signals. It would be great.

Well, the whole reason for using string constants is so that one
doesn’t have to commit to some specific scheme, and strings are more
unique than integers. But no, there’s no way to get the string
constants used by Gtk. And there’s really no reason for doing this.
If you’re worrying about these kinds of things, then you may want to
consider all the layers of code that a call to a Ruby/Gtk method goes
through. It’s a lot more than the creation of a string.

nikolai

I think there is a misunderstanding here. What I suggest/request is only
having constants defined like this :

module Gtk
BUTTON_PRESS_EVENT = ‘button_press_event’
BUTTON_RELEASE_EVENT = ‘button_release_event’
BUTTON_SCROLL_EVENT = ‘scroll_event’
# etc. for other standard GTK events
end

Thus, one can use the constant variables instead of specifying
directly a string and consequently save the creation of a string each
time.

CM.

On 2/12/07, eclipse survey [email protected] wrote:

Thus, one can use the constant variables instead of specifying directly a
string and consequently save the creation of a string each time.

Yes, that was what I was suggesting, but it’s also not something I
recommend. Especially as the events use ‘-’, not ‘_’, as separators.
That is, it’s called ‘button-press-event’

nikolai


Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

Hi,

On Mon, 12 Feb 2007 07:54:50 -0800 (PST)
eclipse survey [email protected] wrote:

doesn’t have to commit to some specific scheme, and strings are more
module Gtk
BUTTON_PRESS_EVENT = ‘button_press_event’
BUTTON_RELEASE_EVENT = ‘button_release_event’
BUTTON_SCROLL_EVENT = ‘scroll_event’
# etc. for other standard GTK events
end

Ruby/GTK(1) was supported these constants.

e.g.)
button.signal_connect(Gtk::Button::SIGNAL_CLICKED) {
p “Hello World”
}

But when Ruby/GTK2 was started, we discussed and decided to remove that
feature.

button.signal_connect(“clicked”) {
p “Hello World”
}

This is simpler than Ruby/GTK(1) style and I prefer this.

Thus, one can use the constant variables instead of specifying directly a string and consequently save the creation of a string each time.

If you really need to save the creation time, you can define them in
your own application/library.
But if you really need to save the trivial time, I recommand to use C
language to implement your application.


Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

Masao M. [email protected] wrote:

Ruby/GTK(1) was supported these constants.
e.g.)
button.signal_connect(Gtk::Button::SIGNAL_CLICKED) {
p “Hello World”
}

But when Ruby/GTK2 was started, we discussed and decided to remove that feature.

button.signal_connect(“clicked”) {
p “Hello World”
}

It’s a pity. Having these constants defined doesn’ force you to use
them. So you can still use directly string.
I agree that I can define these constants in my own application. But
other people may define their own constants as well with different
naming rules … I’m just thinking about convention that “eases” other
people get into my code …
In the standard library, there is the English library that gives a
less cryptic names to global symbols. The aim is different but quite
similar as having those constants. (I don’t say that “clicked” is
cryptic). You can use the English names for the global names.

“clicked” is definitively simpler than Gtk::Button::SIGNAL_CLICKED.
The naming scheme could be simpler as well like Gtk::BUTTON_CLICKED.
All the constants can be defined directly in the Gtk module.

CM.

Hello,

eclipse survey wrote:

But when Ruby/GTK2 was started, we discussed and decided to remove
that feature.

button.signal_connect(“clicked”) {
p “Hello World”
}
It’s a pity. Having these constants defined doesn’ force you to use
them. So you can still use directly string.
A problem is also that someone must maintain the list of constants
up-to-date.
I think the problem is that you don’t provide an argument which is good
enough to justify the work.
If we talk about speed and memory use, those constants actually make the
program slightly less efficient.

Then using it is only a matter of taste. That probably doesn’t justify
the burden of maintaining them.

emmanuel


Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

Hello,

If we talk about speed and memory use, those constants actually make the
program slightly less efficient.

I don’t understand why ? Am I missing something ?
I do understand that a symbol is not a good idea for this issue
given the underlying GTK/GLIB.

But I don’t understant why a constant refering to a string is less
efficient in speed and in memory. Conversely, I think that it saves
the cost of creating each time a new string.
OK, you’re right, actually constants (unlike symbols) don’t make the
program less speed-efficient. Though they probably take up memory even
if you don’t use them. But as Nikolai W. pointed out, this is
saving 0.001% of the runtime speed or memory use here.
So I really think it’s not worth the effort, unless you can make a
benchmark that shows a measurable speed difference… which would
surprise me a lot.

emmanuel


Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

Hi,

Emmanuel T. [email protected] wrote:

A problem is also that someone must maintain the list of constants
up-to-date.

I agree.

If we talk about speed and memory use, those constants actually make
the
program slightly less efficient.

I don’t understand why ? Am I missing something ?
I do understand that a symbol is not a good idea for this issue given
the underlying GTK/GLIB.

But I don’t understant why a constant refering to a string is less
efficient in speed and in memory. Conversely, I think that it saves the
cost of creating each time a new string.

CM.