I guess it all depends on being clever enough to "use the type system
type systems are still a worthy area to pursue. I guess my experience
favor of your plan to verify that narrowing is safe. -Roger
Post by Paul LicameliGood thoughts, Roger.
But my motivation is not so much about the optimization, as instead
the appropriate use of size_t which is the unsigned type describing
sizes of things that are certain to fit inside of memory.
I see too much indiscriminate use of sampleCount expressions (which
might be large, or negative) as subscripts or as sizes passed to the
array version of operator new. I got the bug in my brain now to
inspect all of those conversions for correctness. That's not so hard
as it seems, if you use the type system the right way to help the C++
compiler help you. I can redefine types in my build so that
compilation fails unless I do something explicit when sampleCount
values need to be narrowed, and find a proof in those places that the
narrowing is not losing any nonzero bits.
PRL
My take on this is that the overhead of using 64-bit integers is
quite small in the scheme of things. Furthermore, word, pointer,
and integer sizes are a constant source of pain and portability
problems in C and C++. Looking at this historically, the mindset
when C and C++ were developed was that we needed a high-level way
to express the low-level stuff we wanted to do: manipulate bits,
use memory efficiently, etc. C tries to have it both ways: if you
want an integer, you write int and let the compiler pick the best
(fastest?) implementation, but if you want a 32-bit int, you use
conditional compilation or system-dependent definitions to get it.
In practice, all this depends on being very careful with
assumptions and definitions. I think one of the reasons Java and
Python became popular is that they handle portability at the
virtual machine level, so you don't have int meaning different
things on different systems.
Furthermore, it is very difficult to anticipate the implications
and interactions with different integer sizes -- programmers just
think in terms of ideal integers most of the time, and thinking
about how implementations can diverge from that perfect model is a
huge burden, especially in systems like Audacity that are both
large and worked on by many people. (C.f.
https://www.researchgate.net/publication/221542289_As-If_Infinitely_Ranged_Integer_Model
<https://www.researchgate.net/publication/221542289_As-If_Infinitely_Ranged_Integer_Model>
for some serious integer-related bug discovery.)
If we spend any time optimizing, it should be based on profiling -
since cache lines are longer than 64-bits, I bet the additional
overhead of 64-bit ints is very small compared to 32-bits and the
additional overhead of arithmetic on 64-bit values in the cache is
swamped by memory loads and stores.
To summarize, my approach would be don't sweat the 64-bit ints and
if there's any time for optimization, use profiling to spend
optimization efforts wisely.
-Roger
Post by Paul LicameliI think I have got satsifactory answers.
Audio files over 13.5 hours long are unusual but likely enough
that we ought to accomodate them, therefore we use this wide
integral type, called sampleCount.
My examination of the code, though, convinces me that this type
is also used unnecessarily in many places.
Where we need an offset into an audio file or into a WaveTrack or
WaveClip or Sequence -- use sampleCount. Where we need the
difference of two such values, also use sampleCount.
But where describing the number of samples that fit into a buffer
in memory or in a block file, I think size_t should be used instead.
sampleCount should not be used just whenever counting samples.
Perhaps the type name was a misnomer and sampleOffset would have
been better.
PRL
On Tue, Aug 23, 2016 at 7:50 PM, Martyn Shaw
Hi
Has this question been answered satisfactorily?
Is it still an open question?
TTFN
Martyn
Post by Paul Licamelitypedef sampleCount is a signed 64 bit quantity on Windows,
and long
Post by Paul Licamelilong on others. I can tell that on Mac that is the same
width because
Post by Paul Licamelithis compiles without error (using the new C++11 keyword
static_assert(sizeof(longlong) == 8, "");
Someone tell me if that compiles on Linux too.
Who recalls why it was decided that the type should be this
wide? You
Post by Paul Licamelineed over 27.05 hours of audio, at 44.1 kHz sampling rate,
to overflow
Post by Paul Licamelian unsigned 32 bit integer, or half of that for a signed
integer.
Post by Paul LicameliWho ever makes a track that long?
Well maybe it is sometimes done, because I answered a
question from
Post by Paul Licamelisomeone on the Audacity Google group, who was trying to
export more
Post by Paul Licamelithan 13.5 hours of audio at once. The re-imported audio
was truncated
Post by Paul Licamelito less than that length, but I had reason to believe it
was because
Post by Paul Licameliof the limitations of the .wav format and not the fault of
our own code.
Post by Paul LicameliThere is also the question whether existing uses of
sampleCount should
Post by Paul Licamelibe signed or unsigned. Sometimes it really is meant to be a
count, as
Post by Paul Licameliof a buffer size, so unsigned would be appropriate, but
then the width
Post by Paul Licameliis probably excessive for the purpose. (The width of size_t
is only 4
Post by Paul Licamelibytes on my Mac.)
But sometimes it is meant as a quantized time identifying a
place in
Post by Paul Licamelian audio track, and negative times are not impossible (see
what time
Post by Paul Licamelishift can do), so it should be signed, and also very wide,
if we
Post by Paul Licamelireally mean to support days-long tracks and the possible
very big
------------------------------------------------------------------------------
Post by Paul Licameli_______________________________________________
audacity-devel mailing list
https://lists.sourceforge.net/lists/listinfo/audacity-devel
<https://lists.sourceforge.net/lists/listinfo/audacity-devel>
------------------------------------------------------------------------------
_______________________________________________
audacity-devel mailing list
https://lists.sourceforge.net/lists/listinfo/audacity-devel
<https://lists.sourceforge.net/lists/listinfo/audacity-devel>
------------------------------------------------------------------------------
_______________________________________________
audacity-devel mailing list
https://lists.sourceforge.net/lists/listinfo/audacity-devel
<https://lists.sourceforge.net/lists/listinfo/audacity-devel>
------------------------------------------------------------------------------
_______________________________________________ audacity-devel
https://lists.sourceforge.net/lists/listinfo/audacity-devel
<https://lists.sourceforge.net/lists/listinfo/audacity-devel>
------------------------------------------------------------------------------
_______________________________________________
audacity-devel mailing list
https://lists.sourceforge.net/lists/listinfo/audacity-devel