Passing Context unnecessarily. Is it costly?










1















So, I have this method getSpecialCharacter that's called from many different activities.



public static Spanned getSpecialCharacter(Context context)
Spanned spanned_character = CacheFactory.spannedCache.get("green");
if (spanned_character ==null)
spanned_character = getSymbolColor(context, " uf100", Color.GREEN);
CacheFactory.spannedCache.put("green", spanned_character);

return spanned_character;


private static Spanned getSymbolColor(Context context, String s_symbol, int color)
Typeface font = Typeface.createFromAsset(context.getAssets(), "font.ttf");
SpannableString ss = new SpannableString(s_symbol);
ss.setSpan (new CustomTypefaceSpan(font), 0, s_symbol.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new ForegroundColorSpan(color), 0, s_symbol.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;



It basically sets a special character from a loaded font and applies the color green. It works fine. This method gets called hundreds of times, so I cache the "special character" so that the font and the span is only called once (the first time).



I have to pass the context in order to load the font, however the context is only needed the first time. I wonder if I am losing some performance by passing Context hundreds of times unnecessarily. Does this cost any performance? Any suggestions to improve my code?










share|improve this question


























    1















    So, I have this method getSpecialCharacter that's called from many different activities.



    public static Spanned getSpecialCharacter(Context context)
    Spanned spanned_character = CacheFactory.spannedCache.get("green");
    if (spanned_character ==null)
    spanned_character = getSymbolColor(context, " uf100", Color.GREEN);
    CacheFactory.spannedCache.put("green", spanned_character);

    return spanned_character;


    private static Spanned getSymbolColor(Context context, String s_symbol, int color)
    Typeface font = Typeface.createFromAsset(context.getAssets(), "font.ttf");
    SpannableString ss = new SpannableString(s_symbol);
    ss.setSpan (new CustomTypefaceSpan(font), 0, s_symbol.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(new ForegroundColorSpan(color), 0, s_symbol.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return ss;



    It basically sets a special character from a loaded font and applies the color green. It works fine. This method gets called hundreds of times, so I cache the "special character" so that the font and the span is only called once (the first time).



    I have to pass the context in order to load the font, however the context is only needed the first time. I wonder if I am losing some performance by passing Context hundreds of times unnecessarily. Does this cost any performance? Any suggestions to improve my code?










    share|improve this question
























      1












      1








      1


      0






      So, I have this method getSpecialCharacter that's called from many different activities.



      public static Spanned getSpecialCharacter(Context context)
      Spanned spanned_character = CacheFactory.spannedCache.get("green");
      if (spanned_character ==null)
      spanned_character = getSymbolColor(context, " uf100", Color.GREEN);
      CacheFactory.spannedCache.put("green", spanned_character);

      return spanned_character;


      private static Spanned getSymbolColor(Context context, String s_symbol, int color)
      Typeface font = Typeface.createFromAsset(context.getAssets(), "font.ttf");
      SpannableString ss = new SpannableString(s_symbol);
      ss.setSpan (new CustomTypefaceSpan(font), 0, s_symbol.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      ss.setSpan(new ForegroundColorSpan(color), 0, s_symbol.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      return ss;



      It basically sets a special character from a loaded font and applies the color green. It works fine. This method gets called hundreds of times, so I cache the "special character" so that the font and the span is only called once (the first time).



      I have to pass the context in order to load the font, however the context is only needed the first time. I wonder if I am losing some performance by passing Context hundreds of times unnecessarily. Does this cost any performance? Any suggestions to improve my code?










      share|improve this question














      So, I have this method getSpecialCharacter that's called from many different activities.



      public static Spanned getSpecialCharacter(Context context)
      Spanned spanned_character = CacheFactory.spannedCache.get("green");
      if (spanned_character ==null)
      spanned_character = getSymbolColor(context, " uf100", Color.GREEN);
      CacheFactory.spannedCache.put("green", spanned_character);

      return spanned_character;


      private static Spanned getSymbolColor(Context context, String s_symbol, int color)
      Typeface font = Typeface.createFromAsset(context.getAssets(), "font.ttf");
      SpannableString ss = new SpannableString(s_symbol);
      ss.setSpan (new CustomTypefaceSpan(font), 0, s_symbol.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      ss.setSpan(new ForegroundColorSpan(color), 0, s_symbol.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      return ss;



      It basically sets a special character from a loaded font and applies the color green. It works fine. This method gets called hundreds of times, so I cache the "special character" so that the font and the span is only called once (the first time).



      I have to pass the context in order to load the font, however the context is only needed the first time. I wonder if I am losing some performance by passing Context hundreds of times unnecessarily. Does this cost any performance? Any suggestions to improve my code?







      android fonts assets android-context






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 2:07









      seekingStillnessseekingStillness

      1,80421430




      1,80421430






















          1 Answer
          1






          active

          oldest

          votes


















          2














          It shouldn't. You're only actually using the passed Context if the Spanned doesn't already exist. The real performance issue would be in creating the Typeface itself, which is done only once.



          Since you're caching the Spanned and not the Context parameter, you shouldn't have a problem.




          Edit:



          If you really want to be safe about it, pass context.getApplicationContext() when calling the method. That Context never "expires" as long as your app is running, and will be GCed as soon as it stops running.






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292185%2fpassing-context-unnecessarily-is-it-costly%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            It shouldn't. You're only actually using the passed Context if the Spanned doesn't already exist. The real performance issue would be in creating the Typeface itself, which is done only once.



            Since you're caching the Spanned and not the Context parameter, you shouldn't have a problem.




            Edit:



            If you really want to be safe about it, pass context.getApplicationContext() when calling the method. That Context never "expires" as long as your app is running, and will be GCed as soon as it stops running.






            share|improve this answer





























              2














              It shouldn't. You're only actually using the passed Context if the Spanned doesn't already exist. The real performance issue would be in creating the Typeface itself, which is done only once.



              Since you're caching the Spanned and not the Context parameter, you shouldn't have a problem.




              Edit:



              If you really want to be safe about it, pass context.getApplicationContext() when calling the method. That Context never "expires" as long as your app is running, and will be GCed as soon as it stops running.






              share|improve this answer



























                2












                2








                2







                It shouldn't. You're only actually using the passed Context if the Spanned doesn't already exist. The real performance issue would be in creating the Typeface itself, which is done only once.



                Since you're caching the Spanned and not the Context parameter, you shouldn't have a problem.




                Edit:



                If you really want to be safe about it, pass context.getApplicationContext() when calling the method. That Context never "expires" as long as your app is running, and will be GCed as soon as it stops running.






                share|improve this answer















                It shouldn't. You're only actually using the passed Context if the Spanned doesn't already exist. The real performance issue would be in creating the Typeface itself, which is done only once.



                Since you're caching the Spanned and not the Context parameter, you shouldn't have a problem.




                Edit:



                If you really want to be safe about it, pass context.getApplicationContext() when calling the method. That Context never "expires" as long as your app is running, and will be GCed as soon as it stops running.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 14 '18 at 11:00

























                answered Nov 14 '18 at 2:16









                TheWandererTheWanderer

                7,51021129




                7,51021129





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292185%2fpassing-context-unnecessarily-is-it-costly%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo